December 2021
Intermediate to advanced
510 pages
11h 20m
English
When you use the Cobra generator to create the boilerplate code for your application, it automatically enables Viper. Viper is a configuration management solution for Go applications which allows you to specify configuration options for your application in several ways, including configuration files, environment variables, and command-line flags.
Cobra enables Viper by running the function initConfig when initializing the application. This function is defined in the cmd/root.go file:
| | func initConfig() { |
| | if cfgFile != "" { |
| | // Use config file from the flag. |
| | viper.SetConfigFile(cfgFile) |
| | } else { |
| | // Find home directory. |
| | home, err := homedir.Dir() |
| | if err ... |
Read now
Unlock full access