| 1 | go.yaml.in/yaml |
| 2 | =============== |
| 3 | |
| 4 | YAML Support for the Go Language |
| 5 | |
| 6 | |
| 7 | ## Introduction |
| 8 | |
| 9 | The `yaml` package enables [Go](https://go.dev/) programs to comfortably encode |
| 10 | and decode [YAML](https://yaml.org/) values. |
| 11 | |
| 12 | It was originally developed within [Canonical](https://www.canonical.com) as |
| 13 | part of the [juju](https://juju.ubuntu.com) project, and is based on a pure Go |
| 14 | port of the well-known [libyaml](http://pyyaml.org/wiki/LibYAML) C library to |
| 15 | parse and generate YAML data quickly and reliably. |
| 16 | |
| 17 | |
| 18 | ## Project Status |
| 19 | |
| 20 | This project started as a fork of the extremely popular [go-yaml]( |
| 21 | https://github.com/go-yaml/yaml/) |
| 22 | project, and is being maintained by the official [YAML organization]( |
| 23 | https://github.com/yaml/). |
| 24 | |
| 25 | The YAML team took over ongoing maintenance and development of the project after |
| 26 | discussion with go-yaml's author, @niemeyer, following his decision to |
| 27 | [label the project repository as "unmaintained"]( |
| 28 | https://github.com/go-yaml/yaml/blob/944c86a7d2/README.md) in April 2025. |
| 29 | |
| 30 | We have put together a team of dedicated maintainers including representatives |
| 31 | of go-yaml's most important downstream projects. |
| 32 | |
| 33 | We will strive to earn the trust of the various go-yaml forks to switch back to |
| 34 | this repository as their upstream. |
| 35 | |
| 36 | Please [contact us](https://cloud-native.slack.com/archives/C08PPAT8PS7) if you |
| 37 | would like to contribute or be involved. |
| 38 | |
| 39 | |
| 40 | ### Version Intentions |
| 41 | |
| 42 | Versions `v1`, `v2`, and `v3` will remain as **frozen legacy**. |
| 43 | They will receive **security-fixes only** so that existing consumers keep |
| 44 | working without breaking changes. |
| 45 | |
| 46 | All ongoing work, including new features and routine bug-fixes, will happen in |
| 47 | **`v4`**. |
| 48 | If you’re starting a new project or upgrading an existing one, please use the |
| 49 | `go.yaml.in/yaml/v4` import path. |
| 50 | |
| 51 | |
| 52 | ## Compatibility |
| 53 | |
| 54 | The `yaml` package supports most of YAML 1.2, but preserves some behavior from |
| 55 | 1.1 for backwards compatibility. |
| 56 | |
| 57 | Specifically, v3 of the `yaml` package: |
| 58 | |
| 59 | * Supports YAML 1.1 bools (`yes`/`no`, `on`/`off`) as long as they are being |
| 60 | decoded into a typed bool value. |
| 61 | Otherwise they behave as a string. |
| 62 | Booleans in YAML 1.2 are `true`/`false` only. |
| 63 | * Supports octals encoded and decoded as `0777` per YAML 1.1, rather than |
| 64 | `0o777` as specified in YAML 1.2, because most parsers still use the old |
| 65 | format. |
| 66 | Octals in the `0o777` format are supported though, so new files work. |
| 67 | * Does not support base-60 floats. |
| 68 | These are gone from YAML 1.2, and were actually never supported by this |
| 69 | package as it's clearly a poor choice. |
| 70 | |
| 71 | |
| 72 | ## Installation and Usage |
| 73 | |
| 74 | The import path for the package is *go.yaml.in/yaml/v4*. |
| 75 | |
| 76 | To install it, run: |
| 77 | |
| 78 | ```bash |
| 79 | go get go.yaml.in/yaml/v4 |
| 80 | ``` |
| 81 | |
| 82 | |
| 83 | ## API Documentation |
| 84 | |
| 85 | See: <https://pkg.go.dev/go.yaml.in/yaml/v4> |
| 86 | |
| 87 | |
| 88 | ## API Stability |
| 89 | |
| 90 | The package API for yaml v3 will remain stable as described in [gopkg.in]( |
| 91 | https://gopkg.in). |
| 92 | |
| 93 | |
| 94 | ## Example |
| 95 | |
| 96 | ```go |
| 97 | package main |
| 98 | |
| 99 | import ( |
| 100 | "fmt" |
| 101 | "log" |
| 102 | |
| 103 | "go.yaml.in/yaml/v4" |
| 104 | ) |
| 105 | |
| 106 | var data = ` |
| 107 | a: Easy! |
| 108 | b: |
| 109 | c: 2 |
| 110 | d: [3, 4] |
| 111 | ` |
| 112 | |
| 113 | // Note: struct fields must be public in order for unmarshal to |
| 114 | // correctly populate the data. |
| 115 | type T struct { |
| 116 | A string |
| 117 | B struct { |
| 118 | RenamedC int `yaml:"c"` |
| 119 | D []int `yaml:",flow"` |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func main() { |
| 124 | t := T{} |
| 125 | |
| 126 | err := yaml.Unmarshal([]byte(data), &t) |
| 127 | if err != nil { |
| 128 | log.Fatalf("error: %v", err) |
| 129 | } |
| 130 | fmt.Printf("--- t:\n%v\n\n", t) |
| 131 | |
| 132 | d, err := yaml.Marshal(&t) |
| 133 | if err != nil { |
| 134 | log.Fatalf("error: %v", err) |
| 135 | } |
| 136 | fmt.Printf("--- t dump:\n%s\n\n", string(d)) |
| 137 | |
| 138 | m := make(map[any]any) |
| 139 | |
| 140 | err = yaml.Unmarshal([]byte(data), &m) |
| 141 | if err != nil { |
| 142 | log.Fatalf("error: %v", err) |
| 143 | } |
| 144 | fmt.Printf("--- m:\n%v\n\n", m) |
| 145 | |
| 146 | d, err = yaml.Marshal(&m) |
| 147 | if err != nil { |
| 148 | log.Fatalf("error: %v", err) |
| 149 | } |
| 150 | fmt.Printf("--- m dump:\n%s\n\n", string(d)) |
| 151 | } |
| 152 | ``` |
| 153 | |
| 154 | This example will generate the following output: |
| 155 | |
| 156 | ``` |
| 157 | --- t: |
| 158 | {Easy! {2 [3 4]}} |
| 159 | |
| 160 | --- t dump: |
| 161 | a: Easy! |
| 162 | b: |
| 163 | c: 2 |
| 164 | d: [3, 4] |
| 165 | |
| 166 | |
| 167 | --- m: |
| 168 | map[a:Easy! b:map[c:2 d:[3 4]]] |
| 169 | |
| 170 | --- m dump: |
| 171 | a: Easy! |
| 172 | b: |
| 173 | c: 2 |
| 174 | d: |
| 175 | - 3 |
| 176 | - 4 |
| 177 | ``` |
| 178 | |
| 179 | |
| 180 | ## Development and Testing with `make` |
| 181 | |
| 182 | This project's makefile (`GNUmakefile`) is set up to support all of the |
| 183 | project's testing, automation and development tasks in a completely |
| 184 | deterministic way. |
| 185 | |
| 186 | Some `make` commands are: |
| 187 | |
| 188 | * `make test` |
| 189 | * `make lint tidy` |
| 190 | * `make test-shell` |
| 191 | * `make test v=1` |
| 192 | * `make test o='-foo --bar=baz'` # Add extra CLI options |
| 193 | * `make test GO-VERSION=1.2.34` |
| 194 | * `make test GO_YAML_PATH=/usr/local/go/bin` |
| 195 | * `make shell` # Start a shell with the local `go` environment |
| 196 | * `make shell GO-VERSION=1.2.34` |
| 197 | * `make distclean` # Remove all generated files including `.cache/` |
| 198 | |
| 199 | |
| 200 | ### Dependency Auto-install |
| 201 | |
| 202 | By default, this makefile will not use your system's Go installation, or any |
| 203 | other system tools that it needs. |
| 204 | |
| 205 | The only things from your system that it relies on are: |
| 206 | * Linux or macOS |
| 207 | * GNU `make` (3.81+) |
| 208 | * `git` |
| 209 | * `bash` |
| 210 | * `curl` |
| 211 | |
| 212 | Everything else, including Go and Go utils, are installed and cached as they |
| 213 | are needed by the makefile (under `.cache/`). |
| 214 | |
| 215 | > **Note**: Use `make shell` to get a subshell with the same environment that |
| 216 | > the makefile set up for its commands. |
| 217 | |
| 218 | |
| 219 | ### Using your own Go |
| 220 | |
| 221 | If you want to use your own Go installation and utils, export `GO_YAML_PATH` to |
| 222 | the directory containing the `go` binary. |
| 223 | |
| 224 | Use something like this: |
| 225 | |
| 226 | ``` |
| 227 | export GO_YAML_PATH=$(dirname "$(command -v go)") |
| 228 | make <rule> |
| 229 | # or: |
| 230 | make <rule> GO_YAML_PATH=$(dirname "$(command -v go)") |
| 231 | ``` |
| 232 | |
| 233 | > **Note:** `GO-VERSION` and `GO_YAML_PATH` are mutually exclusive. |
| 234 | > When `GO_YAML_PATH` is set, the Makefile uses your own Go installation and |
| 235 | > ignores any `GO-VERSION` setting. |
| 236 | |
| 237 | |
| 238 | ## The `go-yaml` CLI Tool |
| 239 | |
| 240 | This repository includes a `go-yaml` CLI tool which can be used to understand |
| 241 | the internal stages and final results of YAML processing with the go-yaml |
| 242 | library. |
| 243 | |
| 244 | We strongly encourage you to show pertinent output from this command when |
| 245 | reporting and discussing issues. |
| 246 | |
| 247 | ```bash |
| 248 | make go-yaml |
| 249 | ./go-yaml --help |
| 250 | ./go-yaml <<< ' |
| 251 | foo: &a1 bar |
| 252 | *a1: baz |
| 253 | ' -n # Show value on decoded Node structs (formatted in YAML) |
| 254 | ``` |
| 255 | |
| 256 | You can also install it with: |
| 257 | |
| 258 | ```bash |
| 259 | go install go.yaml.in/yaml/v4/cmd/go-yaml@latest |
| 260 | ``` |
| 261 | |
| 262 | |
| 263 | ## License |
| 264 | |
| 265 | The yaml package is licensed under the MIT and Apache License 2.0 licenses. |
| 266 | Please see the LICENSE file for details. |