Go解析yaml文件

包准备

首先安装解析yaml文件的包

go get gopkg.in/yaml.v2

但是可能会无法安装成功,报错显示go get unrecognized import path "gopkg.in/yaml.v2"
通过下载编译源代码的方式解决:
go/src路径下新建一个文件gopkg.in,将代码下载到go/src/gopkg.in

git config  --global   http.sslVerify "false"
git config --global http.postBuffer 524288000
go env -w GOPROXY=https://goproxy.cn,direct
git clone https://github.com/go-yaml/yaml.git 

或者在https://github.com/go-yaml/yaml上下载zip,解压到该路径下。
之后将yaml的文件名称改为yaml.v2,安装:

cd yaml.v2
go install

main.go

package main
​
import (
    "encoding/json"
    "fmt"
    "gopkg.in/yaml.v2"
    "io/ioutil"
    "log"
    "os"
)//Yaml the yaml file Cluster struct
type Yaml struct {
    
    
    Clusters Clusters `yaml:"clusters"`
}
type Clusters struct {
    
    
    Services []Services `yaml:"services"`
}
type Services struct {
    
    
    Name       string `yaml:"service_name"`
    Ip         string `yaml:"ipaddr"`
    Username   string `yaml:"username"`
    Passwd     string `yaml:"passwd"`
    Ports      []Ports  `yaml:"ports,omitempty"`
}
type  Ports    struct{
    
    
Port      string `yaml:"port"`
Protocol  string `yaml:"protocol"`
}func ReadYamlConfig(path string)  (*Yaml,error){
    
    
    conf := &Yaml{
    
    }
    if f, err := os.Open(path); err != nil {
    
    
        return nil,err
    } else {
    
    
        yaml.NewDecoder(f).Decode(conf)
    }
    return  conf,nil
}//test yamlfunc main() {
    
    
    conf,err := ReadYamlConfig("cluster.yaml")if err != nil {
    
    
        log.Fatal(err)
    }
    byts,err := json.Marshal(conf)
    if err != nil {
    
    
        log.Fatal(err)
    }
    fmt.Println(string(byts))
​
    cyts,err := yaml.Marshal(conf)
    if err != nil {
    
    
        log.Fatal(err)
    }
​
    t := Yaml{
    
    }
    err = yaml.Unmarshal([]byte(cyts), &t)
    if err != nil {
    
    
        log.Fatal(err)
    }
    fmt.Println(t)
    //
    cluster := new(Yaml)
    yamlFile, err := ioutil.ReadFile("cluster.yaml")
    if err != nil {
    
    
        log.Println(err)
    }
​
    err = yaml.Unmarshal(yamlFile, &cluster)if err != nil {
    
    
        log.Println(err)
    }
    fmt.Println(cluster)//log.Println("conf", cluster)
    fmt.Println(cluster.Clusters.Services[1].Ports[0].Port)
}

cluster.yaml

clusters:
  services:
    - service_name: test1
      ipaddr: 192.168.1.12
      username: root
      passwd: zzs
    - service_name: test2
      ipaddr: 192.168.1.13
      username: root
      passwd: eve
      ports:
        - port: 48888
          protocol: TCP

output

{
    
    "Clusters":{
    
    "Services":[{
    
    "Name":"test1","Ip":"192.168.1.12","Username":"root","Passwd":"zzs","Ports":null},{
    
    "Name":"test2","Ip":"192.168.1.13","Username":"root","Passwd":"eve","Ports":[{
    
    "Port":"48888","Protocol":"TCP"}]}]}}
{
    
    {
    
    [{
    
    test1 192.168.1.12 root zzs []} {
    
    test2 192.168.1.13 root eve [{
    
    48888 TCP}]}]}}
&{
    
    {
    
    [{
    
    test1 192.168.1.12 root zzs []} {
    
    test2 192.168.1.13 root eve [{
    
    48888 TCP}]}]}}
48888

猜你喜欢

转载自blog.csdn.net/Maestro_T/article/details/108780042