1 // code examples for godoc
9 toml "github.com/pelletier/go-toml"
13 config, err := toml.LoadFile("config.toml")
16 fmt.Println("Error ", err.Error())
18 // retrieve data directly
19 user := config.Get("postgres.user").(string)
20 password := config.Get("postgres.password").(string)
22 // or using an intermediate object
23 configTree := config.Get("postgres").(*toml.Tree)
24 user = configTree.Get("user").(string)
25 password = configTree.Get("password").(string)
26 fmt.Println("User is", user, " and password is", password)
28 // show where elements are in the file
29 fmt.Printf("User position: %v\n", configTree.GetPosition("user"))
30 fmt.Printf("Password position: %v\n", configTree.GetPosition("password"))
34 func Example_unmarshal() {
35 type Employer struct {
50 phone = "+1 234 567 89012"
54 toml.Unmarshal(document, &person)
55 fmt.Println(person.Name, "is", person.Age, "and works at", person.Employer.Name)
57 // John is 30 and works at Company Inc.
60 func ExampleMarshal() {
61 type Postgres struct {
62 User string `toml:"user"`
63 Password string `toml:"password"`
64 Database string `toml:"db" commented:"true" comment:"not used anymore"`
67 Postgres Postgres `toml:"postgres" comment:"Postgres configuration"`
70 config := Config{Postgres{User: "pelletier", Password: "mypassword", Database: "old_database"}}
71 b, err := toml.Marshal(config)
75 fmt.Println(string(b))
77 // # Postgres configuration
81 // # db = "old_database"
82 // password = "mypassword"
86 func ExampleUnmarshal() {
87 type Postgres struct {
98 password = "mypassword"`)
101 toml.Unmarshal(doc, &config)
102 fmt.Println("user=", config.Postgres.User)