Examples are tests in Go
An Example function with an Output comment is compiled, run and checked.
func Stars(tpm int) string {
if tpm >= 100 {
return "***"
}
return "*.."
}
func ExampleStars() {
fmt.Println(Stars(104))
fmt.Println(Stars(80))
// Output:
// ***
// *..
}
func ExampleStars_slow() {
fmt.Println(Stars(10))
// Output: *..
}
How it works
- The name links it to what it documents: ExampleStars.
- The Output comment must match exactly.
- It appears in the generated documentation.
Keywords and builtins used here
funcifintreturnstring
The run, in numbers
- Lines
- 19
- Characters to type
- 239
- Tokens
- 62
- Three-star pace
- 110 tpm
At the three-star pace of 110 tokens a minute, this run takes about 34 seconds.
Step 3 of 3 in Measuring & generating, step 10 of 13 in Testing.