| 1 |
# go-yaml fork |
| 2 |
|
| 3 |
This package is a fork of the go-yaml library and is intended solely for consumption |
| 4 |
by kubernetes projects. In this fork, we plan to support only critical changes required for |
| 5 |
kubernetes, such as small bug fixes and regressions. Larger, general-purpose feature requests |
| 6 |
should be made in the upstream go-yaml library, and we will reject such changes in this fork |
| 7 |
unless we are pulling them from upstream. |
| 8 |
|
| 9 |
This 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 |
|
| 13 |
Introduction |
| 14 |
------------ |
| 15 |
|
| 16 |
The yaml package enables Go programs to comfortably encode and decode YAML |
| 17 |
values. It was developed within [Canonical](https://www.canonical.com) as |
| 18 |
part of the [juju](https://juju.ubuntu.com) project, and is based on a |
| 19 |
pure Go port of the well-known [libyaml](http://pyyaml.org/wiki/LibYAML) |
| 20 |
C library to parse and generate YAML data quickly and reliably. |
| 21 |
|
| 22 |
Compatibility |
| 23 |
------------- |
| 24 |
|
| 25 |
The yaml package supports most of YAML 1.1 and 1.2, including support for |
| 26 |
anchors, tags, map merging, etc. Multi-document unmarshalling is not yet |
| 27 |
implemented, and base-60 floats from YAML 1.1 are purposefully not |
| 28 |
supported since they're a poor design and are gone in YAML 1.2. |
| 29 |
|
| 30 |
Installation and usage |
| 31 |
---------------------- |
| 32 |
|
| 33 |
The import path for the package is *gopkg.in/yaml.v2*. |
| 34 |
|
| 35 |
To install it, run: |
| 36 |
|
| 37 |
go get gopkg.in/yaml.v2 |
| 38 |
|
| 39 |
API documentation |
| 40 |
----------------- |
| 41 |
|
| 42 |
If 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 |
|
| 46 |
API stability |
| 47 |
------------- |
| 48 |
|
| 49 |
The package API for yaml v2 will remain stable as described in [gopkg.in](https://gopkg.in). |
| 50 |
|
| 51 |
|
| 52 |
License |
| 53 |
------- |
| 54 |
|
| 55 |
The yaml package is licensed under the Apache License 2.0. Please see the LICENSE file for details. |
| 56 |
|
| 57 |
|
| 58 |
Example |
| 59 |
------- |
| 60 |
|
| 61 |
```Go |
| 62 |
package main |
| 63 |
|
| 64 |
import ( |
| 65 |
"fmt" |
| 66 |
"log" |
| 67 |
|
| 68 |
"gopkg.in/yaml.v2" |
| 69 |
) |
| 70 |
|
| 71 |
var data = ` |
| 72 |
a: Easy! |
| 73 |
b: |
| 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. |
| 80 |
type T struct { |
| 81 |
A string |
| 82 |
B struct { |
| 83 |
RenamedC int `yaml:"c"` |
| 84 |
D []int `yaml:",flow"` |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
func 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 |
|
| 119 |
This example will generate the following output: |
| 120 |
|
| 121 |
``` |
| 122 |
--- t: |
| 123 |
{Easy! {2 [3 4]}} |
| 124 |
|
| 125 |
--- t dump: |
| 126 |
a: Easy! |
| 127 |
b: |
| 128 |
c: 2 |
| 129 |
d: [3, 4] |
| 130 |
|
| 131 |
|
| 132 |
--- m: |
| 133 |
map[a:Easy! b:map[c:2 d:[3 4]]] |
| 134 |
|
| 135 |
--- m dump: |
| 136 |
a: Easy! |
| 137 |
b: |
| 138 |
c: 2 |
| 139 |
d: |
| 140 |
- 3 |
| 141 |
- 4 |
| 142 |
``` |
| 143 |
|