Viper 配置設定黨
配置設定黨
What is Viper
func viperSetting(configPath string) error {
viper.SetConfigType(defaultConfigType)
viper.AddConfigPath(configPath)
viper.SetConfigName(serviceFileName)
if err := viper.ReadInConfig(); err != nil {
return errors.Errorf("viper.ReadInConfig error(%v)", err)
}
fmt.Println("viper.ConfigFileUsed OK:", viper.ConfigFileUsed())
viper.AutomaticEnv() //export SERVICE_NAME=test2
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
err := viper.Unmarshal(&configData) //bind struct
if err != nil {
return errors.Errorf("viper.Unmarshal:(%v)", err)
}
fmt.Println("data:", viper.Get("service.name"))
fmt.Println("Service.Name:", configData.Service.Name)
return nil
}
watch
reference
reference
Last updated