How to define a map in TOML?

This works using github.com/BurntSushi/toml (does not support inline tables):

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)

Using github.com/naoina/toml this works (using inline tables):

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)

You can have maps like this:

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

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

In your case, it looks like you want a password table, or an array of maps. You can make it like this:

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

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

Or more concisely:

user_entry = [{ name = "user1", pass = "pass1" },
              { name = "user2", pass = "pass2" }]

Tags:

Go

Toml