Golang有很多第三方包,其中的 viper 支持读取多种配置文件信息。本文只是做一个小小demo,用来学习入门用的。
1、安装
go get github.com/spf13/viper
2、编写一个yaml的配置文件,config.yaml
database:
host: 127.0.0.1
user: root
dbname: test
pwd: 123456
3、编写学习脚本main.go,读取config.yaml配置信息
package main
import (
"fmt"
"os"
"github.com/spf13/viper"
)
func main() {
//获取项目的执行路径
path, err := os.Getwd()
if err != nil {
panic(err)
}
config := viper.New()
config.AddConfigPath(path) //设置读取的文件路径
config.SetConfigName("config") //设置读取的文件名
config.SetConfigType("yaml") //设置文件的类型
//尝试进行配置读取
if err := config.ReadInConfig(); err != nil {
panic(err)
}
//打印文件读取出来的内容:
fmt.Println(config.Get("database.host"))
fmt.Println(config.Get("database.user"))
fmt.Println(config.Get("database.dbname"))
fmt.Println(config.Get("database.pwd"))
}
4、执行go run main.go
输出:
127.0.0.1
root
test
123456
ok!