-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyamlmarshal.go
75 lines (66 loc) · 1.41 KB
/
yamlmarshal.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
package main
import (
"fmt"
"gopkg.in/yaml.v3"
)
type Users struct {
Name string `yaml:"name"`
Age int8 `yaml:"age"`
Address string `yaml:"address"`
Hobby []string `yaml:"hobby"`
}
func main() {
wanger := Users{
Name: "wanger",
Age: 24,
Address: "beijing",
Hobby: []string{"literature", "social"},
}
dongdong := Users{
Name: "冬哥",
Age: 30,
Address: "chengdu",
Hobby: []string{"basketball", "guitar"},
}
xialaoshi := Users{
Name: "夏老师",
Age: 29,
Address: "chengdu",
Hobby: []string{"吃吃喝喝"},
}
huazai := Users{
Name: "华子",
Age: 28,
Address: "shenzhen",
Hobby: []string{"王者荣耀"},
}
qiaoke := Users{
Name: "乔克",
Age: 30,
Address: "chongqing",
Hobby: []string{"阅读", "王者荣耀"},
}
jiangzong := Users{
Name: "姜总",
Age: 25,
Address: "shanghai",
Hobby: []string{"钓鱼","音乐","美食","酒"},
}
zhengge := Users{
Name: "郑哥",
Age: 30,
Address: "beijing",
Hobby: []string{"阅读", "复读机"},
}
userlist:=[7]Users{wanger,dongdong,huazai,qiaoke,xialaoshi,jiangzong,zhengge}
yamlData, err := yaml.Marshal(&userlist)
if err != nil {
fmt.Printf("Error while Marshaling. %v", err)
}
fmt.Println(string(yamlData))
fileName := "test.yaml"
err = ioutil.WriteFile(fileName, yamlData, 0644)
if err != nil {
panic("Unable to write data into the file")
}
}