1 # YAML support for the Go language
6 The yaml package enables Go programs to comfortably encode and decode YAML
7 values. It was developed within [Canonical](https://www.canonical.com) as
8 part of the [juju](https://juju.ubuntu.com) project, and is based on a
9 pure Go port of the well-known [libyaml](http://pyyaml.org/wiki/LibYAML)
10 C library to parse and generate YAML data quickly and reliably.
15 The yaml package supports most of YAML 1.1 and 1.2, including support for
16 anchors, tags, map merging, etc. Multi-document unmarshalling is not yet
17 implemented, and base-60 floats from YAML 1.1 are purposefully not
18 supported since they're a poor design and are gone in YAML 1.2.
20 Installation and usage
21 ----------------------
23 The import path for the package is *gopkg.in/yaml.v2*.
27 go get gopkg.in/yaml.v2
32 If opened in a browser, the import path itself leads to the API documentation:
34 * [https://gopkg.in/yaml.v2](https://gopkg.in/yaml.v2)
39 The package API for yaml v2 will remain stable as described in [gopkg.in](https://gopkg.in).
45 The yaml package is licensed under the Apache License 2.0. Please see the LICENSE file for details.
68 // Note: struct fields must be public in order for unmarshal to
69 // correctly populate the data.
73 RenamedC int `yaml:"c"`
74 D []int `yaml:",flow"`
81 err := yaml.Unmarshal([]byte(data), &t)
83 log.Fatalf("error: %v", err)
85 fmt.Printf("--- t:\n%v\n\n", t)
87 d, err := yaml.Marshal(&t)
89 log.Fatalf("error: %v", err)
91 fmt.Printf("--- t dump:\n%s\n\n", string(d))
93 m := make(map[interface{}]interface{})
95 err = yaml.Unmarshal([]byte(data), &m)
97 log.Fatalf("error: %v", err)
99 fmt.Printf("--- m:\n%v\n\n", m)
101 d, err = yaml.Marshal(&m)
103 log.Fatalf("error: %v", err)
105 fmt.Printf("--- m dump:\n%s\n\n", string(d))
109 This example will generate the following output:
123 map[a:Easy! b:map[c:2 d:[3 4]]]