Golang 的 TOML库
TOML 的全称是 Tom's Obvious, Minimal Language,因为它的作者是 GitHub 联合创始人 Tom Preston-Werner。
TOML 的目标是成为一个极简的配置文件格式。TOML 被设计成可以无歧义地被映射为哈希表,从而被多种语言解析。
TOML 的Spec https://github.com/toml-lang/toml
中文版: http://segmentfault.com/a/1190000000477752
Golang的解析库很多:
- Go (@thompelletier) - https://github.com/pelletier/go-toml
- Go (@laurent22) - https://github.com/laurent22/toml-go
- Go w/ Reflection (@BurntSushi) - https://github.com/BurntSushi/toml
- Go (@achun) - https://github.com/achun/tom-toml
- Go (@naoina) - https://github.com/naoina/toml
对比后,推荐:https://github.com/BurntSushi/toml
更新是16天前的,而且支持把配置文件反序列化成类对象,把类对象序列号成配置文件。而且关注的人也多。
下面是 https://github.com/BurntSushi/toml 的例子翻译:
比如下面的配置文件:
Age = 25
Cats = [ "Cauchy", "Plato" ]
Pi = 3.14
Perfection = [ 6, 28, 496, 8128 ]
DOB = 1987-07-05T05:45:00Z
对应的Go类如下:
type Config struct {
Age int
Cats []string
Pi float64
Perfection []int
DOB time.Time // requires `import time`
}
读取配置文件的代码如下:
var conf Config
if _, err := toml.Decode(tomlData, &conf); err != nil {
// handle error
}
如果我们配置文件中字段名和类的名称无法映射,则可以使用 struct tags
比如,配置文件为:
some_key_NAME = "wat"
对应的Go类如下:
type TOML struct {
ObscureKey string `toml:"some_key_NAME"`
}
下面是一个例子,自动把duration 字符串解析成 time.Duration 对象
配置文件:
[[song]]
name = "Thunder Road"
duration = "4m49s" [[song]]
name = "Stairway to Heaven"
duration = "8m03s"
对应Go类如下:
type song struct {
Name string
Duration duration
}
type songs struct {
Song []song
}
var favorites songs
if _, err := toml.Decode(blob, &favorites); err != nil {
log.Fatal(err)
} for _, s := range favorites.Song {
fmt.Printf("%s (%s)\n", s.Name, s.Duration)
}
这里我们需要 让duration 类型满足 encoding.TextUnmarshaler
接口
type duration struct {
time.Duration
} func (d *duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
对于TOML官方的例子处理如下:
配置文件:
# This is a TOML document. Boom. title = "TOML Example" [owner]
name = "Tom Preston-Werner"
organization = "GitHub"
bio = "GitHub Cofounder & CEO\nLikes tater tots and beer."
dob = 1979-05-27T07:32:00Z # First class dates? Why not? [database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true [servers] # You can indent as you please. Tabs or spaces. TOML don't care.
[servers.alpha]
ip = "10.0.0.1"
dc = "eqdc10" [servers.beta]
ip = "10.0.0.2"
dc = "eqdc10" [clients]
data = [ ["gamma", "delta"], [1, 2] ] # just an update to make sure parsers support it # Line breaks are OK when inside arrays
hosts = [
"alpha",
"omega"
]
对应的Go类如下:
type tomlConfig struct {
Title string
Owner ownerInfo
DB database `toml:"database"`
Servers map[string]server
Clients clients
} type ownerInfo struct {
Name string
Org string `toml:"organization"`
Bio string
DOB time.Time
} type database struct {
Server string
Ports []int
ConnMax int `toml:"connection_max"`
Enabled bool
} type server struct {
IP string
DC string
} type clients struct {
Data [][]interface{}
Hosts []string
}
注意,这里是不区分大小写的匹配。
Golang 的 TOML库的更多相关文章
- Golang实现requests库
Golang实现requests库 简单的封装下,方便使用,像python的requests库一样. Github地址 Github 支持 GET.POST.PUT.DELETE applicatio ...
- 『Golang』—— 标准库之 os
Golang 的 os 库基本承袭 Unix 下 C 语言的用法 path 库: func Base(path string) string //取文件名,不含目录部分 func Dir(path s ...
- Golang编写动态库实现回调函数
Golang编写动态库实现回调函数 我们现在要做一个动态库,但是C++实在是比较难,于是就想能不能用更简单的golang来实现,golang也就是最近的版本才支持编译成动态库,在网上也没找到可用的案例 ...
- Golang学习--TOML配置处理
上一篇文章中我们学会了使用包管理工具,这样我们就可以很方便的使用包管理工具来管理我们依赖的包. 配置工具的选择 但我们又遇到了一个问题,一个项目通常是有很多配置的,比如PHP的php.ini文件.Ng ...
- golang命令行库cobra的使用
简介 Cobra既是一个用来创建强大的现代CLI命令行的golang库,也是一个生成程序应用和命令行文件的程序.下面是Cobra使用的一个演示: Cobra提供的功能 简易的子命令行模式,如 app ...
- golang使用simplejson库解析复杂json
cnblogs原创 golang自带的json解析库encoding/json提供了json字符串到json对象的相互转换,在json字符串比较简单的情况下还是挺好用的,但是当json字符串比较复杂或 ...
- golang调用动态库
测试动态库 test_so.h int test_so_func(int a,int b); test_so.c #include "test_so.h" int test_so_ ...
- window下golang生成静态库给C语言调用
buidmod为c-archive能在window下生成 ,c-shared则不行 1.golang生成c-archive静态库 main.go package main import "C ...
- gohook 一个支持运行时替换 golang 函数的库实现
运行时替换函数对 golang 这类静态语言来说并不是件容易的事情,语言层面的不支持导致只能从机器码层面做些奇怪 hack,往往艰难,但如能成功,那挣脱牢笼带来的成就感,想想就让人兴奋. gohook ...
随机推荐
- sql指定值排在前面
示例: SELECT COL1 FROM TABLE1 ORDER BY CASE WHEN COL1 = 'A' THEN 1 ELSE 2 END,COL1;//COL1为A的排在最前面,剩下的按 ...
- Windows下使用DOS命令进入MySQL数据库
先要配置环境变量 MYSQL_HOME : D:\mysql-8.0.11-winx64 Path:%MYSQL_HOME%\bin 1)新建MYSQL_HOME变量,并配置:C:\Program F ...
- QTREE5 - Query on a tree V(LCT)
题意翻译 你被给定一棵n个点的树,点从1到n编号.每个点可能有两种颜色:黑或白.我们定义dist(a,b)为点a至点b路径上的边个数. 一开始所有的点都是黑色的. 要求作以下操作: 0 i 将点i的颜 ...
- [原创] Shell 参数传递 与 默认值
目录 简介 基本传参 $* 与 $@ 区别 默认参数(变量默认值) if 繁琐方式 - 变量为null = 变量为null时, 同时改变变量值 :- 变量为null 或 空字符串 := 变量为null ...
- 04day->python列表和元祖
一.列表 1.索引.切片 索引:根据索引值获取,里表里的值 切片:和字符串相似 2.增 1)append(object),在列表的末端添加 2)insert(index ...
- Log中关于start meeting在zVideoApp和zVideoUI中的流程可以搜索的几个字符串
[31356:36164:04-29/17:53:38.164:INFO:SBConfUI.cpp(940)] CSBConfUI::OnConfStatusChanged(CONF_STATUS) ...
- Python开发转盘小游戏
Python开发转盘小游戏 Python 一 原理分析 Python开发一个图形界面 有12个选项和2个功能键 确定每个按钮的位置 每个按钮的间隔相同 点击开始时转动,当前选项的背景颜色为红色,其他 ...
- bzoj 3261 最大异或和 可持久化字典树(01树)
题目传送门 思路: 由异或的性质可得,题目要求的式子可以转化成求$max(pre[n]^x^pre[i])$,$pre[i]$表示前缀异或和,那么我们现在就要求出这个东西,所以用可持久化字典树来求,每 ...
- HTML基础信息笔记
HTML 是什么 HTML 指的是超文本标记语言 (Hyper Text Markup Language) HTML 标签(tag) HTML 标签是由尖括号包围的关键词,比如 <html> ...
- Linux 构建ftp服务器
1.安装vsftpd服务器 $sudo apt-get install vsftpd 2.cd 到etc文件,配置vsftpd.conf文件 $sudo vi /etc/vsftpd.conf 修改至 ...