go语言配置文件解析库的使用TOML parser and encoder for Go with reflection
来源:互联网 发布:腾讯问卷怎么导出数据 编辑:程序博客网 时间:2024/06/07 03:45
使用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 = 5000enabled = 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 arrayshosts = [ "alpha", "omega"]
package mainimport ( "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)}
data := make(map[string]interface{}) toml.DecodeFile("example.toml", &data) fmt.Println(len(data)) for key, _ := range data { fmt.Println(key) }
package mainimport ( "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)}
5serversmap[alpha:map[ip:10.0.0.1 dc:eqdc10] beta:map[ip:10.0.0.2 dc:eqdc10]]clientsmap[data:[[gamma delta] [1 2]] hosts:[alpha omega]]titleTOML Exampleownermap[name:Tom Preston-Werner organization:GitHub bio:GitHub Cofounder & CEOLikes tater tots and beer. dob:1979-05-27 07:32:00 +0000 UTC]databasemap[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]
阅读全文
1 0
- go语言配置文件解析库的使用TOML parser and encoder for Go with reflection
- go语言学习之----读取配置文件的方式toml
- GO语言的reflection
- Go实战--也许最快的Go语言Web框架kataras/iris初识二(TOML、Cache、Cookie)
- go语言中yaml配置文件的使用
- Go语言学习笔记 --- reflection
- go语言 TensorFlow for Go
- go语言解析json库
- Go语言标准库Json的使用.
- 【Go】Go语言中反射包的实现原理(The Laws of Reflection)
- Go语言反射的使用
- go语言:flag的使用
- Go语言defer的使用
- go语言的使用感受
- Go语言 for
- Go语言 bufio库 使用
- go语言解析xml
- Go语言解析JSON
- Linux下Memcached的安装 开机自启动
- 进程收不到网络数据,但 tcpdump 能抓到包
- Android之framework修改底部导航栏NavigationBar动态显示和隐藏
- STM32DMA使用详解
- Errors running builder 'DeploymentBuilder' on project '工程名'
- go语言配置文件解析库的使用TOML parser and encoder for Go with reflection
- jar包和war包的区别
- request post 模拟登录 csdn 获取保存cookie
- C++一面
- 编译器相关
- 特殊回文数-Java
- java 新IO
- git如何恢复stash save后被reset hard清空的代码
- 编写mysql函数 随机生成中文姓名