上海專業(yè)網(wǎng)站建設(shè)平臺(tái)最新網(wǎng)絡(luò)推廣平臺(tái)
本文對(duì) yaml 文件進(jìn)行解析。
下載
yaml執(zhí)行 go get github.com/spf13/viper
安裝。
golang 有很多庫可以解釋 yaml 文件。本文選用 viper 進(jìn)行解析,執(zhí)行 go get github.com/spf13/viper
安裝。
yaml語法規(guī)則
- yaml對(duì)大小寫敏感。
- yaml的層級(jí)關(guān)系只能使用空格縮進(jìn),同一層縮進(jìn)的空格數(shù)量相同即可,數(shù)量不重要。不允許使用tab鍵。
- 使用
#
進(jìn)行注釋,與shell
一樣。
測(cè)試
yaml 配置文件
# yaml測(cè)試樣例
# null 或 NULL 為關(guān)鍵字,不能寫# 表示 bool 真假的幾個(gè)值
result_true: - y- Y- yes- Yes- YES- true- True- TRUE- on- On- ON# 數(shù)組的另一種形式
result_false: [n, N, no, No, NO , false, False, FALSE , off, Off, OFF]# 名稱
# 字符串
name: conf file# 版本
# 如按浮點(diǎn),2.0會(huì)轉(zhuǎn)換成2
# 如按字符串,保留原樣
version: 2.0# 布爾類,轉(zhuǎn)換為1或0
need: true# 時(shí)間
time: 2020-10-03T09:21:13empty: nul# 對(duì)象
# 加雙引號(hào)會(huì)轉(zhuǎn)義\n,即會(huì)換行
my:name: late \n leename1: "late \n lee"age: 99# 塊
text: |helloworld!# 數(shù)組
fruit:- apple- apple1- apple2- apple3- apple4- apple5# 多級(jí)數(shù)組
multi:sta:- 110 210 ddd 99- 133 135 1 2 1588 1509- 310-410- 333-444# 多層級(jí)
loginfo:log:dir: log# 多級(jí)對(duì)象
mymap:dir: "mymap"map_data:- name: "在線"attri: "在線電子"url: "http://abc.com"- name: "離線"attri: "離線電子"url: "http://ccc.com"# more
該示例基本涵蓋了大部分的 yaml 格式。包括:字符串,數(shù)值、數(shù)組、多級(jí)map。
測(cè)試代碼
測(cè)試代碼如下:
package testimport ("fmt""os""testing""github.com/spf13/viper"
)var (cfgFile string
)type mapUrl_t struct {Name string `json:"name"`Attri string `json:"attri"`Url string `json:"url"`
}func TestYaml(t *testing.T) {fmt.Println("test of yaml...")// 設(shè)置配置文件的2種方式if cfgFile != "" {// Use config file from the flag.viper.SetConfigFile(cfgFile)} else {viper.AddConfigPath("./")viper.SetConfigName("config")viper.SetConfigType("yaml")}viper.AutomaticEnv() // read in environment variables that match// 讀取err := viper.ReadInConfig()if err != nil {fmt.Println("'config.yaml' file read error:", err)os.Exit(0)}name := viper.GetString("name") // 讀取 字符串version := viper.GetString("version")need := viper.GetBool("need") // 讀取 布爾theTime := viper.GetString("time")empty := viper.GetString("empty")text := viper.GetString("text")fmt.Printf("need: %v name: %v\nversion: %v \ntime: %v \nempty: %s \ntext: %v\n", need, name, version, theTime, empty, text)// 多級(jí)讀取name = viper.GetString("my.name")name1 := viper.GetString("my.name1")age := viper.GetInt("my.age")fmt.Printf("name: %v, name1: %v age: %v \n", name, name1, age)// 字符串?dāng)?shù)組newSta := viper.GetStringSlice("multi.sta")for idx, value := range newSta {fmt.Printf("sta[%d]: %v\n", idx, value)}fruit := viper.GetStringSlice("fruit")fmt.Printf("fruit: %v\n", fruit)// 讀取不存在的字段,字符串為空,數(shù)值為0bad := viper.GetString("bad")bad1 := viper.GetInt("my.bad")fmt.Printf("bad: [%v] bad1: [%v]\n", bad, bad1)// 按數(shù)值、字符串讀取on、off等值result := viper.GetIntSlice("result_true")fmt.Printf("result true: [%v]\n", result)result1 := viper.GetStringSlice("result_true")fmt.Printf("result1 true: [%v]\n", result1)result = viper.GetIntSlice("result_false")fmt.Printf("result false: [%v]\n", result)result1 = viper.GetStringSlice("result_false")fmt.Printf("result1 false: [%v]\n", result1)logdir := viper.GetString("loginfo.log.dir")fmt.Printf("logdir: %v\n", logdir)// 多級(jí)對(duì)象// tmpMap := make([]mapUrl_t, 0, 20)var tmpMap []mapUrl_tviper.UnmarshalKey("mymap.map_data", &tmpMap)for _, item := range tmpMap {fmt.Printf("name: %v url: %v\n", item.Name, item.Url)}
}
測(cè)試命令:
go test -v -run TestYaml
測(cè)試結(jié)果:
test of yaml...
need: true name: conf file
version: 2
time: 2020-10-03T09:21:13
empty: nul
text: hello
world!name: late \n lee, name1: latelee age: 99
sta[0]: 110 210 ddd 99
sta[1]: 133 135 1 2 1588 1509
sta[2]: 310-410
sta[3]: 333-444
fruit: [apple apple1 apple2 apple3 apple4 apple5]
bad: [] bad1: [0]
result true: [[1 1 1 1 1 1 1 1 1 1 1]]
result1 true: [[true true true true true true true true true true true]]
result false: [[0 0 0 0 0 0 0 0 0 0 0]]
result1 false: [[false false false false false false false false false false false]]
logdir: log
name: 在線 url: http://abc.com
name: 離線 url: http://ccc.com
結(jié)果說明
1、name: "late \n lee"
輸出會(huì)換行。而 name: late \n lee
則會(huì)原樣輸出。
2、參數(shù)的值不能為 null 或 NULL,但可以為nul。如果為 null,解析的值為空。
3、如果字段不存在,不會(huì)報(bào)錯(cuò),按字符串解析得到的值為空,如用數(shù)值,值為0。
4、表示false
的關(guān)鍵字有n, N, no, No, NO , false, False, FALSE , off, Off, OFF
, 表示true
的有y, Y, yes, Yes, YES, true, True, TRUE, on, On, ON
。在使用時(shí)需要注意。
5、對(duì)于多層級(jí)的對(duì)象,可以用viper.UnmarshalKey
,用法與解析json類似。