...
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 v3.0.1: https://github.com/go-yaml/yaml/releases/tag/v3.0.1.
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.2, but preserves some behavior
26from 1.1 for backwards compatibility.
27
28Specifically, as of v3 of the yaml package:
29
30 - YAML 1.1 bools (_yes/no, on/off_) are supported as long as they are being
31 decoded into a typed bool value. Otherwise they behave as a string. Booleans
32 in YAML 1.2 are _true/false_ only.
33 - Octals encode and decode as _0777_ per YAML 1.1, rather than _0o777_
34 as specified in YAML 1.2, because most parsers still use the old format.
35 Octals in the _0o777_ format are supported though, so new files work.
36 - Does not support base-60 floats. These are gone from YAML 1.2, and were
37 actually never supported by this package as it's clearly a poor choice.
38
39and offers backwards
40compatibility with YAML 1.1 in some cases.
411.2, including support for
42anchors, tags, map merging, etc. Multi-document unmarshalling is not yet
43implemented, and base-60 floats from YAML 1.1 are purposefully not
44supported since they're a poor design and are gone in YAML 1.2.
45
46Installation and usage
47----------------------
48
49The import path for the package is *gopkg.in/yaml.v3*.
50
51To install it, run:
52
53 go get gopkg.in/yaml.v3
54
55API documentation
56-----------------
57
58If opened in a browser, the import path itself leads to the API documentation:
59
60 - [https://gopkg.in/yaml.v3](https://gopkg.in/yaml.v3)
61
62API stability
63-------------
64
65The package API for yaml v3 will remain stable as described in [gopkg.in](https://gopkg.in).
66
67
68License
69-------
70
71The yaml package is licensed under the MIT and Apache License 2.0 licenses.
72Please see the LICENSE file for details.
73
74
75Example
76-------
77
78```Go
79package main
80
81import (
82 "fmt"
83 "log"
84
85 "gopkg.in/yaml.v3"
86)
87
88var data = `
89a: Easy!
90b:
91 c: 2
92 d: [3, 4]
93`
94
95// Note: struct fields must be public in order for unmarshal to
96// correctly populate the data.
97type T struct {
98 A string
99 B struct {
100 RenamedC int `yaml:"c"`
101 D []int `yaml:",flow"`
102 }
103}
104
105func main() {
106 t := T{}
107
108 err := yaml.Unmarshal([]byte(data), &t)
109 if err != nil {
110 log.Fatalf("error: %v", err)
111 }
112 fmt.Printf("--- t:\n%v\n\n", t)
113
114 d, err := yaml.Marshal(&t)
115 if err != nil {
116 log.Fatalf("error: %v", err)
117 }
118 fmt.Printf("--- t dump:\n%s\n\n", string(d))
119
120 m := make(map[interface{}]interface{})
121
122 err = yaml.Unmarshal([]byte(data), &m)
123 if err != nil {
124 log.Fatalf("error: %v", err)
125 }
126 fmt.Printf("--- m:\n%v\n\n", m)
127
128 d, err = yaml.Marshal(&m)
129 if err != nil {
130 log.Fatalf("error: %v", err)
131 }
132 fmt.Printf("--- m dump:\n%s\n\n", string(d))
133}
134```
135
136This example will generate the following output:
137
138```
139--- t:
140{Easy! {2 [3 4]}}
141
142--- t dump:
143a: Easy!
144b:
145 c: 2
146 d: [3, 4]
147
148
149--- m:
150map[a:Easy! b:map[c:2 d:[3 4]]]
151
152--- m dump:
153a: Easy!
154b:
155 c: 2
156 d:
157 - 3
158 - 4
159```
160
View as plain text