服务器之家

服务器之家 > 正文

golang解析yaml文件操作

时间:2021-03-20 00:57     来源/作者:思维的深度

首先安装解析的第三方包:

go get gopkg.in/yaml.v2

示例:

?
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
 "os"
 "log"
 "fmt"
 "encoding/json"
 "gopkg.in/yaml.v2"
)
 
type Config struct {
 Test Test `yaml:"test"`
}
 
type Test struct {
 User []string `yaml:"user"`
 MQTT MQ `yaml:"mqtt"`
 Http HTTP `yaml:"http"`
}
 
type HTTP struct {
 Port string `yaml:"port"`
 Host string `yaml:"host"`
}
 
type MQ struct {
 Host string `yaml:"host"`
 Username string `yaml:"username"`
 Password string `yaml:"password"`
}
 
//read yaml config
//注:path为yaml或yml文件的路径
func ReadYamlConfig(path string) (*Config,error){
 conf := &Config{}
 if f, err := os.Open(path); err != nil {
  return nil,err
 } else {
 yaml.NewDecoder(f).Decode(conf)
 }
 return conf,nil
}
 
//test yaml
func main() {
 conf,err := ReadYamlConfig("D:/test_yaml/test.yaml")
 if err != nil {
 log.Fatal(err)
 }
 
 byts,err := json.Marshal(conf)
 if err != nil {
 log.Fatal(err)
 }
 
 fmt.Println(string(byts))
}

test.yaml内容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
test:
 user:
 - Tom
 - Lily
 - Skay
 
 mqtt:
  host: localhost:1883
  username: test
  password: test
 
 http: {port: "8080", host: "127.0.0.1"}

运行结果:

{"Test":{"User":["Tom","Lily","Skay"],"MQTT":{"Host":"localhost:1883","Username":"test","Password":"test"},"Http":{"Port":"8080","Host":"127.0.0.1"}}}

补充:golang 读取yml格式,多结构体级联

1.安装yml解析包

进入到gopath下执行命令

go get gopkg.in/yaml.v2

源码地址https://github.com/go-yaml/yaml

2.设置配置文件config.yml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
ipport: 192.168.2.95:10000
startsendtime: 2017-01-02 08:08:08
sendmaxcountperday: 100
devices:
- devid: 123456789
 nodes:
 - pkid: 0
  bkid: 0
  index: 0
  minvalue: 0
  maxvalue: 60
  datatype: normal
 - pkid: 0
  bkid: 0
  index: 0
  datatype: boolean
- devid: 10001
 nodes:
 - pkid: 0
  bkid: 1
  index: 0
  datatype: boolean
warnfrequency: 10
sendfrequency: 10

3.编写测试类

?
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
43
44
package main
import (
  "fmt"
  "gopkg.in/yaml.v2"
  "io/ioutil"
)
 
//配置文件中字母要小写,结构体属性首字母要大写
 
type Myconf struct {
  Ipport  string
  StartSendTime string
  SendMaxCountPerDay int
  Devices []Device
  WarnFrequency int
  SendFrequency int
}
type Device struct {
  DevId string
  Nodes []Node
}
type Node struct {
  PkId string
  BkId string
  Index string
  MinValue float32
  MaxValue float32
  DataType string
}
 
func main() {
  data, _ := ioutil.ReadFile("config.yml")
  fmt.Println(string(data))
  t := Myconf{}
  //把yaml形式的字符串解析成struct类型
  yaml.Unmarshal(data, &t)
  fmt.Println("初始数据", t)
  if(t.Ipport==""){
    fmt.Println("配置文件设置错误")
    return;
  }
  d, _ := yaml.Marshal(&t)
  fmt.Println("看看 :", string(d))
}

4.注意

1.配置文件中字母要小写,结构体属性首字母要大写,开发比较快

也可以指定如:yaml:"c",只不过有点麻烦,当然如果重命名必须要指定

2.yaml:",flow"

这个意思是将数组用[“a”,”b”]这样的格式展示,默认展示形式是

- a

- b

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://skaygo.blog.csdn.net/article/details/85791430

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部