How does Viper parse underlined configuration items? #1148
-
I used Viper to parse YAML to struct, but I found that I couldn't parse it when the struct key was different with the key in the YAML.Here is my struct and the yaml: level: info
log_path: "/data/logs"
group: "sre"
max_size: 50
max_backups: 10
max_age: 7
compress: true
stdout: true type Config struct {
Level string `yaml:"level"`
LogPath string `yaml:"log_path"`
Stdout bool `yaml:"stdout"`
App string `yaml:"app"`
Group string `yaml:"group"`
MaxSize int `yaml:"max_size"`
MaxBackups int `yaml:"max_backups"`
MaxAge int `yaml:"max_age"`
Compress bool `yaml:"compress"`
}
var config *Config
func main(){
viper.SetConfigFile("config.yaml")
viper.ReadInConfig()
viper.Unmarshal(&config)
fmt.Println(config)
}
output:
&{info true sre 0 0 0 true} When the struct key has a different key name than a key in YAML, it cannot resolve it, like "max_size", How do I use viper parse it like I parse a YAML file? |
Beta Was this translation helpful? Give feedback.
Answered by
sagikazarmark
Jun 3, 2021
Replies: 1 comment
-
https://github.com/spf13/viper/blob/master/TROUBLESHOOTING.md#unmarshaling-doesnt-work |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
sagikazarmark
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/spf13/viper/blob/master/TROUBLESHOOTING.md#unmarshaling-doesnt-work