Choose the output style
The task: write a Go value as YAML that matches the house style of the files around it.
The value
type Service struct {
Name string `yaml:"name"`
Ports []int `yaml:"ports"`
Labels map[string]string `yaml:"labels"`
Notes string `yaml:"notes"`
Enabled string `yaml:"enabled"`
}
var svc = Service{
Name: "api",
Ports: []int{80, 443},
Labels: map[string]string{"tier": "web", "team": "core"},
Notes: "line one\nline two\n",
Enabled: "yes", // a string, though YAML 1.1 would read the plain word as true
}Full source: docs/examples/encoding/style/example_test.go
The default
out, err := codec.Marshal(svc)
if err != nil {
fmt.Println(err)
return
}
fmt.Print(string(out))Full source: docs/examples/encoding/style/example_test.go
name: api
ports:
- 80
- 443
labels:
team: core
tier: web
notes: |
line one
line two
enabled: "yes"Full source: docs/examples/encoding/style/example_test.go
Two spaces per level. Sequence entries sit at their key’s column, as
go.yaml.in/yaml/v3 writes them. A map’s keys come out sorted, a struct’s
fields in declaration order. A multiline string is a | block. "yes" is
quoted because YAML 1.1 reads the plain word as a boolean.
Indent
out, _ := codec.Marshal(svc, codec.Indent(4))
fmt.Print(string(out))
out, _ := codec.Marshal(svc, codec.Indent(4), codec.IndentSequence(true))
fmt.Print(string(out))Full source: docs/examples/encoding/style/example_test.go
name: api
ports:
- 80
- 443
labels:
team: core
tier: web
notes: |
line one
line two
enabled: "yes"Full source: docs/examples/encoding/style/example_test.go
Indent(4) moves the mappings and the block scalar to four spaces, and leaves
the ports entries at column 0. Indent sets the spaces per level of mapping
nesting, and a sequence entry is not a level.
IndentSequence
out, _ := codec.Marshal(svc, codec.Indent(4), codec.IndentSequence(true))
fmt.Print(string(out))Full source: docs/examples/encoding/style/example_test.go
name: api
ports:
- 80
- 443
labels:
team: core
tier: web
notes: |
line one
line two
enabled: "yes"Full source: docs/examples/encoding/style/example_test.go
IndentSequence(true) moves the entries under their key, by the Indent
width. Pass both for a file indented by four throughout.
Flow
out, _ := codec.Marshal(svc, codec.Flow(true))
fmt.Print(string(out))Full source: docs/examples/encoding/style/example_test.go
{name: api, ports: [80, 443], labels: {team: core, tier: web}, notes: "line one\nline two\n", enabled: "yes"}Full source: docs/examples/encoding/style/example_test.go
Flow(true) writes every collection in flow style. The multiline string
becomes a double-quoted scalar with \n escapes, since a block scalar cannot
stand inside a flow collection.
Single quotes
out, _ := codec.Marshal(map[string]string{"enabled": "yes", "port": "8080"}, codec.UseSingleQuote(true))
fmt.Print(string(out))Full source: docs/examples/encoding/style/example_test.go
enabled: 'yes'
port: '8080'Full source: docs/examples/encoding/style/example_test.go
UseSingleQuote(true) changes the quote character, not which strings get
quoted. api stays plain under either.
Literal blocks
// "key: value" on the first line would read as a mapping in a plain scalar,
// so the default quotes the whole string.
v := map[string]string{"script": "set -e: on\nmake test\n"}
plain, _ := codec.Marshal(v)
literal, _ := codec.Marshal(v, codec.UseLiteralStyleIfMultiline(true))
fmt.Print(string(plain), "---\n", string(literal))Full source: docs/examples/encoding/style/example_test.go
script: "set -e: on\nmake test\n"
---
script: |
set -e: on
make testFull source: docs/examples/encoding/style/example_test.go
The default already writes most multiline strings as a | block, as notes
above shows. It falls back to a double-quoted scalar when the first line would
read as something else in plain YAML, such as set -e: on.
UseLiteralStyleIfMultiline(true) writes those as a block too.
Variations
- JSON output.
codec.JSON()writes JSON. To convert a YAML document to JSON without a Go value, see YAML to JSON and back. - Every option. Encode options lists all of them, with what each one leaves out.