What is the meaning of “examples” in testing testing (golang)?

I recently read into testing and examples as part of testing testing and did not understand what they were intended for. I see the documentation says:

The package also launches and validates the sample code. Examples of functions may include a closing line comment that begins with "Output:" and compares with the standard output of the function when performing tests. (The comparison ignores the starting and ending spaces.)

However, I do not really appreciate the reason why this will exist. When I write my tests, it seems that it should be more than clear how to use the code just by reading unit tests and tests. What additional motivation does the examples section provide? It seems superfluous to me, but I’m sure that the go inventors put it for a good reason, especially because they seem to sympathize with the good programming practice of designing their language. I hope to understand their motivation or how this part of the language can be used positively in the golang project! :)

+7
go testing
source share
1 answer

In Go, source code is also used to create documentation. The examples are not intended for testing, but rather for documentation. By compiling examples, you can make sure that they work correctly for those who use them.

The examples found in standard libraries can be run directly in a browser using golang.org to view documentation. Godoc.org instead links your sample code to play.golang.org , where you can try it.

For an example or usage example, see http://golang.org/pkg/sort/

+10
source share

All Articles