中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁(yè) > news >正文

上海專(zhuān)業(yè)網(wǎng)站建設(shè)平臺(tái)最新網(wǎng)絡(luò)推廣平臺(tái)

上海專(zhuān)業(yè)網(wǎng)站建設(shè)平臺(tái),最新網(wǎng)絡(luò)推廣平臺(tái),免費(fèi)空間訪問(wèn),太倉(cāng)有沒(méi)有做網(wǎng)站建設(shè)的本文對(duì) yaml 文件進(jìn)行解析。 下載 yaml執(zhí)行 go get github.com/spf13/viper 安裝。 golang 有很多庫(kù)可以解釋 yaml 文件。本文選用 viper 進(jìn)行解析,執(zhí)行 go get github.com/spf13/viper 安裝。 yaml語(yǔ)法規(guī)則 yaml對(duì)大小寫(xiě)敏感。yaml的層級(jí)關(guān)系只能使用空格縮進(jìn)&a…

本文對(duì) yaml 文件進(jìn)行解析。

下載

yaml執(zhí)行 go get github.com/spf13/viper 安裝。
golang 有很多庫(kù)可以解釋 yaml 文件。本文選用 viper 進(jìn)行解析,執(zhí)行 go get github.com/spf13/viper 安裝。

yaml語(yǔ)法規(guī)則

  • yaml對(duì)大小寫(xiě)敏感。
  • yaml的層級(jí)關(guān)系只能使用空格縮進(jìn),同一層縮進(jìn)的空格數(shù)量相同即可,數(shù)量不重要。不允許使用tab鍵。
  • 使用#進(jìn)行注釋,與shell一樣。

測(cè)試

yaml 配置文件

# yaml測(cè)試樣例
# null 或 NULL 為關(guān)鍵字,不能寫(xiě)# 表示 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]# 名稱(chēng)
# 字符串
name: conf file# 版本
# 如按浮點(diǎn),2.0會(huì)轉(zhuǎn)換成2
# 如按字符串,保留原樣
version: 2.0# 布爾類(lèi),轉(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é)果說(shuō)明

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類(lèi)似。

http://www.risenshineclean.com/news/42763.html

相關(guān)文章:

  • 株洲網(wǎng)站優(yōu)化網(wǎng)站制作的費(fèi)用
  • l5手機(jī)網(wǎng)站模板如何發(fā)布一個(gè)網(wǎng)站
  • 石家莊微信網(wǎng)站建設(shè)公司互聯(lián)網(wǎng)營(yíng)銷(xiāo)師考證多少錢(qián)
  • 先進(jìn)的網(wǎng)站建設(shè)百度推廣客服電話人工服務(wù)
  • 中小型企業(yè)網(wǎng)站的設(shè)計(jì)與開(kāi)發(fā)百度搜索競(jìng)價(jià)
  • 重慶網(wǎng)站公司淘寶指數(shù)網(wǎng)站
  • wordpress mb_strimwidth htmlseo優(yōu)化工具大全
  • 網(wǎng)站制作策劃今日熱點(diǎn)
  • 南寧微信網(wǎng)站制作網(wǎng)頁(yè)制作軟件推薦
  • 去哪兒網(wǎng)站開(kāi)發(fā)中國(guó)國(guó)家培訓(xùn)網(wǎng)靠譜嗎
  • 福州手機(jī)網(wǎng)站建設(shè)最新國(guó)內(nèi)新聞事件今天
  • 網(wǎng)站店鋪分布圖怎么做網(wǎng)絡(luò)營(yíng)銷(xiāo)專(zhuān)業(yè)是學(xué)什么的
  • java做的k線圖網(wǎng)站源碼下載seo搜索引擎是什么
  • 為什么做電影網(wǎng)站沒(méi)有流量嗎東莞百度seo電話
  • 做網(wǎng)站搞什么流量百度競(jìng)價(jià)點(diǎn)擊軟件奔奔
  • 網(wǎng)站是如何建立的山東做網(wǎng)站
  • 網(wǎng)站企業(yè)備案代理短視頻拍攝剪輯培訓(xùn)班
  • 溫州網(wǎng)站制作多少錢(qián)谷歌google 官網(wǎng)下載
  • 手機(jī)html5網(wǎng)站源碼廣告投放的方式有哪些
  • 深圳網(wǎng)站建設(shè)培訓(xùn)班深圳最新通告今天
  • 技術(shù)支持:淄博網(wǎng)站建設(shè)優(yōu)化設(shè)計(jì)三年級(jí)上冊(cè)語(yǔ)文答案
  • 山東省建設(shè)工程招標(biāo)中心網(wǎng)站當(dāng)日網(wǎng)站收錄查詢(xún)統(tǒng)計(jì)
  • 網(wǎng)站建設(shè)需求分析寫(xiě)什么茶葉seo網(wǎng)站推廣與優(yōu)化方案
  • 網(wǎng)站程序組成seo搜狗排名點(diǎn)擊
  • 辛集seo網(wǎng)站優(yōu)化電話靠譜的免費(fèi)建站
  • 建立手機(jī)個(gè)人網(wǎng)站營(yíng)銷(xiāo)網(wǎng)站建設(shè)制作
  • 視頻資源的網(wǎng)站怎么做站長(zhǎng)資訊
  • 網(wǎng)站建設(shè)課程設(shè)計(jì)內(nèi)容淘寶店鋪轉(zhuǎn)讓價(jià)格表
  • wordpress評(píng)論框文件采集站seo課程
  • 自己做網(wǎng)站外包百度熱搜高考大數(shù)據(jù)