0
点赞
收藏
分享

微信扫一扫

go语言配置文件解析库的使用TOML parser and encoder for Go with reflection



使用go get命令下载 go get github.com/BurntSushi/toml 会下载到你配置gopath/src目录下

TOML 的目标是成为一个极简的配置文件格式。TOML 被设计成可以无歧义地被映射为哈希表,从而被多种语言解析。

go语言中读取配置文件的


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"
]


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
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
}

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

fmt.Printf("Title: %s\n", config.Title)
fmt.Printf("Owner: %s (%s, %s), Born: %s\n",
config.Owner.Name, config.Owner.Org, config.Owner.Bio,
config.Owner.DOB)
fmt.Printf("Database: %s %v (Max conn. %d), Enabled? %v\n",
config.DB.Server, config.DB.Ports, config.DB.ConnMax,
config.DB.Enabled)
for serverName, server := range config.Servers {
fmt.Printf("Server: %s (%s, %s)\n", serverName, server.IP, server.DC)
}
fmt.Printf("Client data: %v\n", config.Clients.Data)
fmt.Printf("Client hosts: %v\n", config.Clients.Hosts)
}

go语言配置文件解析库的使用TOML parser and encoder for Go with reflection_ide

data := make(map[string]interface{})
toml.DecodeFile("example.toml", &data)
fmt.Println(len(data))

for key, _ := range data {
fmt.Println(key)
}


go语言配置文件解析库的使用TOML parser and encoder for Go with reflection_ide_02



package main

import (
"fmt"
"time"
//"strings"

"github.com/BurntSushi/toml"
)

func main() {

data := make(map[string]interface{})
toml.DecodeFile("example.toml", &data)
fmt.Println(len(data))

for key, val := range data {
fmt.Println(key)
fmt.Println(val)
}

port := data["database"]
fmt.Println(port)

}

5
servers
map[alpha:map[ip:10.0.0.1 dc:eqdc10] beta:map[ip:10.0.0.2 dc:eqdc10]]
clients
map[data:[[gamma delta] [1 2]] hosts:[alpha omega]]
title
TOML Example
owner
map[name:Tom Preston-Werner organization:GitHub bio:GitHub Cofounder & CEO
Likes tater tots and beer. dob:1979-05-27 07:32:00 +0000 UTC]
database
map[server:192.168.1.1 ports:[8001 8001 8002] connection_max:5000 enabled:true]
map[ports:[8001 8001 8002] connection_max:5000 enabled:true server:192.168.1.1]







举报

相关推荐

0 条评论