Golang实践录:读取toml配置

本文对 toml 文件进行解析。

下载

对于toml格式文件,golang 有很多库可以解释 yaml 文件,如tomlviper。由于 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:00

empty = "empty"

  
# 多行
text = '''
  hello
  world!
'''

# 简单的数组
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 test

import (
	"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)

	// 读取不存在的字段,字符串为空,数值为0
	bad := 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_t

	viper.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:   hello
  world!

name: late
 lee, 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类似。

相关推荐

  1. Golang实践读取toml配置

    2023-12-07 07:26:05       56 阅读
  2. Golang实践读取ini配置文件

    2023-12-07 07:26:05       68 阅读
  3. Golang实践:sqlite的使用

    2023-12-07 07:26:05       51 阅读
  4. Golang 中 map[string]string 如何在 TOML 文件中配置

    2023-12-07 07:26:05       43 阅读
  5. 【go语言】读取toml文件

    2023-12-07 07:26:05       60 阅读
  6. Golang实践:gin框架使用自定义日志模块

    2023-12-07 07:26:05       35 阅读
  7. golang读取json文件

    2023-12-07 07:26:05       64 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-07 07:26:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-07 07:26:05       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-07 07:26:05       87 阅读
  4. Python语言-面向对象

    2023-12-07 07:26:05       96 阅读

热门阅读

  1. ElasticSearch 了解文本相似度 TF-IDF吗?

    2023-12-07 07:26:05       47 阅读
  2. Go语言语法上的一些主要特点(1)

    2023-12-07 07:26:05       51 阅读
  3. 人工智能对现代生活的影响

    2023-12-07 07:26:05       49 阅读
  4. Apache Ofbiz XML-RPC RCE漏洞复现(CVE-2023-49070)

    2023-12-07 07:26:05       49 阅读
  5. 超越节点引擎临界:华为云NES颠覆游戏规则

    2023-12-07 07:26:05       59 阅读
  6. Linux网卡命名规则

    2023-12-07 07:26:05       54 阅读
  7. 继承与派生(1)

    2023-12-07 07:26:05       55 阅读
  8. MATLAB基础应用精讲-【数模应用】深度学习杂谈

    2023-12-07 07:26:05       58 阅读
  9. Spacemesh、Kaspa和Chia的全面对比!

    2023-12-07 07:26:05       62 阅读