標籤:go yaml
1、例子11.1、yaml檔案內容如下:
host: localhost:3306user: rootpwd: 123456dbname: test
1.2、代碼如下:
//將yaml檔案,轉換成對象,再轉換成json格式輸出package mainimport ( "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: v1kind: KafkaCluster2metadata: 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: slavespec: 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 mainimport ( "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: v1Kind: KafkaCluster
3.2、代碼如下:
//測試讀取yaml檔案的package mainimport ( "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配置裡的某一個屬性, 因此,兩者的使用情境是不一樣的
Go語言之讀取yaml設定檔,轉換成struct結構,json形式輸出