-
Notifications
You must be signed in to change notification settings - Fork 6
/
example_test.go
102 lines (87 loc) · 1.59 KB
/
example_test.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package ini_test
import (
"fmt"
"os"
"github.com/subpop/go-ini"
)
func ExampleMarshal() {
type Database struct {
Server string
Port int
File string
Path map[string]string
}
type Person struct {
Name string
Organization string
}
type Config struct {
Version string
Owner Person
Database Database
}
config := Config{
Version: "1.2.3",
Owner: Person{
Name: "John Doe",
Organization: "Acme Widgets Inc.",
},
Database: Database{
Server: "192.0.2.62",
Port: 143,
File: "payroll.dat",
Path: map[string]string{"unix": "/var/db"},
},
}
b, err := ini.Marshal(config)
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write(b)
// Output:
// Version=1.2.3
//
// [Owner]
// Name=John Doe
// Organization=Acme Widgets Inc.
//
// [Database]
// Server=192.0.2.62
// Port=143
// File=payroll.dat
// Path[unix]=/var/db
}
func ExampleUnmarshal() {
type Database struct {
Server string
Port int
File string
Path map[string]string
}
type Person struct {
Name string
Organization string
}
type Config struct {
Version string
Owner Person
Database Database
}
var config Config
data := []byte(`Version=1.2.3
[Owner]
Name=John Doe
Organization=Acme Widgets Inc.
[Database]
Server=192.0.2.62
Port=143
File=payroll.dat
Path[unix]=/var/db
Path[win32]=C:\db`)
if err := ini.Unmarshal(data, &config); err != nil {
fmt.Println("error:", err)
}
fmt.Println(config)
// Output:
// {1.2.3 {John Doe Acme Widgets Inc.} {192.0.2.62 143 payroll.dat map[unix:/var/db win32:C:\db]}}
}