viper基本使用
导入包: go get -u github.com/spf13/viper
以读取 yaml配置文件为例:
func loadConfig() {
v := viper.New()
v.SetConfigName("config") // 配置文件的文件名(不需要扩展名)
v.SetConfigType("yaml") // 配置文件的类型(可以是 yaml、json、toml 等)
v.AddConfigPath("./") // 可选:指定配置文件的搜索路径(默认为当前目录)
err := v.ReadInConfig()
if err != nil {
fmt.Println("Load config.yaml failed, check err output.")
return
}
// define your global constants here
v.Get() // return interface{}
v.GetString("")
}