TIL: Testable Examples in Go

  • Seen while going through Go with tests.

  • If you add a function in your test file that starts with the name Example, the test suite will run it.

func ExampleAdd() {
	sum := Add(1, 5)
	fmt.Println(sum)
	// Output: 6
}
  • Expected output, or value to assert against, is that last line, // Output: 6
  • Docs on Testable Examples
  • Apparently all this plays well with godoc, which I’m yet to get working.