Find a value in parsed UTF-8 YAML. Span() gives its byte range; Source() keeps quotes and escapes.
go get oneyaml.dev/go@v1.0.0Requires Go 1.24 or later.
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, then go run ..
Prints the line and column, the start and end offsets, then the source spelling:
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.
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:
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:
fmt.Println(node.TagAsWritten()) fmt.Println(node.Source())
!!str "line\nbreak"
Span() covers the value text, not the !!str tag in front of it.
Related recipes
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 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, 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.