您好,登錄后才能下訂單哦!
host: localhost:3306
user: root
pwd: 123456
dbname: test
//將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 包的路徑
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"
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
}
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里的說明
apiVersion: v1
Kind: KafkaCluster
//測試讀取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)
}
=apiVersion===: v1
例子3中用到的yaml解析包跟前面兩個例子不是同一個。
"gopkg.in/yaml.v2"
"github.com/kylelemons/go-gypsy/yaml"
例子1,例子2 是整體讀取Yaml配置文件,轉換成其他格式
例子3,是讀取yaml配置里的某一個屬性,
因此,兩者的使用場景是不一樣的
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。