Skip to content

Instantly share code, notes, and snippets.

View mainawycliffe's full-sized avatar

Maina Wycliffe mainawycliffe

View GitHub Profile
viper.AutomaticEnv()
err := viper.WriteConfigAs("path/to/ConfigFile.yaml")
if err != nil {
return nil, err
}
err := viper.SafeWriteConfig("path/to/ConfigFile.yaml")
if err != nil {
return nil, err
}
err := viper.WriteConfig()
if err != nil {
return nil, err
}
err := viper.SafeWriteConfig()
if err != nil {
return nil, err
}
viper.OnConfigChange(func(e fsnotify.Event) {
// do something whenever a configguration changes
})
viper.WatchConfig()
allSettings := viper.AllSettings()
allKeys := viper.AllKeys()
if(viper.IsSet("key")) {
// do something
}
string := viper.GetString("key")
int := viper.GetInt("int")
bool := viper.GetBool("key")
duration := viper.GetDuration("key")
stringMap := viper.GetStringMap("key")
viper.SetDefault("logs", true)
viper.SetDefault("port", 8080)
viper.SetDefault("db", "mongodb://localhost:27017")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %w \n", err))
}