8

How to define a map in TOML?

For example, I want to define something like:

[FOO]

Usernames_Passwords='{"user1":"pass1","user2":"pass2"}'

and then in go convert them to a map[string]string

4

2 回答 2

7

你可以有这样的地图:

name = { first = "Tom", last = "Preston-Werner" }
point = { x = 1, y = 2 }

见:https ://github.com/toml-lang/toml#user-content-inline-table

在您的情况下,您似乎需要一个密码表或一组地图。你可以这样:

[[user_entry]]
name = "user1"
pass = "pass1"

[[user_entry]]
name = "user2"
pass = "pass2"

或更简洁地说:

user_entry = [{ name = "user1", pass = "pass1" },
              { name = "user2", pass = "pass2" }]
于 2016-12-26T05:23:53.957 回答
5

这适用于 github.com/BurntSushi/toml(不支持内联表):

d := `
    [FOO.Usernames_Passwords]
    a="foo"
    b="bar"
    `
var s struct {
    FOO struct {
        Usernames_Passwords map[string]string
    }
}
_, err := toml.Decode(d, &s)
// check err!
fmt.Printf("%+v", s)

使用github.com/naoina/toml这有效(使用内联表):

d := `
    [FOO]
    Usernames_Passwords = { a = "foo" , b = "bar" }
    `
var s struct {
    FOO struct {
        Usernames_Passwords map[string]string
    }
}
err := toml.Unmarshal([]byte(d), &s)
if err != nil {
    panic(err)
}
fmt.Printf("%+v", s)
于 2016-12-26T05:19:47.057 回答