...
1# go-yaml fork
2
3This package is a fork of the go-yaml library and is intended solely for consumption
4by kubernetes projects. In this fork, we plan to support only critical changes required for
5kubernetes, such as small bug fixes and regressions. Larger, general-purpose feature requests
6should be made in the upstream go-yaml library, and we will reject such changes in this fork
7unless we are pulling them from upstream.
8
9This fork is based on v2.4.0: https://github.com/go-yaml/yaml/releases/tag/v2.4.0
10
11# YAML support for the Go language
12
13Introduction
14------------
15
16The yaml package enables Go programs to comfortably encode and decode YAML
17values. It was developed within [Canonical](https://www.canonical.com) as
18part of the [juju](https://juju.ubuntu.com) project, and is based on a
19pure Go port of the well-known [libyaml](http://pyyaml.org/wiki/LibYAML)
20C library to parse and generate YAML data quickly and reliably.
21
22Compatibility
23-------------
24
25The yaml package supports most of YAML 1.1 and 1.2, including support for
26anchors, tags, map merging, etc. Multi-document unmarshalling is not yet
27implemented, and base-60 floats from YAML 1.1 are purposefully not
28supported since they're a poor design and are gone in YAML 1.2.
29
30Installation and usage
31----------------------
32
33The import path for the package is *gopkg.in/yaml.v2*.
34
35To install it, run:
36
37 go get gopkg.in/yaml.v2
38
39API documentation
40-----------------
41
42If opened in a browser, the import path itself leads to the API documentation:
43
44 * [https://gopkg.in/yaml.v2](https://gopkg.in/yaml.v2)
45
46API stability
47-------------
48
49The package API for yaml v2 will remain stable as described in [gopkg.in](https://gopkg.in).
50
51
52License
53-------
54
55The yaml package is licensed under the Apache License 2.0. Please see the LICENSE file for details.
56
57
58Example
59-------
60
61```Go
62package main
63
64import (
65 "fmt"
66 "log"
67
68 "gopkg.in/yaml.v2"
69)
70
71var data = `
72a: Easy!
73b:
74 c: 2
75 d: [3, 4]
76`
77
78// Note: struct fields must be public in order for unmarshal to
79// correctly populate the data.
80type T struct {
81 A string
82 B struct {
83 RenamedC int `yaml:"c"`
84 D []int `yaml:",flow"`
85 }
86}
87
88func main() {
89 t := T{}
90
91 err := yaml.Unmarshal([]byte(data), &t)
92 if err != nil {
93 log.Fatalf("error: %v", err)
94 }
95 fmt.Printf("--- t:\n%v\n\n", t)
96
97 d, err := yaml.Marshal(&t)
98 if err != nil {
99 log.Fatalf("error: %v", err)
100 }
101 fmt.Printf("--- t dump:\n%s\n\n", string(d))
102
103 m := make(map[interface{}]interface{})
104
105 err = yaml.Unmarshal([]byte(data), &m)
106 if err != nil {
107 log.Fatalf("error: %v", err)
108 }
109 fmt.Printf("--- m:\n%v\n\n", m)
110
111 d, err = yaml.Marshal(&m)
112 if err != nil {
113 log.Fatalf("error: %v", err)
114 }
115 fmt.Printf("--- m dump:\n%s\n\n", string(d))
116}
117```
118
119This example will generate the following output:
120
121```
122--- t:
123{Easy! {2 [3 4]}}
124
125--- t dump:
126a: Easy!
127b:
128 c: 2
129 d: [3, 4]
130
131
132--- m:
133map[a:Easy! b:map[c:2 d:[3 4]]]
134
135--- m dump:
136a: Easy!
137b:
138 c: 2
139 d:
140 - 3
141 - 4
142```
143
View as plain text