Skip to content

Wrong marshalling of "\n" #807

@AzazKamaz

Description

@AzazKamaz

Describe the bug
String "\n" is marshalled as ""
It seems that such corner cases needs to be handled separately/carefully

To Reproduce
https://go.dev/play/p/g2_Q0O3Grgi

package main

import (
	"fmt"
	"github.com/goccy/go-yaml"
)

func main() {
	data := "\n"
	buf, _ := yaml.Marshal(&data)
	fmt.Printf("%#v\n", string(buf))
	_ = yaml.Unmarshal(buf, &data)
	fmt.Printf("%#v\n", data)
}

Gives

"|\n\n"
""

Expected behavior
I expect it to stringify newline as newline

Version Variables

  • Go version: 1.25
  • go-yaml's Version: 1.15 (in my project and I guess latest 1.18 on go playground)

Additional context
Python stringifies it as "'\n\n '\n"
Also I guess "|+\n\n" will do the thing. I saw some lib that also explicitly sets indentation like |4+

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions