Skip to content

testing: examples with trailing whitespace in multiple lines of output can be confusing #26460

@joesis

Description

@joesis

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions