liveBook · Manning

Output comment

As explained in Learn Go with Pocket-Sized Projects

An output directive is a Go example comment that marks the following text as the expected standard output for test comparison.

An output comment marks the following commented lines as the expected standard output for a Go example function. Go compares that output when the example runs under testing. section 2.1.2

Testing printed output

func Example_main() { main() // Output: // Hello world } Run `go test` to execute the example and compare its printed output with the expected lines. section 2.1.2

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Defines placement and behavior, including compilation without execution when the directive is absent, but omits testing internals.
Examples
Uses an Example_main function that calls main and expects the line Hello world.
What it assumes
Assumes familiarity with Go functions and test files while demonstrating the output-checking convention from a small example.
What you can do after
Apply it via Go's test command to validate printed output and detect regressions after code changes.
How it connects
Connects output comments with example functions, standard output, documentation generation, and automated testing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.