1. support several formats of configuration
config.yaml
name: 'bobby' port: 12334
main.go to quick start
package main import ( "fmt" "github.com/spf13/viper" ) type ServerConfig struct { ServiceName string `mapstructure:"name"` // using mapstructure to support other forms of config & using mapstructure pkg Port int `mapstructure:"port"` } func main() { v := viper.New() // return a Viper v.SetConfigFile("viper_test/ch01/config.yaml") //v.SetConfigFile("config.yaml") if err := v.ReadInConfig(); err != nil { panic(err) } sc := ServerConfig{} if err := v.Unmarshal(&sc); err != nil { // unserilaize to Object panic(err) } fmt.Println(sc) fmt.Println(v.Get("name")) }
标签:err,yaml,sc,Viper,Go,mapstructure,configuration,config From: https://www.cnblogs.com/sabertobih/p/17808484.html