I'm working on Go project where I need to set up some configurations and for that I am using Viper.
When I merge map with viper object I can't get individual config fields after, however when I do viper.AllSettings()
I do get all the settings, for example:
package main
import (
"fmt"
"github.com/spf13/viper"
)
type config map[string]interface{}
func defaultConfig() config {
return config{
"prod": false,
"amqp": config{
"url": "url",
"workers": 2,
},
"log": config{
"filename": "viperConfig",
},
}
}
func init() {
conf := defaultConfig()
if err := viper.MergeConfigMap(conf); err != nil {
panic(err)
}
fmt.Println(viper.AllSettings())
fmt.Println(viper.GetString("log.filename"))
}
func main() {
// Some code here...
}
Output:
map[amqp:map[url:url workers:2] log:map[filename:viperConfig] prod:false]
// viperConfig is not printed out
Field "log" is a map in your config. You should be able to access its entries through GetStringMapString
.
func init() {
conf := defaultConfig()
if err := viper.MergeConfigMap(conf); err != nil {
panic(err)
}
fmt.Println(viper.AllSettings())
fmt.Println(viper.GetStringMapString("log")["filename"])
}