drudfe0446838
2015-12-16 08:50
采纳率: 0%
浏览 290
已采纳

(继续)如何使用toml文件?

As title, I want to know how to use toml files from golang.

Before that, I show my toml examples. 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!

图片转代码服务由CSDN问答提供 功能建议

作为标题,我想知道如何使用golang中的toml文件。 < 在此之前,我将展示我的toml示例。

  [datatitle] 
enable = true 
userids = [
“ 12345”,“ 67890” 
] 
 [datatitle.12345] 
  prop1 = 30 
 prop2 = 10 
 
 [datatitle.67890] 
 prop1 = 30 
 prop2 = 10 
   
 
 

然后,我想 将这些数据设置为struct类型。

因此,我想按以下方式访问子元素。

  datatitle [“ 12345”  ] .prop1 
datatitle [“ 67890”]。prop2 
   
 
 

谢谢!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dttl3933 2015-12-16 09:46
    已采纳

    First get BurntSushi's toml parser:

    go get github.com/BurntSushi/toml

    BurntSushi parses toml and maps it to structs, which is what you want.

    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.

    See more examples at https://github.com/BurntSushi/toml

    已采纳该答案
    打赏 评论
  • doujiao0110 2015-12-18 03:48

    This issue was solved using recommended pkg BurntSushi/toml!! I did as below and it's part of code.

    [toml example]

    [title]
    enable = true
    [title.clientinfo.12345]
    distance = 30
    some_id = 6
    

    [Golang example]

    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!

    打赏 评论
  • dtkf64283 2019-09-21 00:24

    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:

    pelletier/go-toml (and documentation)

    For example having config.toml file (or in memory):

    [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:

    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
    ", configTree.GetPosition("user"))
        fmt.Printf("Password position: %v
    ", 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.

    打赏 评论

相关推荐 更多相似问题