Recursive AI

Implement MarshalYAML for a custom Go type

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

Return a formatted string such as “21.5 C” from MarshalYAML to write text instead of a number.

Terminal
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" // Package name: oneyaml.
)
 
type Celsius float64
 
func (c Celsius) MarshalYAML() (any, error) {
    text := fmt.Sprintf("%.1f C", float64(c))
    return text, nil
}
 
func main() {
    reading := map[string]Celsius{
        "temp": 21.5,
    }
    out, err := oneyaml.Marshal(reading)
    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
temp: 21.5 C

If the method returns an error, Marshal fails and the example exits without printing YAML.

Variations

Write one field as a number. Reuse reading from the example and replace the Marshal call to convert the value to float64 before marshaling:

GO
out, err := oneyaml.Marshal(map[string]float64{
    "temp": float64(reading["temp"]),
})
Text
temp: 21.5

The short answer

Define MarshalYAML() (any, error) on your type. Return the Go value you want encoded in its place. For Celsius, that’s a string containing the formatted number and unit; for a different type it could be a list or mapping. The encoder handles YAML syntax and quoting for the returned value.

How do I customize YAML marshaling for a Go type?

Use a defined type, such as type Celsius float64, and keep that type on the value you hand to Marshal. The example’s map[string]Celsius gives the encoder a Celsius for each value, so it can call the method. If you convert a value to float64 first, there’s no longer a Celsius method to call. That’s useful when you need a numeric output for a specific destination instead of changing the type’s behavior everywhere.

The method uses a value receiver because it only reads the temperature, and both Celsius and *Celsius then have the method in their Go method sets. A pointer receiver alone doesn’t give the value type that method. In particular, don’t rely on the encoder taking the address of a map value. Prefer the value receiver for a small read-only value like this, or pass pointers when your hook is on the pointer type.

What should a MarshalYAML method return?

Return the replacement value, not a fragment of YAML. For a single text scalar, return a string as the example does, without adding quotes or a key. If you return the receiver unchanged, the encoder sees the same hook again; return a built-in value or a helper type without that method instead. If you can’t represent a value, return an error.

Changing the YAML from a number to a string changes what downstream readers receive, not just how it looks. The example also formats to one decimal place, so it isn’t a lossless representation of arbitrary float64 values. Keep the numeric form if readers need numeric precision. If your program needs to read the formatted text back into Celsius, implement the corresponding decode behavior separately; MarshalYAML only defines what you write.

All oneyaml Go recipes