Docs

Viewing Docs

go install golang.org/x/pkgsite/cmd/pkgsite@latest
pkgsite -open .

Open browser on http://localhost:8080

Method Description

You must start the comment off with the method name

// Add takes two ints and returns the sum
func Add(a, b int) int {
	return a + b
}

Examples

Inside your test file add a method prefixed with Example

This ensures that the code compiles

func ExampleAdd() {
	sum := Add(1, 2)
	fmt.Printf("%d", sum)
}

You can provide a comment at the end with prefix Output: this will do the following

  1. Make the example run when you run your test suit
  1. Test and validate your stdout against the comment (if they don’t match the example will fail)
  1. Provide clear commenting in your docs
func ExampleAdd() {
	sum := Add(1, 2)
	fmt.Printf("%d", sum)
	// Output: 3
}