Skip to content

Instantly share code, notes, and snippets.

@manuelarte
Last active July 4, 2025 06:13
Show Gist options
  • Save manuelarte/6a68d270174edea452f4f8d07d69c17d to your computer and use it in GitHub Desktop.
Save manuelarte/6a68d270174edea452f4f8d07d69c17d to your computer and use it in GitHub Desktop.
Round trip yaml not honoring space distance
// ISSUE: https://github.com/goccy/go-yaml/issues/772
package main
import (
"fmt"
"github.com/goccy/go-yaml"
"log"
)
func main() {
input := []byte(`
version: 2
key:
prop: value0 # double space between value and comment
list:
- hello # hello
- bye # bye
`)
var fields yaml.MapSlice
cm := make(map[string][]*yaml.Comment)
err := yaml.UnmarshalWithOptions(input, &fields, yaml.UseOrderedMap(), yaml.CommentToMap(cm))
if err != nil {
log.Fatal(err)
}
output, err := yaml.MarshalWithOptions(fields, yaml.IndentSequence(true), yaml.WithComment(cm))
if err != nil {
log.Fatal(err)
}
fmt.Println(string(output))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment