---
title: "Marshal a Go slice as an inline YAML list"
url: https://recursiveai.org/oneyaml/go/cookbook/marshal-yaml-list-in-flow-style/
description: "Add the flow option to a Go struct field’s YAML tag to print a slice as an inline list while leaving the rest of the document in block style."
version: "1.0.0"
---

# 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.

[pkg.go.dev](https://pkg.go.dev/oneyaml.dev/go) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-go) · [Docs](https://recursiveai.org/oneyaml/go/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/go/cookbook/marshal-yaml-list-in-flow-style/marshal-yaml-list-in-flow-style.zip)

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

```shell
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](https://recursiveai.org/oneyaml/go/cookbook/marshal-yaml-list-in-flow-style/marshal-yaml-list-in-flow-style.zip) 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
```

## Related recipes

- [Write YAML from Go values](https://recursiveai.org/oneyaml/go/cookbook/write-yaml/)
- [Flatten an embedded struct in YAML](https://recursiveai.org/oneyaml/go/cookbook/flatten-embedded-struct-in-yaml/)

---

## 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](https://yaml.org/spec/1.2.2/#chapter-7-flow-style-productions). 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](https://recursiveai.org/oneyaml/go/cookbook/edit-yaml-keep-comments/) instead of rebuilding the file from a struct.
