Ask questionsTrailing spaces force quoted string encoding
I use yaml.Unmarshal to Unmarshal a file.
The file is as follows:
Then I change the struct
Then I use Marshal to reverse it:
The output is as follows: the output is Completely messed up，Who can tell me How can I preserve the original format? I expect the output looks like that: Many thanks!
Answer questions niemeyer
Your str2 Go literal has invisible trailing spaces:
" \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.