[英](Go) How to use toml files?
As title, I want to know how to use toml files from golang.作为标题,我想知道如何使用 golang 中的 toml 文件。
Before that, I show my toml examples.在此之前,我展示了我的 toml 示例。 Is it right?
这样对吗?
[datatitle]
enable = true
userids = [
"12345", "67890"
]
[datatitle.12345]
prop1 = 30
prop2 = 10
[datatitle.67890]
prop1 = 30
prop2 = 10
And then, I want to set these data as type of struct.然后,我想将这些数据设置为结构类型。
As a result I want to access child element as below.因此,我想访问子元素,如下所示。
datatitle["12345"].prop1
datatitle["67890"].prop2
Thanks in advance!提前致谢!
First get BurntSushi's toml parser:首先获取BurntSushi的toml解析器:
go get github.com/BurntSushi/toml
BurntSushi parses toml and maps it to structs, which is what you want. BurntSushi 解析 toml 并将其映射到结构体,这就是您想要的。
Then execute the following example and learn from it:然后执行下面的例子并从中学习:
package main
import (
"github.com/BurntSushi/toml"
"log"
)
var tomlData = `title = "config"
[feature1]
enable = true
userids = [
"12345", "67890"
]
[feature2]
enable = false`
type feature1 struct {
Enable bool
Userids []string
}
type feature2 struct {
Enable bool
}
type tomlConfig struct {
Title string
F1 feature1 `toml:"feature1"`
F2 feature2 `toml:"feature2"`
}
func main() {
var conf tomlConfig
if _, err := toml.Decode(tomlData, &conf); err != nil {
log.Fatal(err)
}
log.Printf("title: %s", conf.Title)
log.Printf("Feature 1: %#v", conf.F1)
log.Printf("Feature 2: %#v", conf.F2)
}
Notice the tomlData
and how it maps to the tomlConfig
struct.注意
tomlData
以及它如何映射到tomlConfig
结构。
See more examples at https://github.com/BurntSushi/toml在https://github.com/BurntSushi/toml查看更多示例
A small update for the year 2019 - there is now newer alternative to BurntSushi/toml with a bit richer API to work with .toml files: 2019 年的一个小更新 - 现在有更新的BurntSushi/toml替代品,具有更丰富的 API 来处理.toml文件:
pelletier/go-toml (and documentation )颗粒/go-toml (和文档)
For example having config.toml
file (or in memory):例如有
config.toml
文件(或在内存中):
[postgres]
user = "pelletier"
password = "mypassword"
apart from regular marshal and unmarshal of the entire thing into predefined structure (which you can see in the accepted answer) with pelletier/go-toml you can also query individual values like this:除了使用颗粒/go-toml将整个事物常规编组和解组为预定义结构(您可以在接受的答案中看到)之外,您还可以查询这样的单个值:
config, err := toml.LoadFile("config.toml")
if err != nil {
fmt.Println("Error ", err.Error())
} else {
// retrieve data directly
directUser := config.Get("postgres.user").(string)
directPassword := config.Get("postgres.password").(string)
fmt.Println("User is", directUser, " and password is", directPassword)
// or using an intermediate object
configTree := config.Get("postgres").(*toml.Tree)
user := configTree.Get("user").(string)
password := configTree.Get("password").(string)
fmt.Println("User is", user, " and password is", password)
// show where elements are in the file
fmt.Printf("User position: %v\n", configTree.GetPosition("user"))
fmt.Printf("Password position: %v\n", configTree.GetPosition("password"))
// use a query to gather elements without walking the tree
q, _ := query.Compile("$..[user,password]")
results := q.Execute(config)
for ii, item := range results.Values() {
fmt.Println("Query result %d: %v", ii, item)
}
}
UPDATE更新
There is also spf13/viper that works with .toml config files (among other supported formats), but it might be a bit overkill in many cases.还有spf13/viper可以与 .toml 配置文件(以及其他支持的格式)一起使用,但在许多情况下它可能有点矫枉过正。
UPDATE 2更新 2
Viper is not really an alternative (credits to @GoForth ). Viper 并不是真正的替代品(归功于@GoForth )。
This issue was solved using recommended pkg BurntSushi/toml!!此问题已使用推荐的 pkg BurntSushi/toml 解决!! I did as below and it's part of code.
我做了如下,它是代码的一部分。
[toml example] [toml 示例]
[title]
enable = true
[title.clientinfo.12345]
distance = 30
some_id = 6
[Golang example] 【Golang 示例】
type TitleClientInfo struct {
Distance int `toml:"distance"`
SomeId int `toml:"some_id"`
}
type Config struct {
Enable bool `toml:"enable"`
ClientInfo map[string]TitleClientInfo `toml:"clientinfo"`
}
var config Config
_, err := toml.Decode(string(d), &config)
And then, it can be used as I expected.然后,它可以按我的预期使用。
config.ClientInfo[12345].Distance
Thanks!谢谢!
With solution Viper you can use a configuration file in JSON, TOML, YAML, HCL, INI and others properties formats.使用Viper解决方案,您可以使用 JSON、TOML、YAML、HCL、INI 和其他属性格式的配置文件。
Create file:创建文件:
./config.toml
First import:第一次导入:
import (config "github.com/spf13/viper")
Initialize:初始化:
config.SetConfigName("config")
config.AddConfigPath(".")
err := config.ReadInConfig()
if err != nil {
log.Println("ERROR", err.Error())
}
And get the value:并获得价值:
config.GetString("datatitle.12345.prop1")
config.Get("datatitle.12345.prop1").(int32)
Doc.: https://github.com/spf13/viper文档: https : //github.com/spf13/viper
eg: https://repl.it/@DarlanD/Viper-Examples#main.go例如: https : //repl.it/@DarlanD/Viper-Examples#main.go
I am using spf13/viper我正在使用spf13/viper
Status![]() |
Project![]() |
Starts![]() |
Forks![]() |
---|---|---|---|
Alive![]() |
spf13/viper ![]() |
||
Unmaintained![]() |
BurntSushi/toml ![]() |
so that is why I choose viper
所以这就是我选择
viper
I tried to use a table to put the code and the contents of the configuration file together, but obviously, the editing did not match the final result, so I put the image up in the hope that it would make it easier for you to compare我试着用一张表格把代码和配置文件的内容放在一起,但是很明显,编辑和最终的结果不符,所以我把图片放上来,希望可以方便大家比较
package main
import (
"github.com/spf13/viper"
"log"
"os"
)
func main() {
check := func(err error) {
if err != nil {
panic(err)
}
}
myConfigPath := "test_config.toml"
fh, err := os.OpenFile(myConfigPath, os.O_RDWR, 0666)
check(err)
viper.SetConfigType("toml") // do not ignore
err = viper.ReadConfig(fh)
check(err)
// Read
log.Printf("%#v", viper.GetString("title")) // "my config"
log.Printf("%#v", viper.GetString("DataTitle.12345.prop1")) // "30"
log.Printf("%#v", viper.GetString("dataTitle.12345.prop1")) // "30" // case-insensitive
log.Printf("%#v", viper.GetInt("DataTitle.12345.prop1")) // 30
log.Printf("%#v", viper.GetIntSlice("feature1.userids")) // []int{456, 789}
// Write
viper.Set("database", "newuser")
viper.Set("owner.name", "Carson")
viper.Set("feature1.userids", []int{111, 222}) // overwrite
err = viper.WriteConfigAs(myConfigPath)
check(err)
}
title = "my config"
[datatitle]
[datatitle.12345]
prop1 = 30
[feature1]
userids = [456,789]
database = "newuser" # New
title = "my config"
[datatitle]
[datatitle.12345]
prop1 = 30
[feature1]
userids = [111,222] # Update
[owner] # New
name = "Carson"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.