---
title: "Implement MarshalYAML for a custom Go type"
url: https://recursiveai.org/oneyaml/go/cookbook/marshal-custom-type-to-yaml/
description: "Give a custom Go type a MarshalYAML method so the YAML encoder writes a formatted string with its unit instead of the underlying number."
version: "1.0.0"
---

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

[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-custom-type-to-yaml/marshal-custom-type-to-yaml.zip)

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

```shell
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](https://recursiveai.org/oneyaml/go/cookbook/marshal-custom-type-to-yaml/marshal-custom-type-to-yaml.zip) 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
```

## Related recipes

- [Decode a YAML scalar into a custom Go type](https://recursiveai.org/oneyaml/go/cookbook/read-yaml-into-custom-go-type/)
- [Write a struct to a YAML file in Go](https://recursiveai.org/oneyaml/go/cookbook/write-yaml/)

---

## 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](https://go.dev/ref/spec#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.
