Recursive AI

Marshal a Go slice as an inline YAML list

oneyaml-go is a free, open-source YAML library for Go from Recursive AI. MIT or Apache-2.0, no dependencies.

Add ,flow to the field’s YAML tag to write [80, 443] instead of one dash-prefixed item per line.

Terminal
go mod init example && go get oneyaml.dev/go

Requires Go 1.24 or later. Run in a Go module.

GO
package main
 
import (
    "fmt"
    "log"
 
    "oneyaml.dev/go"
)
 
func main() {
    config := struct {
        Ports []int    `yaml:"ports,flow"`
        Hosts []string `yaml:"hosts"`
    }{
        Ports: []int{80, 443},
        Hosts: []string{"web"},
    }
    out, err := oneyaml.Marshal(config)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Print(string(out))
}

Paste the code above into main.go, run go mod init example and go get oneyaml.dev/go, then go run .; or unzip the example and go run ..

Text
ports: [80, 443]
hosts:
  - web

Variations

Write one item per line. Remove ,flow from the Ports field declaration:

GO
Ports []int `yaml:"ports"`
Text
ports:
  - 80
  - 443
hosts:
  - web

The short answer

Use yaml:"ports,flow" on the slice field and pass the struct to Marshal. Only ports gets the bracketed form. The top-level mapping still has one key per line, and the hosts slice without flow still uses dashes. There’s no need to format the slice yourself or rewrite the output after marshaling.

How do I write a sequence in YAML?

YAML has two collection styles. Flow style uses brackets and commas, as in ports: [80, 443]. Block style writes ports: on one line and indents the - 80 and - 443 items below it. Either form represents the same ordered sequence of integers. A reader doesn’t need a different Go field type for the two spellings.

Flow style is useful for short lists of scalars such as ports, where the whole list is easy to see at once. Leave longer lists in block style when one item per line makes a change easier to review. The tag controls representation, not the type of the entries. For example, hand-joining a slice into the string [80, 443] before you call Marshal gives the encoder a string, not a sequence. Pass the slice itself so the encoder can keep each item’s type and handle quoting.

How do I marshal a YAML list inline without changing the whole document?

Put flow on the slice field, after its name in the tag. The name ports decides the key; flow decides the collection style. Apply the option only to the fields you want to compact. inline is a different option: it puts the fields of an embedded struct, or entries of an inlined map, into the enclosing mapping. It doesn’t mean “put this list on one line.”

A missing slice and a present but empty slice are also different. In oneyaml-go, a nil slice marshals as null when the field is emitted; adding flow won’t turn it into a sequence. Pass an initialized empty slice when you want an empty sequence instead. This recipe controls output generated from Go values. To preserve the style and comments of a YAML file someone already wrote, use the document editing API instead of rebuilding the file from a struct.

All oneyaml Go recipes