Am I missing something or are you talking testable examples ?
Basically, it works as follows: in the *_test.go file, you need to adhere to the agreement Example[[T][_M]] , where T is a placeholder for a type and M placeholder for the method that you want to display the tested example as an example code in Godoc. If the function is simply called Example() , the code will be shown as an example package.
Below the last line of code in your example, you can put a comment like this
Now go test will verify that the test case functions as it should , place everything below // Output: (including spaces) or it will cause the test to fail.
Here is an example for a verified example.
func ExampleMongoStore_Get() { sessionId := "ExampleGetSession" data, err := ms.Get(sessionId) if err == sessionmw.ErrSessionNotFound { fmt.Printf("Session '%s' not found\n", sessionId) data = make(map[string]interface{}) data["foo"] = "bar" ms.Save(sessionId, data) } loaded, _ := ms.Get(sessionId) fmt.Printf("Loaded value '%s' for key '%s' in session '%s'", loaded["foo"], "foo", sessionId) // Output: // Session 'ExampleGetSession' not found // Loaded value 'bar' for key 'foo' in session 'ExampleGetSession' }
Edit: see the output of the above example on godoc.org
Markus W Mahlberg
source share