真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Go語(yǔ)言之讀取yaml配置文件,轉(zhuǎn)換成struct結(jié)構(gòu),json形式輸出

1、例子1

1.1、yaml文件內(nèi)容如下:

host: localhost:3306
user: root
pwd: 123456
dbname: test  

1.2、代碼如下:

//將yaml文件,轉(zhuǎn)換成對(duì)象,再轉(zhuǎn)換成json格式輸出
package main

import (
    "encoding/json"
    "fmt"
    "gopkg.in/yaml.v2"
    "io/ioutil"
)

//定義conf類型
//類型里的屬性,全是配置文件里的屬性
type conf struct {
    Host   string `yaml: "host"`
    User   string `yaml:"user"`
    Pwd    string `yaml:"pwd"`
    Dbname string `yaml:"dbname"`
}

func main() {
    var c conf
    //讀取yaml配置文件
    conf := c.getConf()
    fmt.Println(conf)

    //將對(duì)象,轉(zhuǎn)換成json格式
    data, err := json.Marshal(conf)

    if err != nil {
        fmt.Println("err:\t", err.Error())
        return
    }

    //最終以json格式,輸出
    fmt.Println("data:\t", string(data))
}

//讀取Yaml配置文件,
//并轉(zhuǎn)換成conf對(duì)象
func (c *conf) getConf() *conf {
    //應(yīng)該是 絕對(duì)地址
    yamlFile, err := ioutil.ReadFile("E:\\Program\\go2\\goPath\\src\\xingej-go\\xingej-go\\xingej-go666\\lib\\yaml\\conf.yaml")
    if err != nil {
        fmt.Println(err.Error())
    }

    err = yaml.Unmarshal(yamlFile, c)

    if err != nil {
        fmt.Println(err.Error())
    }

    return c
}

如果某一個(gè)包,本地沒有的話,可以在cmd中使用下面的命令,進(jìn)行下載,如:
go get gopkg.in/yaml.v2

目前成都創(chuàng)新互聯(lián)已為上1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁(yè)空間、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、天河網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

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

2、例子2,該配置文件中,存在map,slice類型,稍微復(fù)雜些

2.1、 配置文件內(nèi)容:

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"

2.2、 代碼如下:

package main

import (
    "encoding/json"
    "fmt"
    "gopkg.in/yaml.v2"
    "io/ioutil"
)

type KafkaCluster struct {
    ApiVersion string   `yaml:"apiVersion"`
    Kind       string   `yaml: "kind"`
    Metadata   Metadata `yaml: "metadata"`
    Spec       Spec     `yaml: "spec"`
}

type Metadata struct {
    Name string `yaml:"name"`
    //map類型
    Labels map[string]*NodeServer `yaml:"labels"`
}

type NodeServer struct {
    Address string `yaml: "address"`
    Id      string `yaml: "id"`
    Name    string `yaml: "name"`
    //注意,屬性里,如果有大寫的話,tag里不能存在空格
    //如yaml: "nodeName" 格式是錯(cuò)誤的,中間多了一個(gè)空格,不能識(shí)別的
    NodeName string `yaml:"nodeName"`
    Role     string `yaml: "role"`
}

type Spec struct {
    Replicas int    `yaml: "replicas"`
    Name     string `yaml: "name"`
    Image    string `yaml: "iamge"`
    Ports    int    `yaml: "ports"`
    //slice類型
    Conditions []Conditions `yaml: "conditions"`
}

type Conditions struct {
    ContainerPort string   `yaml:"containerPort"`
    Requests      Requests `yaml: "requests"`
    Limits        Limits   `yaml: "limits"`
}

type Requests struct {
    CPU    string `yaml: "cpu"`
    MEMORY string `yaml: "memory"`
}

type Limits struct {
    CPU    string `yaml: "cpu"`
    MEMORY string `yaml: "memory"`
}

func main() {
    var c KafkaCluster
    //讀取yaml配置文件, 將yaml配置文件,轉(zhuǎn)換struct類型
    conf := c.getConf()

    //將對(duì)象,轉(zhuǎn)換成json格式
    data, err := json.Marshal(conf)

    if err != nil {
        fmt.Println("err:\t", err.Error())
        return
    }

    //最終以json格式,輸出
    fmt.Println("data:\t", string(data))
}

//讀取Yaml配置文件,
//并轉(zhuǎn)換成conf對(duì)象  struct結(jié)構(gòu)
func (kafkaCluster *KafkaCluster) getConf() *KafkaCluster {
    //應(yīng)該是 絕對(duì)地址
    yamlFile, err := ioutil.ReadFile("E:\\Program\\go2\\goPath\\src\\xingej-go\\xingej-go\\xingej-go666\\lib\\yaml\\sparkConfig.yaml")
    if err != nil {
        fmt.Println(err.Error())
    }

    //err = yaml.Unmarshal(yamlFile, kafkaCluster)
    err = yaml.UnmarshalStrict(yamlFile, kafkaCluster)

    if err != nil {
        fmt.Println(err.Error())
    }

    return kafkaCluster
}

2.3、運(yùn)行結(jié)果:

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配置文件里,如果屬性里存在大寫的話,定義對(duì)應(yīng)的屬性時(shí),一定不能有空格,可以參考上面例子中NodeServer里的說明  

3 例子3,讀取yaml配置文件中的某一個(gè)屬性

3.1、 配置文件的內(nèi)容:

apiVersion: v1
Kind: KafkaCluster

3.2、代碼如下:

//測(cè)試讀取yaml文件的
package main

import (
    "fmt"
    "github.com/kylelemons/go-gypsy/yaml"
)

func main() {
    file, err := yaml.ReadFile("E:\\Program\\go2\\goPath\\src\\xingej-go\\xingej-go\\xingej-go666\\lib\\yaml\\nginx")

    if err != nil {
        panic(err.Error())
    }

    apiVersion, error := file.Get("apiVersion")
    if error != nil {
        panic(error.Error())
    }

    fmt.Println("=apiVersion===:\t", apiVersion)

}

3.3、運(yùn)行結(jié)果 :

=apiVersion===:  v1 

4. 說明

例子3中用到的yaml解析包跟前面兩個(gè)例子不是同一個(gè)。  

"gopkg.in/yaml.v2"
"github.com/kylelemons/go-gypsy/yaml"  

例子1,例子2 是整體讀取Yaml配置文件,轉(zhuǎn)換成其他格式  

例子3,是讀取yaml配置里的某一個(gè)屬性,  

因此,兩者的使用場(chǎng)景是不一樣的

網(wǎng)站欄目:Go語(yǔ)言之讀取yaml配置文件,轉(zhuǎn)換成struct結(jié)構(gòu),json形式輸出
標(biāo)題URL:http://weahome.cn/article/ipepso.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部