---
title: "Get YAML value byte offsets in Go"
url: https://recursiveai.org/oneyaml/go/cookbook/locate-yaml-value-in-source/
description: "Get a parsed YAML value’s byte range, exact spelling, and line and column in UTF-8 source."
version: "1.0.0"
---

# Get YAML value byte offsets in Go

**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/locate-yaml-value-in-source/locate-yaml-value-in-source.zip)

Find a value in parsed UTF-8 YAML. `Span()` gives its byte range; `Source()` keeps quotes and escapes.

```shell
go get oneyaml.dev/go@v1.0.0
```

Requires Go 1.24 or later.

```go
package main

import (
    "fmt"
    yaml "oneyaml.dev/go"
)

func main() {
    const input = `# Saved labels
café: "line\nbreak" # note
`
    doc, err := yaml.Parse([]byte(input))
    if err != nil { panic(err) }
    node, ok := doc.Root().Lookup("café")
    if !ok { panic("missing café key") }
    start, end := node.Span()
    line, col := node.Position()
    fmt.Println(line, col, start, end)
    fmt.Println(node.Source())
}
```

Unzip [the example](https://recursiveai.org/oneyaml/go/cookbook/locate-yaml-value-in-source/locate-yaml-value-in-source.zip), then `go run .`.

Prints the line and column, the start and end offsets, then the source spelling:

```text
2 7 22 35
"line\nbreak"
```

## The full program

The download also slices the bytes at the reported offsets and prints the decoded string. In the decoded string, `\n` becomes a newline.

```go
package main

import (
	"fmt"
	"log"

	yaml "oneyaml.dev/go"
)

func main() {
	const input = `# Saved labels
café: "line\nbreak" # note
`
	doc, err := yaml.Parse([]byte(input))
	if err != nil {
		log.Fatal(err)
	}
	node, ok := doc.Root().Lookup("café")
	if !ok {
		log.Fatal("missing café key")
	}
	start, end := node.Span()
	line, col := node.Position()
	fmt.Printf("Line %d, column %d\n", line, col)
	fmt.Printf("Bytes [%d:%d]: %s\n", start, end, node.Source())
	fmt.Printf("Slice: %s\n", doc.Bytes()[start:end])
	fmt.Printf("Decoded:\n%s\n", node.Value())
}
```

`go run .` prints:

```text
Line 2, column 7
Bytes [22:35]: "line\nbreak"
Slice: "line\nbreak"
Decoded:
line
break
```

## Variations

**Compare a tag with the value it decorates.** Use `café: !!str "line\nbreak" # note` in the full program’s input. After finding `node`, print the tag and value separately:

```go
fmt.Println(node.TagAsWritten())
fmt.Println(node.Source())
```

```text
!!str
"line\nbreak"
```

`Span()` covers the value text, not the `!!str` tag in front of it.

## Related recipes

- [Show YAML errors with line and column in Go](https://recursiveai.org/oneyaml/go/cookbook/errors-with-line-and-column/)
- [Read YAML tags and directives in Go](https://recursiveai.org/oneyaml/go/cookbook/read-yaml-tags-and-directives/)

## The short answer

Parse the UTF-8 document and find the value node, then use `Span()` to get the byte offsets and `Source()` to get the exact text. `Position()` gives the 1-based line and column of the node’s start. Use the byte range for a Go slice and the line and column for a message that someone can follow in their editor.

## What does byte offset mean?

A byte offset counts from zero at the start of the text. For the example, `22` is the opening quote and `35` is the first byte after the closing quote. That’s why `doc.Bytes()[22:35]` includes both quotes but not the space before `# note`. The [pinned `Span()` API](https://github.com/recursive-ai-oss/oneyaml-go/blob/7a4c8e39f041b945ee5f121c055f03bc2d7d01d9/node.go#L691-L718) gives offsets into the UTF-8 text and excludes a node’s anchor or tag.

If all you need is the text to show in a message, use `Source()` instead of slicing. If you’re recording a range to highlight in another tool, save the byte offsets with the specific source you parsed. An insertion earlier in the file can change where the same value starts, so re-find the node in the updated text rather than applying the old numbers to it.

## What’s the difference between a byte offset and a character column?

A Go string can contain UTF-8, and an index into a [Go string counts bytes](https://go.dev/blog/strings), not characters. In `café`, `é` takes two UTF-8 bytes but counts as one character for the column in this recipe. The quote is at column 7 on the second line, after the four characters in `café`, the colon, and a space. Its byte offset is 22 because that count also includes the whole comment line and its newline.

Keep a node’s position separate from its decoded value, too. The written text has the characters backslash and `n` on line 2. `Value()` decodes them into a newline. Measuring or searching for that decoded string in the original bytes would not give the range of the quoted value. The parser already identified that range, so use `Span()` for the original spelling even when `Value()` looks quite different.
