File: example_decoder_test.go

package info (click to toggle)
golang-github-naoina-toml 0.1.1-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 416 kB
  • sloc: makefile: 7
file content (51 lines) | stat: -rw-r--r-- 794 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package toml_test

import (
	"fmt"
	"net"
	"os"
	"time"

	"github.com/naoina/toml"
)

type Config struct {
	Title string
	Owner struct {
		Name string
		Org  string `toml:"organization"`
		Bio  string
		Dob  time.Time
	}
	Database struct {
		Server        string
		Ports         []int
		ConnectionMax uint
		Enabled       bool
	}
	Servers map[string]ServerInfo
	Clients struct {
		Data  [][]interface{}
		Hosts []string
	}
}

type ServerInfo struct {
	IP net.IP
	DC string
}

func Example() {
	f, err := os.Open("testdata/example.toml")
	if err != nil {
		panic(err)
	}
	defer f.Close()
	var config Config
	if err := toml.NewDecoder(f).Decode(&config); err != nil {
		panic(err)
	}

	fmt.Println("IP of server 'alpha':", config.Servers["alpha"].IP)
	// Output: IP of server 'alpha': 10.0.0.1
}