// used for comments

Go doc

You can launch Go docs locally with godoc -http :8000. Docs will be available at the localhost:8000/pkg with all packages that installed on your system.

This command isn’t included by default in newer version of Go, but can be installed with go install golang.org/x/tools/cmd/godoc@latest.

Function docstring

Add documentation to the function:

// Add takes two integers and returns the sum of them.
func Add(x, y int) int {
	return x + y
}

It will be picked up by your IDE:

Examples

Go has interesting feature with function examples. They are compiled and executed (optionally) as part of package’s test suite.

They reside in a package’s _test.go files. Functions should be prefixed with Example:

func ExampleAdd() {
	sum := Add(1, 5)
	fmt.Println(sum)
	// Output: 6
}

Then when we serve Go doc with godoc -http=:6060, we can find a documentation for our package at http://localhost:6060/pkg/: