1
go get github.com/spf13/viper

viper 是什么

  • go 开发工具,主要是用于处理各种格式的配置文件,简化程序配置的读取问题
  • viper 支持:
    • 设置默认配置
    • 支持读取 JSON TOML YAML HCL 和 Java 属性配置文件
    • 监听配置文件变化,实时读取读取配置文件内容
    • 读取环境变量值
    • 读取远程配置系统 (etcd Consul) 和监控配置变化
    • 读取命令 Flag 值
    • 读取 buffer 值
    • 读取确切值

quick start

json 配置文件 (config.json)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"port": 10666,
"mysql": {
"url": "(127.0.0.1:3306)/biezhi",
"username": "root",
"password": "123456"
},
"redis": ["127.0.0.1:6377", "127.0.0.1:6378", "127.0.0.1:6379"],
"smtp": {
"enable": true,
"addr": "mail_addr",
"username": "mail_user",
"password": "mail_password",
"to": ["xxx@gmail.com", "xxx@163.com"]
}
}

yaml 配置文件 (config1.yaml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
port: 10666
mysql:
url: "(127.0.0.1:3306)/biezhi"
username: root
password: 123456
redis:
- 127.0.0.1:6377
- 127.0.0.1:6378
- 127.0.0.1:6379
smtp:
enable: true
addr: mail_addr
username: mail_user
password: mail_password
to:
- xxx@gmail.com
- xxx@163.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main

import (
"fmt"
"log"

"github.com/spf13/viper"
)

func init() {
// viper.SetConfigName("config1") // 读取yaml配置文件
viper.SetConfigName("config") // 读取json配置文件
//viper.AddConfigPath("/etc/appname/") //设置配置文件的搜索目录
//viper.AddConfigPath("$HOME/.appname") // 设置配置文件的搜索目录
viper.AddConfigPath(".") // 设置配置文件和可执行二进制文件在用一个目录
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; ignore error if desired
log.Println("no such config file")
} else {
// Config file was found but another error was produced
log.Println("read config error")
}
log.Fatal(err) // 读取配置文件失败致命错误
}
}

func main() {
fmt.Println("获取配置文件的port", viper.GetInt("port"))
fmt.Println("获取配置文件的mysql.url", viper.GetString(`mysql.url`))
fmt.Println("获取配置文件的mysql.username", viper.GetString(`mysql.username`))
fmt.Println("获取配置文件的mysql.password", viper.GetString(`mysql.password`))
fmt.Println("获取配置文件的redis", viper.GetStringSlice("redis"))
fmt.Println("获取配置文件的smtp", viper.GetStringMap("smtp"))
}
  • viper.SetConfigName (“config”) 设置配置文件名为 config, 不需要配置文件扩展名,配置文件的类型 viper 会自动根据扩展名自动匹配.

  • viper.AddConfigPath (“.”) 设置配置文件搜索的目录,. 表示和当前编译好的二进制文件在同一个目录。可以添加多个配置文件目录,如在第一个目录中找到就不不继续到其他目录中查找.

  • viper.ReadInConfig () 加载配置文件内容

  • viper.Get 获取配置文件中配置项的信息

io.Reader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main

import (
"bytes"
"fmt"

"github.com/spf13/viper"
)

func main() {
v := viper.New()
v.SetConfigType("json") // 设置配置文件的类型

// 配置文件内容
var jsonExample = []byte(`
{
"port": 10666,
"mysql": {
"url": "(127.0.0.1:3306)/biezhi",
"username": "root",
"password": "123456"
},
"redis": ["127.0.0.1:6377", "127.0.0.1:6378", "127.0.0.1:6379"],
"smtp": {
"enable": true,
"addr": "mail_addr",
"username": "mail_user",
"password": "mail_password",
"to": ["xxx@gmail.com", "xxx@163.com"]
}
}
`)
//创建io.Reader
v.ReadConfig(bytes.NewBuffer(jsonExample))

fmt.Println("获取配置文件的port", v.GetInt("port"))
fmt.Println("获取配置文件的mysql.url", v.GetString(`mysql.url`))
fmt.Println("获取配置文件的mysql.username", v.GetString(`mysql.username`))
fmt.Println("获取配置文件的mysql.password", v.GetString(`mysql.password`))
fmt.Println("获取配置文件的redis", v.GetStringSlice("redis"))
fmt.Println("获取配置文件的smtp", v.GetStringMap("smtp"))
}