This is a created article in which the information may have evolved or changed.
Brief introduction
The Dodoc example is an aggregation of some executable test code that they provide to the reader for reading and execution as part of the package's documentation. The reader can click the "Run" button to test the code.
Golang's standard package includes many of these code examples (such as the strings package)
This article describes how to write a similar code example.
Sample is Unit test
The code example compiles and executes as part of the package.
In a typical unit test, the example is some of the methods in the _test.go file in the package. The code sample differs from the test code in that the example method begins with Example (different from test of the code tested) and has no parameters.
stringutil 包是 golang 代码示例仓库中的一部分。下面的代码展示了他是如何演示 Reverse 的用法。
package stringutil_testimport ("fmt""github.com/golang/example/stringutil")func ExampleReverse() {fmt.Println(stringutil.Reverse("hello"))// Output: olleh}
The above code can be found in the Example_test.go file in the Stringutil directory.
Godoc This code example in the documentation for the Reverse method, as detailed in the figure: