怎么做送餐網(wǎng)站做網(wǎng)絡(luò)推廣的網(wǎng)站有哪些
本文對 toml 文件進(jìn)行解析。
下載
對于toml格式文件,golang 有很多庫可以解釋 yaml 文件,如toml、viper。由于 viper 可解析格式較多,本文采用該庫。
toml語法規(guī)則
toml語法規(guī)則在官方中文文檔上有說明,這里直接使用。
- TOML 是大小寫敏感的。
- TOML 文件必須是合法的 UTF-8 編碼的 Unicode 文檔。
- 空白是指制表符(0x09)或空格(0x20)。
- 換行是指 LF(0x0A)或 CRLF(0x0D 0x0A)
測試
yaml 配置文件
# toml 測試樣例
# 文件格式必須為utf8# 名稱
# 字符串
name = "conf file"# 版本
# 如按浮點(diǎn),2.0會(huì)轉(zhuǎn)換成2
# 如按字符串,保留原樣
version = "2.0"# 布爾類,轉(zhuǎn)換為true或false
need = true# 時(shí)間 注:如果加雙引號,就變成字符串了
time = 2020-10-03T09:21:13Z
#time = 2020-10-03T09:21:13-08:00empty = "empty"# 多行
text = '''helloworld!
'''# 簡單的數(shù)組
fruit = ["apple","apple1","apple2","apple3","apple4","apple5",]ports = [8080, 8081, 8082,]# 多層級
loginfo.log.dir = "log"## 前面是頂層表 下面是類似ini節(jié)的概念
#######節(jié)或表,后必須再跟 [],如果不加,即使頂格,也被認(rèn)為在該節(jié)名下#
# \n會(huì)被識別
[my]name = "late \n lee"#name1 = "late \n lee"age = 99# 多級數(shù)組
[multi]sta = ["jim kent jk@latelee.org","late lee li@latelee.org","foo foo@latelee.org",]# 多級對象
[mymap]
dir = "mymap"
[[mymap.map_data]]
name = "在線"
attri = "在線電子"
url = "http://abc.com"
[[mymap.map_data]]
name = "離線"
attri = "離線電子"
url = "http://ccc.com"
# more
該示例基本涵蓋了大部分的 yaml 格式。包括:字符串,數(shù)值、數(shù)組、多級map。
測試代碼
測試代碼如下:
package testimport ("fmt""testing""github.com/spf13/viper"
)var (cfgFile string // = "config.toml"
)type mapUrl_t struct {Name string `json:"name"`Attri string `json:"attri"`Url string `json:"url"`
}func TestToml(t *testing.T) {fmt.Println("test of toml...")// 設(shè)置配置文件的2種方式if cfgFile != "" {// Use config file from the flag.viper.SetConfigFile(cfgFile)} else {viper.AddConfigPath("./")viper.SetConfigName("config")viper.SetConfigType("toml")}// 讀取 注:如果toml格式有誤,此處報(bào)錯(cuò)err := viper.ReadInConfig()if err != nil {fmt.Printf("'%v' file read error: %v\n", cfgFile, err)return}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)// 多級讀取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)ports := viper.GetIntSlice("ports")fmt.Printf("ports: %v\n", ports)// 讀取不存在的字段,字符串為空,數(shù)值為0bad := viper.GetString("bad")bad1 := viper.GetInt("my.bad")fmt.Printf("bad: [%v] bad1: [%v]\n", bad, bad1)logdir := viper.GetString("loginfo.log.dir")fmt.Printf("logdir: %v\n", logdir)// 多級對象// 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)}// viper.WatchConfig()// viper.OnConfigChange(func(e fsnotify.Event) {// fmt.Println("配置發(fā)生變更:", e.Name)// })
}
測試命令:
go test -v -run TestYaml
測試結(jié)果:
test of toml...
need: true name: conf file
version: 2.0
time: 2020-10-03 09:21:13 +0000 UTC
empty: empty
text: helloworld!name: latelee, name1: age: 99
sta[0]: jim kent jk@latelee.org
sta[1]: late lee li@latelee.org
sta[2]: foo foo@latelee.org
fruit: [apple apple1 apple2 apple3 apple4 apple5]
ports: [8080 8081 8082]
bad: [] bad1: [0]
logdir: log
name: 在線 url: http://abc.com
name: 離線 url: http://ccc.com
結(jié)果說明
1、由于使用的是viper解析,除了配置文件不同外,API接口及用法和解析yaml保持一致。
2、參數(shù)的值不能為 null 或 NULL,但可以為nul。如果為 null,解析的值為空。
3、如果字段不存在,不會(huì)報(bào)錯(cuò),按字符串解析得到的值為空,如用數(shù)值,值為0。
4、對于多層級的對象,可以用viper.UnmarshalKey
,用法與解析json、yaml類似。