Golang’s best-kept secret: ‘executable examples’

Go programming language provides a feature called executable examples to write code that can be included in the generated documentation and automatically verified behaviorally in the same way as tests. An executable example is like a test function, whose name must begin with the word Example and can test functions that print to standard output or return a result using fmt.Println to print the result and verify it with the Output comment. Go supports multiple examples for the same function by adding a suffix after the function name or Example function for the whole package. Go’s documentation tool automatically associates the example with the function.


To top