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?

3
  • 1
    It is not possible to display examples using the go doc command. The command implementation ignores examples. Commented Sep 10, 2022 at 22:29
  • 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? Commented Sep 15, 2022 at 3:48
  • github.com/golang/go/issues/26715
    – quanta
    Commented Mar 19 at 6:23

3 Answers 3

4

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

The command implementation ignores examples.

0
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

1
  • 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. Commented Aug 8, 2023 at 1:06
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.

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. Commented Aug 8, 2023 at 1:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.