Viewing Example functions with `go doc` command?
Asked Answered
K

3

7

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?

Karalee answered 10/9, 2022 at 17:1 Comment(3)
It is not possible to display examples using the go doc command. The command implementation ignores examples.Oasis
Thank you, @CeriseLimón. That seemed to be the case, but I was holding out hope. I don't mean to generate work for you, but would you consider turning your comment into an answer?Karalee
github.com/golang/go/issues/26715Urus
M
4

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

The command implementation ignores examples.

Myke answered 15/9, 2022 at 4:13 Comment(0)
E
1

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

Erfert answered 10/9, 2022 at 17:37 Comment(1)
Thanks for the answer, but what I really wanted was just to see the examples in the terminal output—just like the rest of the go documentation, but I guess the go doc command does not allow this.Karalee
S
0

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.

Stinkpot answered 19/6, 2023 at 11:1 Comment(1)
Thanks for the answer. It's a good answer, but not what I wanted, which was just to see the examples in the terminal, not in my browser.Karalee

© 2022 - 2024 — McMap. All rights reserved.