這篇文章將為大家詳細(xì)講解有關(guān)利用golang怎么對yaml文件進(jìn)行解析,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
創(chuàng)新互聯(lián)建站專注于綏中企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,商城開發(fā)。綏中網(wǎng)站建設(shè)公司,為綏中等地區(qū)提供建站服務(wù)。全流程按需求定制制作,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)首先安裝解析的第三方包:
go get gopkg.in/yaml.v2
示例:
package main import ( "os" "log" "fmt" "encoding/json" "gopkg.in/yaml.v2" ) type Config struct { Test Test `yaml:"test"` } type Test struct { User []string `yaml:"user"` MQTT MQ `yaml:"mqtt"` Http HTTP `yaml:"http"` } type HTTP struct { Port string `yaml:"port"` Host string `yaml:"host"` } type MQ struct { Host string `yaml:"host"` Username string `yaml:"username"` Password string `yaml:"password"` } //read yaml config //注:path為yaml或yml文件的路徑 func ReadYamlConfig(path string) (*Config,error){ conf := &Config{} if f, err := os.Open(path); err != nil { return nil,err } else { yaml.NewDecoder(f).Decode(conf) } return conf,nil } //test yaml func main() { conf,err := ReadYamlConfig("D:/test_yaml/test.yaml") if err != nil { log.Fatal(err) } byts,err := json.Marshal(conf) if err != nil { log.Fatal(err) } fmt.Println(string(byts)) }
test.yaml內(nèi)容如下:
test: user: - Tom - Lily - Skay mqtt: host: localhost:1883 username: test password: test http: {port: "8080", host: "127.0.0.1"}
運行結(jié)果:
{"Test":{"User":["Tom","Lily","Skay"],"MQTT":{"Host":"localhost:1883","Username":"test","Password":"test"},"Http":{"Port":"8080","Host":"127.0.0.1"}}}
補充:golang 讀取yml格式,多結(jié)構(gòu)體級聯(lián)
進(jìn)入到gopath下執(zhí)行命令
go get gopkg.in/yaml.v2
源碼地址https://github.com/go-yaml/yaml
ipport: 192.168.2.95:10000 startsendtime: 2017-01-02 08:08:08 sendmaxcountperday: 100 devices: - devid: 123456789 nodes: - pkid: 0 bkid: 0 index: 0 minvalue: 0 maxvalue: 60 datatype: normal - pkid: 0 bkid: 0 index: 0 datatype: boolean - devid: 10001 nodes: - pkid: 0 bkid: 1 index: 0 datatype: boolean warnfrequency: 10 sendfrequency: 10
package main import ( "fmt" "gopkg.in/yaml.v2" "io/ioutil" ) //配置文件中字母要小寫,結(jié)構(gòu)體屬性首字母要大寫 type Myconf struct { Ipport string StartSendTime string SendMaxCountPerDay int Devices []Device WarnFrequency int SendFrequency int } type Device struct { DevId string Nodes []Node } type Node struct { PkId string BkId string Index string MinValue float32 MaxValue float32 DataType string } func main() { data, _ := ioutil.ReadFile("config.yml") fmt.Println(string(data)) t := Myconf{} //把yaml形式的字符串解析成struct類型 yaml.Unmarshal(data, &t) fmt.Println("初始數(shù)據(jù)", t) if(t.Ipport==""){ fmt.Println("配置文件設(shè)置錯誤") return; } d, _ := yaml.Marshal(&t) fmt.Println("看看 :", string(d)) }
1.配置文件中字母要小寫,結(jié)構(gòu)體屬性首字母要大寫,開發(fā)比較快
也可以指定如:yaml:"c",只不過有點麻煩,當(dāng)然如果重命名必須要指定
2.yaml:",flow"
這個意思是將數(shù)組用[“a”,”b”]這樣的格式展示,默認(rèn)展示形式是
- a
- b
關(guān)于利用golang怎么對yaml文件進(jìn)行解析就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
分享名稱:利用golang怎么對yaml文件進(jìn)行解析-創(chuàng)新互聯(lián)
文章來源:http://muchs.cn/article4/dhioie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、品牌網(wǎng)站設(shè)計、定制網(wǎng)站、Google、網(wǎng)頁設(shè)計公司、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容