Below example fails. Even if I copy the output of go test to the output section, it still fails.
func ExampleTrailingSpace() {
fmt.Println("abc ")
fmt.Println("cde")
// Output:
//abc
//cde
}
However, if I remove the second print, it succeeds.
func ExampleTrailingSpace() {
fmt.Println("abc ")
// Output:
//abc
}
Why? Because testing/example.go trims leading and trailing spaces of the whole output, but not every line. Code formatting tools, including gofmt, trims the space of each comment line.
I suggest trimming each line before comparing the output.
Below example fails. Even if I copy the output of
go testto the output section, it still fails.However, if I remove the second print, it succeeds.
Why? Because
testing/example.gotrims leading and trailing spaces of the whole output, but not every line. Code formatting tools, includinggofmt, trims the space of each comment line.I suggest trimming each line before comparing the output.