profile
viewpoint

Ask questionsTrailing spaces force quoted string encoding

I use yaml.Unmarshal to Unmarshal a file. image

image

The file is as follows: image

Then I change the struct

image

Then I use Marshal to reverse it: image

The output is as follows: image the output is Completely messed up,Who can tell me How can I preserve the original format? I expect the output looks like that: image Many thanks!

go-yaml/yaml

Answer questions niemeyer

Your str2 Go literal has invisible trailing spaces:

fmt.Printf("%q\n", str2)

Output:

"   \nkey namespace_name\npattern %s\n"

The yaml encoder currently avoids marshaling it out as a plain scalar because, just like in your Go source code, this makes the data invisible. If the Go string is fixed to not have trailing spaces, the yaml output will look like what you seem to expect.

useful!

Related questions

v3 panics on bad input hot 1
v3 equivalent of v2's MapSlice and MapItem? hot 1
unmarshal with comments provide wrong structure hot 1
Head comments in list elements misplaced hot 1
source:https://uonfu.com/
Github User Rank List