Example syntax defines executable documentation examples in Go. A test-file function named with the Example convention can call code and, when paired with an Output comment, verify its standard output. section 2.1.2
Use the convention in three steps:
- Name the function with the Example pattern; for an unexported main function, Example_main follows the demonstrated convention. section 2.1.2
- Call the function under test, then place an Output comment containing the expected output. section 2.1.2
- Run go test to execute examples with expected output; examples without Output still compile and appear in generated documentation. section 2.1.2
