Golang解析yaml格式文件

前言

Yaml是一種簡潔易懂的文件配置語言,比如其巧妙避開各種封閉符號(hào),如:引號(hào)、各種括號(hào)等,這些符號(hào)在嵌套結(jié)構(gòu)中會(huì)變得復(fù)雜而難以辨認(rèn)。對(duì)于更加具體的介紹,大家可以去自行Google一下。
本文是在基于golang第三方開源庫yaml.v2的基礎(chǔ)上進(jìn)行操作的,只是簡單介紹了一下怎樣在golang中對(duì)yaml文件進(jìn)行解析。下面是yaml.v2在github上的地址yaml.v2地址以及在godoc.org上的介紹yaml庫簡介。

正文

下面就直接使用代碼來進(jìn)行簡單的介紹了。
測(cè)試文件如下:
test.yaml

cache:
  enable : false
  list : [redis,mongoDB]
mysql:
  user : root
  password : Tech2501
  host : 10.11.22.33
  port : 3306
  name : cwi

test1.yaml

enable : false
list : [redis,mongoDB]
user : root
password : Tech2501
host : 10.11.22.33
port : 3306
name : cwi

yaml.go

package module
// Yaml struct of yaml
type Yaml struct {
    Mysql struct {
        User string `yaml:"user"`
        Host string `yaml:"host"`
        Password string `yaml:"password"`
        Port string `yaml:"port"`
        Name string `yaml:"name"`
    }
    Cache struct {
        Enable bool `yaml:"enable"`
        List []string `yaml:"list,flow"`
    }
}

// Yaml1 struct of yaml
type Yaml1 struct {
    SQLConf Mysql `yaml:"mysql"`
    CacheConf Cache `yaml:"cache"`
}

// Yaml2 struct of yaml
type Yaml2 struct {
    Mysql `yaml:"mysql,inline"`
    Cache `yaml:"cache,inline"`
}

// Mysql struct of mysql conf
type Mysql struct {
    User string `yaml:"user"`
    Host string `yaml:"host"`
    Password string `yaml:"password"`
    Port string `yaml:"port"`
    Name string `yaml:"name"`
}

// Cache struct of cache conf
type Cache struct {
    Enable bool `yaml:"enable"`
    List []string `yaml:"list,flow"`
}

main.go

package main
import (
    "io/ioutil"
    "log"
    "module"
    yaml "gopkg.in/yaml.v2"
)
func main() {
    // resultMap := make(map[string]interface{})
    conf := new(module.Yaml)
    yamlFile, err := ioutil.ReadFile("test.yaml")
   
    // conf := new(module.Yaml1)
    // yamlFile, err := ioutil.ReadFile("test.yaml")

    // conf := new(module.Yaml2)
   //  yamlFile, err := ioutil.ReadFile("test1.yaml")

    log.Println("yamlFile:", yamlFile)
    if err != nil {
        log.Printf("yamlFile.Get err #%v ", err)
    }
    err = yaml.Unmarshal(yamlFile, conf)
    // err = yaml.Unmarshal(yamlFile, &resultMap)
    if err != nil {
        log.Fatalf("Unmarshal: %v", err)
    }
    log.Println("conf", conf)
    // log.Println("conf", resultMap)
}

總結(jié)

從main.go的代碼中可以看得出,當(dāng)使用如test.yaml這種格式的yaml文件時(shí),可以使用yaml.go中的Yaml和Yaml1這兩種struct來進(jìn)行解析。當(dāng)使用類似于test1.yaml這種格式的文件時(shí),可以使用yaml.go中的Yaml2這種struct來進(jìn)行解析。
個(gè)人理解,Yaml1與Yaml2的區(qū)別在于Yaml2中在tag中加入了inline,使之變成了內(nèi)嵌類型。
在官方的簡介中對(duì)于tag中支持的flag進(jìn)行了說明,分別有flow、inline、omitempty。其中flow用于對(duì)數(shù)組進(jìn)行解析,而omitempty的作用在于當(dāng)帶有此flag變量的值為nil或者零值的時(shí)候,則在Marshal之后的結(jié)果不會(huì)帶有此變量。
當(dāng)然大家如果懶得去寫struct進(jìn)行Unmarshal時(shí),也是可以像main.go中直接聲明一個(gè)resultMap := make(map[string]interface{}) 這樣來進(jìn)行解析的。

本人第一次寫這類的文章,難免會(huì)有錯(cuò)誤,請(qǐng)大家多多指教。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容