(Go) How to use toml files?

前端 未结 5 1096
长发绾君心
长发绾君心 2021-02-15 17:47

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


        
5条回答
  •  无人及你
    2021-02-15 18:26

    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!

提交回复
热议问题