当前位置:   article > 正文

Go语言之读取yaml配置文件,转换成struct结构,json形式输出

yaml转struct

1、例子1

1.1、yaml文件内容如下:

  1. host: localhost:3306
  2. user: root
  3. pwd: 123456
  4. dbname: test

1.2、代码如下:

  1. //将yaml文件,转换成对象,再转换成json格式输出
  2. package main
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "gopkg.in/yaml.v2"
  7. "io/ioutil"
  8. )
  9. //定义conf类型
  10. //类型里的属性,全是配置文件里的属性
  11. type conf struct {
  12. Host string `yaml: "host"`
  13. User string `yaml:"user"`
  14. Pwd string `yaml:"pwd"`
  15. Dbname string `yaml:"dbname"`
  16. }
  17. func main() {
  18. var c conf
  19. //读取yaml配置文件
  20. conf := c.getConf()
  21. fmt.Println(conf)
  22. //将对象,转换成json格式
  23. data, err := json.Marshal(conf)
  24. if err != nil {
  25. fmt.Println("err:\t", err.Error())
  26. return
  27. }
  28. //最终以json格式,输出
  29. fmt.Println("data:\t", string(data))
  30. }
  31. //读取Yaml配置文件,
  32. //并转换成conf对象
  33. func (c *conf) getConf() *conf {
  34. //应该是 绝对地址
  35. yamlFile, err := ioutil.ReadFile("E:\\Program\\go2\\goPath\\src\\xingej-go\\xingej-go\\xingej-go666\\lib\\yaml\\conf.yaml")
  36. if err != nil {
  37. fmt.Println(err.Error())
  38. }
  39. err = yaml.Unmarshal(yamlFile, c)
  40. if err != nil {
  41. fmt.Println(err.Error())
  42. }
  43. return c
  44. }

如果某一个包,本地没有的话,可以在cmd中使用下面的命令,进行下载,如:
go get gopkg.in/yaml.v2

==基本格式:== go get 包的路径

2、例子2,该配置文件中,存在map,slice类型,稍微复杂些

2.1、 配置文件内容:

  1. apiVersion: v1
  2. kind: KafkaCluster2
  3. metadata:
  4. name: kafka-operator
  5. labels:
  6. config1:
  7. address: kafka-operator-labels-01
  8. id: kafka-operator-labels-02
  9. name: mysql-example-cluster-master
  10. nodeName: 172.16.91.21
  11. role: master
  12. config2:
  13. address: kafka-operator-labels-01
  14. id: kafka-operator-labels-02
  15. name: mysql-example-cluster-slave
  16. nodeName: 172.16.91.110
  17. role: slave
  18. spec:
  19. replicas: 1
  20. name: kafka-controller
  21. image: 172.16.26.4:5000/nginx
  22. ports: 8088
  23. conditions:
  24. - containerPort: 8080
  25. requests:
  26. cpu: "0.25"
  27. memory: "512Mi"
  28. limits:
  29. cpu: "0.25"
  30. memory: "1Gi"
  31. - containerPort: 9090
  32. requests:
  33. cpu: "0.33"
  34. memory: "333Mi"
  35. limits:
  36. cpu: "0.55"
  37. memory: "5Gi"

2.2、 代码如下:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "gopkg.in/yaml.v2"
  6. "io/ioutil"
  7. )
  8. type KafkaCluster struct {
  9. ApiVersion string `yaml:"apiVersion"`
  10. Kind string `yaml: "kind"`
  11. Metadata Metadata `yaml: "metadata"`
  12. Spec Spec `yaml: "spec"`
  13. }
  14. type Metadata struct {
  15. Name string `yaml:"name"`
  16. //map类型
  17. Labels map[string]*NodeServer `yaml:"labels"`
  18. }
  19. type NodeServer struct {
  20. Address string `yaml: "address"`
  21. Id string `yaml: "id"`
  22. Name string `yaml: "name"`
  23. //注意,属性里,如果有大写的话,tag里不能存在空格
  24. //如yaml: "nodeName" 格式是错误的,中间多了一个空格,不能识别的
  25. NodeName string `yaml:"nodeName"`
  26. Role string `yaml: "role"`
  27. }
  28. type Spec struct {
  29. Replicas int `yaml: "replicas"`
  30. Name string `yaml: "name"`
  31. Image string `yaml: "iamge"`
  32. Ports int `yaml: "ports"`
  33. //slice类型
  34. Conditions []Conditions `yaml: "conditions"`
  35. }
  36. type Conditions struct {
  37. ContainerPort string `yaml:"containerPort"`
  38. Requests Requests `yaml: "requests"`
  39. Limits Limits `yaml: "limits"`
  40. }
  41. type Requests struct {
  42. CPU string `yaml: "cpu"`
  43. MEMORY string `yaml: "memory"`
  44. }
  45. type Limits struct {
  46. CPU string `yaml: "cpu"`
  47. MEMORY string `yaml: "memory"`
  48. }
  49. func main() {
  50. var c KafkaCluster
  51. //读取yaml配置文件, 将yaml配置文件,转换struct类型
  52. conf := c.getConf()
  53. //将对象,转换成json格式
  54. data, err := json.Marshal(conf)
  55. if err != nil {
  56. fmt.Println("err:\t", err.Error())
  57. return
  58. }
  59. //最终以json格式,输出
  60. fmt.Println("data:\t", string(data))
  61. }
  62. //读取Yaml配置文件,
  63. //并转换成conf对象 struct结构
  64. func (kafkaCluster *KafkaCluster) getConf() *KafkaCluster {
  65. //应该是 绝对地址
  66. yamlFile, err := ioutil.ReadFile("E:\\Program\\go2\\goPath\\src\\xingej-go\\xingej-go\\xingej-go666\\lib\\yaml\\sparkConfig.yaml")
  67. if err != nil {
  68. fmt.Println(err.Error())
  69. }
  70. //err = yaml.Unmarshal(yamlFile, kafkaCluster)
  71. err = yaml.UnmarshalStrict(yamlFile, kafkaCluster)
  72. if err != nil {
  73. fmt.Println(err.Error())
  74. }
  75. return kafkaCluster
  76. }

2.3、运行结果:

data:    {"ApiVersion":"v1","Kind":"KafkaCluster2","Metadata":{"Name":"kafka-operator","Labels":{"config1":{"Address":"kafka-operator-labels-01","Id":"kafka-operator-labels-02","Name":"mysql-example-cluster-master","NodeName":"172.16.91.21","Role":"master"},"config2":{"Address":"kafka-operator-labels-01","Id":"kafka-operator-labels-02","Name":"mysql-example-cluster-slave","NodeName":"172.16.91.110","Role":"slave"}}},"Spec":{"Replicas":1,"Name":"kafka-controller","Image":"172.16.26.4:5000/nginx","Ports":8088,"Conditions":[{"ContainerPort":"8080","Requests":{"CPU":"0.25","MEMORY":"512Mi"},"Limits":{"CPU":"0.25","MEMORY":"1Gi"}},{"ContainerPort":"9090","Requests":{"CPU":"0.33","MEMORY":"333Mi"},"Limits":{"CPU":"0.55","MEMORY":"5Gi"}}]}}

==注意:==

yaml配置文件里,如果属性里存在大写的话,定义对应的属性时,一定不能有空格,可以参考上面例子中NodeServer里的说明  

3 例子3,读取yaml配置文件中的某一个属性

3.1、 配置文件的内容:

  1. apiVersion: v1
  2. Kind: KafkaCluster

3.2、代码如下:

  1. //测试读取yaml文件的
  2. package main
  3. import (
  4. "fmt"
  5. "github.com/kylelemons/go-gypsy/yaml"
  6. )
  7. func main() {
  8. file, err := yaml.ReadFile("E:\\Program\\go2\\goPath\\src\\xingej-go\\xingej-go\\xingej-go666\\lib\\yaml\\nginx")
  9. if err != nil {
  10. panic(err.Error())
  11. }
  12. apiVersion, error := file.Get("apiVersion")
  13. if error != nil {
  14. panic(error.Error())
  15. }
  16. fmt.Println("=apiVersion===:\t", apiVersion)
  17. }

3.3、运行结果 :

=apiVersion===:  v1 

4. 说明

  1. 例子3中用到的yaml解析包跟前面两个例子不是同一个。
  2. "gopkg.in/yaml.v2"
  3. "github.com/kylelemons/go-gypsy/yaml"
  4. 例子1,例子2 是整体读取Yaml配置文件,转换成其他格式
  5. 例子3,是读取yaml配置里的某一个属性,
  6. 因此,两者的使用场景是不一样的

转载于:https://blog.51cto.com/xingej/2115258

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/613993
推荐阅读
相关标签
  

闽ICP备14008679号