├── .gitignore ├── pkg ├── billing │ └── service.go ├── subscriptions │ └── service.go └── accounts │ └── service.go ├── cmd └── main.go └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.idea 2 | -------------------------------------------------------------------------------- /pkg/billing/service.go: -------------------------------------------------------------------------------- 1 | package billing 2 | 3 | // New initialize billing struct 4 | func New() Client { 5 | return &billing{} 6 | } 7 | 8 | // Client is interface for billing struct 9 | type Client interface { 10 | Increase() 11 | } 12 | 13 | type billing struct { 14 | balance string 15 | } 16 | 17 | func (a billing) Increase() { 18 | // do stuff 19 | } 20 | -------------------------------------------------------------------------------- /pkg/subscriptions/service.go: -------------------------------------------------------------------------------- 1 | package subscriptions 2 | 3 | import "fmt" 4 | 5 | // New initialize subscriptions struct 6 | func New(propVal int64) Client { 7 | return &subs{Prop: propVal} 8 | } 9 | 10 | // Client is interface for subscriptions struct 11 | type Client interface { 12 | Bind(int64) 13 | } 14 | 15 | type subs struct { 16 | Prop int64 17 | } 18 | 19 | func (s subs) Bind(propValue int64) { 20 | fmt.Println(fmt.Sprintf("\ts.Prop (before): %#v", s.Prop)) 21 | 22 | s.Prop = propValue 23 | fmt.Println(fmt.Sprintf("\ts.Prop (after): %#v\n", s.Prop)) 24 | } -------------------------------------------------------------------------------- /cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "../pkg/billing" 5 | "../pkg/subscriptions" 6 | "../pkg/accounts" 7 | "fmt" 8 | ) 9 | 10 | func main() { 11 | fmt.Println("Variant_1:") 12 | variant1() 13 | 14 | fmt.Println("Variant_2:") 15 | variant2() 16 | 17 | fmt.Println("Variant_3:") 18 | variant3() 19 | } 20 | 21 | 22 | func variant1() { 23 | accs := accounts.New() 24 | 25 | srvSubscriptions := subscriptions.New(111) 26 | 27 | sbs := accounts.BindSubscriptions(srvSubscriptions) 28 | 29 | accs.AddOptions(sbs) 30 | 31 | accs.Process(222) 32 | } 33 | 34 | func variant2() { 35 | accs := accounts.New() 36 | 37 | srvSubscriptions := subscriptions.New(333) 38 | srvBilling := billing.New() 39 | 40 | sbs := accounts.BindSubscriptions(srvSubscriptions) 41 | bl := accounts.BindBilling(srvBilling) 42 | 43 | accs.AddOptions(sbs, bl) 44 | 45 | accs.Process(444) 46 | } 47 | 48 | func variant3() { 49 | accs := accounts.New() 50 | 51 | srvSubscriptions := subscriptions.New(555) 52 | srvBilling := billing.New() 53 | 54 | sbs := accounts.BindSubscriptions(srvSubscriptions) 55 | bl := accounts.BindBilling(srvBilling) 56 | 57 | accs.AddOptions(sbs) 58 | accs.AddOptions(bl) 59 | 60 | accs.Process(777) 61 | } 62 | -------------------------------------------------------------------------------- /pkg/accounts/service.go: -------------------------------------------------------------------------------- 1 | package accounts 2 | 3 | import ( 4 | "../billing" 5 | "../subscriptions" 6 | ) 7 | 8 | // Init ------------------ 9 | 10 | // New initialize account struct 11 | func New() Client { 12 | return &accs{} 13 | } 14 | 15 | // Client is interface for account struct 16 | type Client interface { 17 | AddOptions(...Option) 18 | Process(int64) 19 | } 20 | 21 | type accs struct { 22 | verbosity int 23 | srvSubscriptions subscriptions.Client 24 | srvBilling billing.Client 25 | } 26 | 27 | // Dependencies ------------------ 28 | 29 | // Option self-referential function 30 | type Option func(*accs) 31 | 32 | func (f *accs) AddOptions(opts ...Option) { 33 | for _, opt := range opts { 34 | opt(f) 35 | } 36 | } 37 | 38 | // Verbosity set verbosity value 39 | func Verbosity(v int) Option { 40 | return func(f *accs) { 41 | f.verbosity = v 42 | } 43 | } 44 | 45 | // BindSubscriptions - bind property: subscription service 46 | func BindSubscriptions(v subscriptions.Client) Option { 47 | return func(f *accs) { 48 | f.srvSubscriptions = v 49 | } 50 | } 51 | 52 | // BindBilling - bind property: billing service 53 | func BindBilling(v billing.Client) Option { 54 | return func(f *accs) { 55 | f.srvBilling = v 56 | } 57 | } 58 | 59 | // Public methods ------------------ 60 | 61 | func (f accs) Process(val int64) { 62 | f.srvSubscriptions.Bind(val) 63 | } 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # go-service-options 2 | 3 | 4 | ``` 5 | . 6 | ├── cmd 7 | │   └── main.go 8 | ├── pkg 9 | │   ├── accounts 10 | │   │   └── service.go 11 | │   ├── billing 12 | │   │   └── service.go 13 | │   └── subscriptions 14 | │   └── service.go 15 | └── README.md 16 | ``` 17 | 18 | Entry point: 19 | ```go 20 | package main 21 | 22 | import ( 23 | "../pkg/accounts" 24 | "../pkg/billing" 25 | "../pkg/subscriptions" 26 | "fmt" 27 | ) 28 | 29 | func main() { 30 | fmt.Println("Variant_1:") 31 | variant1() 32 | 33 | fmt.Println("Variant_2:") 34 | variant2() 35 | 36 | fmt.Println("Variant_3:") 37 | variant3() 38 | } 39 | 40 | 41 | func variant1() { 42 | subs := subscriptions.New() 43 | 44 | srvAccounts := accounts.New(111) 45 | 46 | ac := subscriptions.BindAccounts(srvAccounts) 47 | 48 | subs.AddOptions(ac) 49 | 50 | subs.Process(222) 51 | } 52 | 53 | func variant2() { 54 | subs := subscriptions.New() 55 | 56 | srvAccounts := accounts.New(333) 57 | srvBilling := billing.New() 58 | 59 | ac := subscriptions.BindAccounts(srvAccounts) 60 | bl := subscriptions.BindBilling(srvBilling) 61 | 62 | subs.AddOptions(ac, bl) 63 | 64 | subs.Process(444) 65 | } 66 | 67 | func variant3() { 68 | subs := subscriptions.New() 69 | 70 | srvAccounts := accounts.New(555) 71 | srvBilling := billing.New() 72 | 73 | ac := subscriptions.BindAccounts(srvAccounts) 74 | bl := subscriptions.BindBilling(srvBilling) 75 | 76 | subs.AddOptions(ac) 77 | subs.AddOptions(bl) 78 | 79 | subs.Process(777) 80 | } 81 | 82 | 83 | ``` 84 | 85 | 86 | Output result: 87 | ``` 88 | Variant_1: 89 | a.Prop (before): 111 90 | a.Prop (after): 222 91 | 92 | Variant_2: 93 | a.Prop (before): 333 94 | a.Prop (after): 444 95 | 96 | Variant_3: 97 | a.Prop (before): 555 98 | a.Prop (after): 777 99 | 100 | ``` --------------------------------------------------------------------------------