亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Go語言之讀取yaml配置文件,轉換成struct結構,json形式輸出

發布時間:2020-06-26 01:04:41 來源:網絡 閱讀:15153 作者:故新 欄目:編程語言

1、例子1

1.1、yaml文件內容如下:

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

1.2、代碼如下:

//將yaml文件,轉換成對象,再轉換成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)

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

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

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

//讀取Yaml配置文件,
//并轉換成conf對象
func (c *conf) getConf() *conf {
    //應該是 絕對地址
    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
}

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

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

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

2.1、 配置文件內容:

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" 格式是錯誤的,中間多了一個空格,不能識別的
    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配置文件,轉換struct類型
    conf := c.getConf()

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

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

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

//讀取Yaml配置文件,
//并轉換成conf對象  struct結構
func (kafkaCluster *KafkaCluster) getConf() *KafkaCluster {
    //應該是 絕對地址
    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、運行結果:

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、 配置文件的內容:

apiVersion: v1
Kind: KafkaCluster

3.2、代碼如下:

//測試讀取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、運行結果 :

=apiVersion===:  v1 

4. 說明

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

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

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

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

因此,兩者的使用場景是不一樣的
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

来安县| 鄂托克旗| 玉田县| 彩票| 鄯善县| 文昌市| 虎林市| 札达县| 开封县| 横山县| 平顺县| 都昌县| 琼海市| 钦州市| 贡山| 渭源县| 江城| 青铜峡市| 哈巴河县| 仙居县| 苏尼特左旗| 建宁县| 朝阳市| 桐城市| 鄯善县| 桓台县| 娱乐| 宣恩县| 奉化市| 贺兰县| 巫山县| 龙岩市| 定日县| 通河县| 三台县| 涞源县| 濮阳县| 甘洛县| 阿拉善右旗| 惠水县| 泾川县|