go读取yaml配置文件
配置文件
cfg.yaml
name: testsvr
blackip:
- 1.1.1.1
- 2.2.2.2
auto: false
port: 9999
clusterip: [3.3.3.3,4.4.4.4]
health:
url: http://localhost:5444 # 地址
cmd: netstat -anlt # 命令
interval: 3s # 间隔时间
timeout: 20s # 超时时间
disable: true # 是否启用
读取配置文件
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
)
type Cfg struct {
Name string `yaml:name`
Auto bool `yaml:auto`
Port int `yaml:port`
Blackip []string
Clusterip []string
Health Health
}
type Health struct {
Url string
Cmd string
Interval string
Timeout string
Disable bool
}
func main() {
file, err := os.Open("cfg.yaml")
if err != nil {
panic(err)
}
bytes, err := ioutil.ReadAll(file)
if err != nil {
panic(err)
}
cfg := Cfg{}
err = yaml.Unmarshal(bytes, &cfg)
if err != nil {
panic(err)
}
fmt.Println(cfg)
}
更多更全拥有的代码下载
链接: 点我获取yaml
链接: 点我全要