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
The yaml package will already format it as suggested automatically, as long as the string contains newlines. The fact this heuristic is not working for your case probably implies that the content being marshaled contains unusual characters that had to be quoted to be parsed properly. Can you please provide a reproducer using https://play.golang.org so we can look into it?