github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/docs/v2/library/go-options.md (about)

     1  ---
     2  title: Options
     3  keywords: go-micro, framework
     4  tags: [go-micro, framework]
     5  sidebar: home_sidebar
     6  permalink: /go-options
     7  summary: Setting and using Go Micro options
     8  ---
     9  
    10  Go Micro uses a variadic options model for the design of passing arguments for the creation and initialisation of 
    11  packages and also as optional params for methods. This offers flexibility in power for extending our option usage 
    12  across plugins.
    13  
    14  ## Overview
    15  
    16  When create a new service you have option of passing additional parameters such as setting the name, version, 
    17  the message broker, registry or store to use along with all the other internals. 
    18  
    19  Options are normally defined as follows
    20  
    21  ```
    22  type Options struct {
    23    Name string
    24    Version string
    25    Broker broker.Broker
    26    Registry registry.Registry
    27  }
    28  
    29  type Option func(*Options)
    30  
    31  // set the name
    32  func Name(n string) Option {
    33  	return func(o *Options) {
    34  		o.Name = n
    35  	}
    36  }
    37  
    38  // set the broker
    39  func Broker(b broker.Broker) Option {
    40  	return func(o *Options) {
    41  		o.Broker = b
    42  	}
    43  }
    44  ```
    45  
    46  These can then be set as follows
    47  
    48  ```
    49  service := micro.NewService(
    50  	micro.Name("foobar"),
    51  	micro.Broker(broker),
    52  )
    53  ```
    54  
    55  ## Service Options
    56  
    57  Within Go Micro we have a number of options that can be set including the underlying packages that will be used 
    58  for things such as authentication, configuration and storage. You can use `service.Options()` to access these.
    59  
    60  The packages such as auth, config, registry, store, etc will default to our zero dep plugins. Where you want 
    61  to configure them via env vars or flags you can specify `service.Init()` to parse them.
    62  
    63  For example, if you replace the memory store to use a file store it can be done as follows
    64  
    65  ```
    66  ## as an env var
    67  MICRO_STORE=file go run main.go
    68  
    69  ## or as a flag
    70  go run main.go --store=file
    71  ```
    72  
    73  Internally the store is then accessible via the options
    74  
    75  ```
    76  service := micro.NewService(
    77  	micro.Name("foobar"),
    78  )
    79  
    80  service.Init()
    81  
    82  store := service.Options().Store
    83  ```
    84  
    85