网架生产企业,网站目录优化,wordpress 固定连接,政民互动平台本文对 toml 文件进行解析。 下载
对于toml格式文件#xff0c;golang 有很多库可以解释 yaml 文件#xff0c;如toml、viper。由于 viper 可解析格式较多#xff0c;本文采用该库。
toml语法规则
toml语法规则在官方中文文档上有说明#xff0c;这里直接使用。
TOML 是…本文对 toml 文件进行解析。 下载
对于toml格式文件golang 有很多库可以解释 yaml 文件如toml、viper。由于 viper 可解析格式较多本文采用该库。
toml语法规则
toml语法规则在官方中文文档上有说明这里直接使用。
TOML 是大小写敏感的。TOML 文件必须是合法的 UTF-8 编码的 Unicode 文档。空白是指制表符0x09或空格0x20。换行是指 LF0x0A或 CRLF0x0D 0x0A
测试
yaml 配置文件
# toml 测试样例
# 文件格式必须为utf8# 名称
# 字符串
name conf file# 版本
# 如按浮点2.0会转换成2
# 如按字符串保留原样
version 2.0# 布尔类转换为true或false
need true# 时间 注如果加双引号就变成字符串了
time 2020-10-03T09:21:13Z
#time 2020-10-03T09:21:13-08:00empty empty# 多行
text helloworld!
# 简单的数组
fruit [apple,apple1,apple2,apple3,apple4,apple5,]ports [8080, 8081, 8082,]# 多层级
loginfo.log.dir log## 前面是顶层表 下面是类似ini节的概念
#######节或表后必须再跟 []如果不加即使顶格也被认为在该节名下#
# \n会被识别
[my]name late \n lee#name1 late \n leeage 99# 多级数组
[multi]sta [jim kent jklatelee.org,late lee lilatelee.org,foo foolatelee.org,]# 多级对象
[mymap]
dir mymap
[[mymap.map_data]]
name 在线
attri 在线电子
url http://abc.com
[[mymap.map_data]]
name 离线
attri 离线电子
url http://ccc.com
# more该示例基本涵盖了大部分的 yaml 格式。包括字符串数值、数组、多级map。
测试代码
测试代码如下
package testimport (fmttestinggithub.com/spf13/viper
)var (cfgFile string // config.toml
)type mapUrl_t struct {Name string json:nameAttri string json:attriUrl string json:url
}func TestToml(t *testing.T) {fmt.Println(test of toml...)// 设置配置文件的2种方式if cfgFile ! {// Use config file from the flag.viper.SetConfigFile(cfgFile)} else {viper.AddConfigPath(./)viper.SetConfigName(config)viper.SetConfigType(toml)}// 读取 注如果toml格式有误此处报错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)// 字符串数组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)// 读取不存在的字段字符串为空数值为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(配置发生变更, e.Name)// })
}
测试命令
go test -v -run TestYaml测试结果
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 jklatelee.org
sta[1]: late lee lilatelee.org
sta[2]: foo foolatelee.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结果说明
1、由于使用的是viper解析除了配置文件不同外API接口及用法和解析yaml保持一致。 2、参数的值不能为 null 或 NULL但可以为nul。如果为 null解析的值为空。 3、如果字段不存在不会报错按字符串解析得到的值为空如用数值值为0。 4、对于多层级的对象可以用viper.UnmarshalKey用法与解析json、yaml类似。