import ( "fmt" "github.com/spf13/viper" ) func main() { viper.SetConfigFile("config.yaml") err := viper.ReadInConfig() if err != nil { panic(fmt.Errorf("Fatal error config file: %s \n", err)) } fmt.Println(viper.GetString("db.host")) }
import ( "fmt" "github.com/spf13/viper" ) func main() { viper.SetConfigFile("config.yaml") viper.SetDefault("db.host", "localhost") viper.AutomaticEnv() if err := viper.ReadInConfig(); err != nil { panic(fmt.Errorf("Fatal error config file: %s \n", err)) } host := viper.GetString("db.host") username := viper.GetString("db.username") password := viper.GetString("db.password") fmt.Println("Host:", host) fmt.Println("Username:", username) fmt.Println("Password:", password) }This example sets default values for the `db.host` key and reads them from both the configuration file and environment variables, using `viper.AutomaticEnv()`. It then prints the values for the `db.host`, `db.username`, and `db.password` keys. Package Library: `github.com/spf13/viper`