Skip to content

How to install and use godoc tool for golang programming?

How to install and use godoc tool for golang programming?

Problem

Sometimes you want to find examples or specifications of a Go module (package). You should use godoc, a tool to quickly find the documentation of a package. However, when you execute the godoc command in the command line, you might encounter an error message like this:

Terminal window
godoc fmt
command not found: godoc

Environments

The environments are as follows:

  • Go version: go version go1.14.2 darwin/amd64
  • GOROOT: /usr/local/go
  • GOPATH: /Users/me/GoProjects/src
  • IDE: IntelliJ IDEA Ultimate 2019.3 with Go plugin

The godoc tool

GoDoc hosts documentation for Go packages on Bitbucket, GitHub, Launchpad, and Google Project Hosting.

The source code for GoDoc is available on GitHub.

GoDoc displays documentation for GOOS=linux unless otherwise noted at the bottom of the documentation page.

Use godoc in your terminal

If you want to look up some package’s documentation in the command line, you should install the godoc tool first.

gotool

GoTools holds the source for various packages and tools that support the Go programming language.

Some of the tools, such as godoc and vet, are included in binary Go distributions. Others, including the Go guru and the test coverage tool, can be fetched with go get.

Packages include a type-checker for Go and an implementation of the Static Single Assignment form (SSA) representation for Go programs.

Install godoc tool with golang tools

You can install the godoc command like this:

install_godoc.sh
go get -v golang.org/x/tools/cmd/godoc
go: found golang.org/x/tools/cmd/godoc in golang.org/x/tools v0.0.0-20200717024301-6ddee64345a6
go: downloading golang.org/x/net v0.0.0-20200625001655-4c5254603344
golang.org/x/tools/godoc/vfs/gatefs
golang.org/x/tools/godoc/vfs/mapfs
golang.org/x/tools/godoc/vfs/zipfs
golang.org/x/tools/godoc/static
golang.org/x/net/context/ctxhttp
golang.org/x/tools/playground
golang.org/x/tools/godoc/redirect
golang.org/x/tools/cmd/godoc

Use the godoc tool in command line

You can now use the godoc tool in the command line to look up documentation for Go packages:

Terminal window
go doc fmt.Println
package fmt // import "fmt"
func Println(a ...interface{}) (n int, err error)
Println formats using the default formats for its operands and writes to
standard output. Spaces are always added between operands and a newline is
appended. It returns the number of bytes written and any write error
encountered.

Use the godoc tool online

Instead of using the godoc command line, you can also use it online in your web browser:

Navigate to: https://godoc.org/, and you will see this:

godoc1

Search for the package fmt, and you will get this result. You can see that it is more informative than the command line.

godoc2

Summary

The godoc tool is an essential resource for Go developers, providing quick access to package documentation. Whether you prefer using the command line or browsing online, godoc makes it easy to find the information you need. By following the steps in this post, you can install and use godoc effectively in your Go projects.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!