File: example_text_marshaler_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 (50 lines) | stat: -rw-r--r-- 880 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
package toml_test

import (
	"fmt"
	"net"

	"github.com/naoina/toml"
)

func Example_textUnmarshaler() {
	type Config struct {
		Servers []net.IP
	}

	input := []byte(`
servers = ["192.0.2.10", "198.51.100.3"]
`)

	var config Config
	toml.Unmarshal(input, &config)
	fmt.Printf("Unmarshaled:\n%+v\n\n", config)

	output, _ := toml.Marshal(&config)
	fmt.Printf("Marshaled:\n%s", output)

	// Output:
	// Unmarshaled:
	// {Servers:[192.0.2.10 198.51.100.3]}
	//
	// Marshaled:
	// servers = ["192.0.2.10", "198.51.100.3"]
}

func Example_textUnmarshalerError() {
	type Config struct {
		Servers []net.IP
	}

	input := []byte(`
servers = ["192.0.2.10", "198.51.100.500"]
`)

	var config Config
	err := toml.Unmarshal(input, &config)
	fmt.Printf("Unmarshal error:\n%v", err)

	// Output:
	// Unmarshal error:
	// line 2: (toml_test.Config.Servers) invalid IP address: 198.51.100.500
}