Configuring Structs
Dec 14, 2016 23:47 UTC
· 80 words
· 1 minute read
golang
Compilation of tips and tricks
Config for flags
e.g.
- Have a config struct to contain the variables
- Use
flag.<T>Var(*ptr)
to link up the config to flags - Call
flag.Parse
- use the config as a global
Functional configuration
- You have a struct for your domain
- For each configurable field, create a option function
- You can pass these functions as a variadic argument. e.g. New(… Opts)
- (-) You have to write one for each parameter that you want to be able to configure
See Also
Go to top