2020-12-26 15:17:20 +00:00
|
|
|
# Options
|
|
|
|
|
|
|
|
Go-micro makes the use of [functional options](https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis). It's a design
|
|
|
|
pattern that allows the addition of new options without changing the method signature.
|
|
|
|
|
2021-01-20 21:28:48 +00:00
|
|
|
Each package has an [Option](https://godoc.org/github.com/asim/go-micro#Option) type
|
2020-12-26 15:17:20 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
type Option func(*Options)
|
|
|
|
```
|
|
|
|
|
2021-01-20 21:28:48 +00:00
|
|
|
Options such as the [Name](https://godoc.org/github.com/asim/go-micro#Name) function exist to set a service name
|
2020-12-26 15:17:20 +00:00
|
|
|
|
|
|
|
The implementation is as follows
|
|
|
|
|
|
|
|
```
|
|
|
|
func Name(n string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Server.Init(server.Name(n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
Here's an example at the top level
|
|
|
|
|
|
|
|
```
|
2021-01-20 13:54:31 +00:00
|
|
|
import "github.com/asim/go-micro/v3"
|
2020-12-26 15:17:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
service := micro.NewService(
|
|
|
|
micro.Name("my.service"),
|
|
|
|
)
|
|
|
|
```
|