Creating Example Functions
Let’s learn how to create example functions.
We'll cover the following
Part of the documentation process is generating example code that showcases the use of some or all the functions and data types of a package. Example functions have many benefits, including the fact that they are executable tests that are executed by go test
. Therefore, if an example function contains an // Output:
line, the go test
tool checks whether the calculated output matches the values found after the // Output:
line. Although we should include example functions in Go files that end with _test.go
, we do not need to import the testing
Go package for example functions. Moreover, the name of each example function must begin with Example
. Lastly, example functions take no input parameters and return no results.
Coding example
We are going to illustrate example functions using the code of exampleFunctions.go
and exampleFunctions_test.go
. The content of exampleFunctions.go
is as follows:
Get hands-on with 1400+ tech skills courses.