LinuxCommandLibrary

godoc

Display Go documentation and source code

TLDR

Display help for a specific package

$ godoc [fmt]
copy

Display help for the function "Printf" of "fmt" package
$ godoc [fmt] [Printf]
copy

Serve documentation as a web server on port 6060
$ godoc -http=:[6060]
copy

Create an index file
$ godoc -write_index -index_files=[path/to/file]
copy

Use the given index file to search the docs
$ godoc -http=:[6060] -index -index_files=[path/to/file]
copy

SYNOPSIS

godoc [options] [package_path [symbol]]
godoc -http=[address]

PARAMETERS

-http
    Start an HTTP server to serve documentation on the specified address (e.g., :6060).

-src
    Display the source code of the requested package or symbol.

-url
    Print the URL to godoc.org for the given package or symbol.

-html
    Print the documentation in HTML format to standard output.

-goroot
    Specify an alternative GOROOT directory to find Go source files.

-index
    Enable the search index for faster searching in HTTP mode.

-v
    Enable verbose logging, showing more information about operations.

DESCRIPTION

The godoc command provides a way to display documentation for Go packages, types, functions, and methods. It parses Go source code and extracts documentation comments, presenting them in a human-readable format. This command can be used in two primary modes: as a command-line tool to print documentation directly to the console, or as an HTTP server to serve documentation through a web browser. When run as an HTTP server, it can browse all installed Go packages, their source code, and examples, making it an invaluable tool for Go developers to explore the standard library and third-party modules without an internet connection. It integrates directly with the Go module system and the GOROOT environment variable to locate source files.

CAVEATS

The standalone godoc command is largely considered deprecated in modern Go environments. Its primary functionality for command-line display has been superseded by the go doc subcommand. For serving local documentation via HTTP, go doc -http offers similar capabilities. Furthermore, in recent Go versions, the godoc binary is no longer bundled with the standard Go distribution and must be installed separately via go install golang.org/x/tools/cmd/godoc@latest. This makes it less commonly used directly by developers today, who prefer the integrated go doc or pkg.go.dev.

WEB SERVER FUNCTIONALITY

When run with the -http option, godoc transforms into a local web server. By default, it often listens on localhost:6060. Navigating to this address in a web browser allows users to browse all Go packages, search for symbols, and view their source code, acting as a personal, offline version of pkg.go.dev.

INTEGRATION WITH GO MODULES

godoc (and go doc) automatically resolves and displays documentation for modules located in your GOPATH or module cache, providing a seamless experience for projects using Go Modules.

HISTORY

The godoc command was a foundational tool in the early days of the Go programming language, providing a unified and accessible way to view documentation directly from Go source code. It was notable for its ability to generate comprehensive documentation on-the-fly and serve it via a built-in HTTP server, a feature that significantly enhanced developer productivity by allowing offline access to the entire Go standard library documentation. As the Go ecosystem matured, some of godoc's functionalities were integrated into the main go command, particularly the console-based documentation viewing through go doc. This evolution led to the standalone godoc binary becoming less central, eventually being moved out of the standard Go distribution in favor of go doc and external tools/services like pkg.go.dev, while still being available for separate installation.

SEE ALSO

go(1), go-doc(1)

Copied to clipboard