Golang Viper 库 初始化:传入配置文件路径
使用 Viper 库初始化,传入配置文件路径的示例代码如下:
import (
"github.com/spf13/viper"
)
func main() {
// 初始化 viper 库
viper.SetConfigFile('./config.yaml')
// 读取配置文件
if err := viper.ReadInConfig(); err != nil {
panic(fmt.Errorf('fatal error config file: %s', err))
}
// 使用配置
fmt.Println(viper.Get('key'))
}
在上面的例子中,我们将配置文件路径设置为 ./config.yaml,然后使用 viper.ReadInConfig() 方法读取配置文件。读取配置文件后,我们可以使用 viper.Get() 方法获取配置值。
原文地址: https://www.cveoy.top/t/topic/n8w4 著作权归作者所有。请勿转载和采集!