opensource software
github.com/spf13/viper
Load configuration file
viper is configuration framework and it’s widely used in other opensource golang software such as hugo, kubernets.
1 | go get "github.com/spf13/viper" |
load json format
config file is like
1 | json config example, |
1 |
|
nested config with json format
nested json config example
1 | { |
1 | //code example to load config |
load yaml format
config.yaml config example
1 | environments: |
viper.SetConfigFile("$PATH/config.yaml")
viper.ReadInConfig()
//get the environments part
server := viper.Get("environments").(map[string]interface {})
//get the dev part
dev := server["dev"].(map[string]interface{})
fmt.Println("reading server:", dev["server"].(string))
`