In project I set config like this
- map structure to map config.yaml with model
- use viper to load it
type Config struct {
MyString string `mapstructure:"MyString"`
MyInt int `mapstructure:"MyInt"`
MyBool bool `mapstructure:"MyBool"`
MyArray StringSlice `mapstructure:"MyArray"`
}
func main() {
// Automatically load environment variables into Viper
viper.AutomaticEnv()
// Create a configuration structure
var config Config
// Use Viper's Unmarshal to decode environment variables into the structure
if err := viper.Unmarshal(&config); err != nil {
fmt.Println("Error decoding configuration:", err)
return
}
}
but in server use env instead config so use AutomaticEnv for check if have env key match will replace it
I used to set MyArray separate by space that when I don’t use map structure
MyArray=aaa bbb ccc
I set value directly by viper.GetStringSlice and It work
Config.MyArray=: viper.GetStringSlice("MyArray"),
But if use map structure default delimiter is comma!!
if use space delimiter string cannot separate to array