Go 使用toml配置文件

我们常见的一些配置文件包括,yaml、xml、toml、json和ini。

今天我们就来介绍这个toml配置文件,在go语言中的使用。
toml(Tom’s Obvious, Minimal Language),简约又明显的语言。

使用的包是:github.com/BurntSushi/toml

安装 toml 包

1
go get github.com/BurntSushi/toml@latest

创建 toml 配置文件

首先,创建一个 TOML 配置文件,例如 config.toml,并在其中存储你的配置信息。以下是一个简单的 TOML 配置文件示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# This is a TOML document. Boom.

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
organization = "ABC"
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],[true, false] ] # just an update to make sure parsers support it

# Line breaks are OK when inside arrays
hosts = [
"alpha",
"omega"
]

读取 toml 配置文件

更复杂的示例:https://github.com/BurntSushi/toml/tree/master/_example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main

import (
"fmt"
"time"

"github.com/BurntSushi/toml"
)

type tomlConfig struct {
Title string
Owner ownerInfo
DB database `toml:"database"`
Servers map[string]server
Clients clients
}

type ownerInfo struct {
Name string `toml:"name"`
Org string `toml:"organization"`
Bio string `toml:"bio"`
DOB time.Time `toml:"bob"`
}

type database struct {
Server string `toml:"server"`
Ports []int `toml:"ports"`
CommMax int64 `toml:"connection_max"`
Enabled bool `toml:"enabled"`
}

type server struct {
IP string `toml:"ip"`
DC string `toml:"dc"`
}

type clients struct {
Data [][]interface{} `toml:"data"`
Hosts []string `toml:"hosts"`
}

func readToml() {
var config tomlConfig
if _, err := toml.DecodeFile("conf/config.toml", &config); err != nil {
fmt.Println(err)
return
}

fmt.Printf("Title: %s\n", config.Title)
fmt.Println()

fmt.Printf("Owner: %s (%s, %s), Born: %s\n",
config.Owner.Name, config.Owner.Org, config.Owner.Bio, config.Owner.DOB)
fmt.Println()

fmt.Printf("Database: %s %v (Max conn, %d), Enable? %v\n",
config.DB.Server, config.DB.Ports, config.DB.CommMax, config.DB.Enabled)
fmt.Println()

for serverName, server := range config.Servers {
fmt.Printf("Server: %s (%s, %s)\n", serverName, server.IP, server.DC)
}
fmt.Println()

fmt.Printf("Client data: %v\n", config.Clients.Data)
fmt.Printf("Client hosts: %v\n", config.Clients.Hosts)
}

func main() {
readToml()
}

输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
Title: TOML Example

Owner: Tom Preston-Werner (ABC, GitHub Cofounder & CEO
Likes tater tots and beer.), Born: 0001-01-01 00:00:00 +0000 UTC

Database: 192.168.1.1 [8001 8001 8002] (Max conn, 5000), Enable? true

Server: alpha (10.0.0.1, eqdc10)
Server: beta (10.0.0.2, eqdc10)

Client data: [[gamma delta] [1 2] [true false]]
Client hosts: [alpha omega]
-------------本文结束感谢您的阅读-------------