Search⌘ K
AI Features

Accessing Go Documentation

Explore how to use the go doc command and godoc utility to access Go documentation without an internet connection. Understand how to run these tools from the command line and browse documentation through a local web server, enabling quick reference for Go packages and functions.

The go doc and godoc utilities

The Go distribution comes with a plethora of tools that can make our lives as programmers easier. Two of these tools are the go doc subcommand and the godoc utility, which allow us to see the documentation of existing Go functions and packages without needing an internet connection. We can also view the Go documentation online. Because godoc is not installed by default, we might need to install it by running go get golang.org/x/tools/cmd/godoc.

The go doc command can be executed as a normal command-line application that displays its output on a terminal, and godoc can be executed as a command-line application that starts a web server. In the latter case, we need a web browser to look at the Go documentation. The first utility is similar to the UNIX man(1) command but for Go functions and packages.

So, in order to find information about the Printf() function of the fmt package, we should execute the following command:

go doc fmt.Printf

Similarly, we can find information about the entire fmt package by running the following command:

go doc fmt

The second utility requires executing godoc with the -http parameter:

godoc -http=:8001

The numeric value in the preceding command, which in this case is 8001, is the port number the HTTP server will listen to. Because we’ve omitted the IP address, godoc is going to listen to all network interfaces.

Note: We can choose any port number that is available, provided that we have the right privileges. However, note that port numbers 0-1023 are restricted and can only be used by the root user, so it is better to avoid choosing one of those and pick something else, provided that it is not already in use by a different process.

We can omit the equals sign in the presented command and put a space character in its place. So, the following command is completely equivalent to the previous one:

godoc -http :8001

After that, we should point our web browser to the following URL in order to get the list of available Go packages and browse their documentation:

C++
{{EDUCATIVE_LIVE_VM_URL}}

If you’re using Go for the first time, you’ll find the Go documentation very handy for learning the parameters and the return values of the functions you want to use.

Let’s try the learned commands

Let’s try what we have learned so far. Please copy a command from script.sh and paste it into the terminal window to see the output:

# This command provides documentation for Printf() 
#function of the fmt package in the terminal window
go doc fmt.Printf

# This command displays information about the entire
# fmt package in the terminal window
go doc fmt

# The following two commands open documentation of 
# Go packages in a browser
godoc -http=:8001

# To proceed, please follow these steps:

# 1. Press Ctrl + C to stop the currently running server.
# 2. Execute the following command.
# 3. Click on the link provided in the widget to continue.
godoc -http :8001
script.sh

Note: We need to press “Ctrl + C” to terminate the server.