当前位置: 首页 > news >正文

Golang实践录:读取toml配置

本文对 toml 文件进行解析。

下载

对于toml格式文件,golang 有很多库可以解释 yaml 文件,如toml、viper。由于 viper 可解析格式较多,本文采用该库。

toml语法规则

toml语法规则在官方中文文档上有说明,这里直接使用。

  • TOML 是大小写敏感的。
  • TOML 文件必须是合法的 UTF-8 编码的 Unicode 文档。
  • 空白是指制表符(0x09)或空格(0x20)。
  • 换行是指 LF(0x0A)或 CRLF(0x0D 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 lee"age = 99# 多级数组
[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 格式。包括:字符串,数值、数组、多级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...")// 设置配置文件的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 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

结果说明

1、由于使用的是viper解析,除了配置文件不同外,API接口及用法和解析yaml保持一致。
2、参数的值不能为 null 或 NULL,但可以为nul。如果为 null,解析的值为空。
3、如果字段不存在,不会报错,按字符串解析得到的值为空,如用数值,值为0。
4、对于多层级的对象,可以用viper.UnmarshalKey,用法与解析json、yaml类似。

http://www.lryc.cn/news/254456.html

相关文章:

  • 超大规模集成电路设计----基于阵列的可编程逻辑(七)
  • 深入探索FastAPI单元测试:使用TestClient轻松测试你的API
  • 基于ssm小型企业办公自动化系统论文
  • CasADi - 最优控制开源 Python/MATLAB 库
  • Java中使用String字符串的注意事项
  • 离线数仓构建案例一
  • nginx优雅如何优雅的接管【跨域配置】
  • 远离危险的购买手机的渠道
  • 外包干了2个多月,技术明显有退步了。。。。。
  • 【Java项目管理工具】Maven
  • solidity案例详解(六)服务评价合约
  • 使用kubeadm搭建高可用的K8s集群
  • C#图像处理OpenCV开发指南(CVStar,07)——通用滤波(Filter2D)的实例代码
  • c++函数模板STL详解
  • Java利用UDP实现简单群聊
  • fastapi.templating与HTMLResponse
  • 当初为什么选择计算机这类的行业?
  • tif文件转png、Excel
  • 【PyTorch】训练过程可视化
  • 深入理解Go语言GC机制
  • qt-C++笔记之组件-分组框QGroupBox
  • qt 定时器用法
  • 用23种设计模式打造一个cocos creator的游戏框架----(九)访问者模式
  • 根文件系统初步测试
  • 【精选】设计模式——策略设计模式-两种举例说明,具体代码实现
  • 外包干了3个月,技术倒退2年。。。
  • 微信小程序:chooseimage从本地相册选择图片或使用相机拍照
  • 「Swift」取消UITableView起始位置在状态栏下方开始
  • android高版本适配使用Tools.java
  • 面试官:说说webpack中常见的Loader?解决了什么问题?