项目配置

https://github.com/spf13/viper

用来搞定 Go 应用中配置的库。支持多种配置文件类型、监控并重新加载配置文件、远程读取配置系统等

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
viper.SetConfigName("config") // 配置文件名,不包括后缀
viper.SetConfigType("yaml") // 配置文件的后缀
viper.AddConfigPath("/etc/appname/")   // 查找配置文件的目录
viper.AddConfigPath("$HOME/.appname")  // 支持查找多个目录
// 异常处理
if err := viper.ReadInConfig(); err != nil {
    if _, ok := err.(viper.ConfigFileNotFoundError); ok {
        // 如果没有找到配置文件
    } else {
        // 找到了配置文件,但出现了其他错误
    }
}