Colin Fraizer
Colin Fraizer

Reputation: 582

Viewing Example functions with `go doc` command?

If I view a package's documentation via the web at https://pkg.go.dev, the page includes Example test functions. The output of go doc -u -all for a package does not. Is it possible to have such information included?

Upvotes: 9

Views: 1680

Answers (3)

Mitar
Mitar

Reputation: 7050

I installed godoc with:

go install -v golang.org/x/tools/cmd/godoc@latest

And examples (if correctly named) are shown for me. I think you should correctly name your example functions in tests.

Upvotes: 0

Thundercat
Thundercat

Reputation: 121049

It is not possible to display examples using the go doc command.

The command implementation ignores examples.

Upvotes: 5

Mahdi zarepoor
Mahdi zarepoor

Reputation: 430

Godoc examples are snippets of Go code that are displayed as package documentation and that are verified by running them as tests. They can also be run by a user visiting the godoc web page for the package and clicking the associated “Run” button

https://go.dev/blog/examples

Upvotes: 1

Related Questions