blob: e75b1059193de67be1231bc4e8cf1a6537fc3da4 [file] [log] [blame]
Russ Coxaf5018f2020-03-09 23:54:35 -04001# Godoc: documenting Go code
Andrew Gerranddb9a09f2013-03-08 11:17:09 +1100231 Mar 2011
Andrew Gerrandb316fcd2013-06-05 09:59:16 +10003Tags: godoc, technical
Russ Coxaf5018f2020-03-09 23:54:35 -04004Summary: The Go project takes documentation seriously. Documentation is a huge part of making software accessible and maintainable. Of course it must be well-written and accurate, but it also must be easy to write and to maintain. Ideally, it should be coupled to the code itself so the documentation evolves along with the code. The easier it is for programmers to produce good documentation, the better for everyone.
Andrew Gerranddb9a09f2013-03-08 11:17:09 +11005
6Andrew Gerrand
7
Russ Coxaf5018f2020-03-09 23:54:35 -04008##
Andrew Gerranddb9a09f2013-03-08 11:17:09 +11009
Russ Cox482079d2020-03-09 22:11:04 -040010The Go project takes documentation seriously.
11Documentation is a huge part of making software accessible and maintainable.
12Of course it must be well-written and accurate,
13but it also must be easy to write and to maintain.
14Ideally, it should be coupled to the code itself so the documentation evolves
15along with the code.
16The easier it is for programmers to produce good documentation,
17the better for everyone.
Andrew Gerranddb9a09f2013-03-08 11:17:09 +110018
Russ Coxaf5018f2020-03-09 23:54:35 -040019To that end, we have developed the [godoc](https://golang.org/cmd/godoc/) documentation tool.
Russ Cox482079d2020-03-09 22:11:04 -040020This article describes godoc's approach to documentation,
21and explains how you can use our conventions and tools to write good documentation
22for your own projects.
Andrew Gerranddb9a09f2013-03-08 11:17:09 +110023
Russ Cox482079d2020-03-09 22:11:04 -040024Godoc parses Go source code - including comments - and produces documentation
25as HTML or plain text.
26The end result is documentation tightly coupled with the code it documents.
27For example, through godoc's web interface you can navigate from a function's
Russ Coxaf5018f2020-03-09 23:54:35 -040028[documentation](https://golang.org/pkg/strings/#HasPrefix) to its [implementation](https://golang.org/src/pkg/strings/strings.go#L493) with one click.
Andrew Gerranddb9a09f2013-03-08 11:17:09 +110029
Russ Coxaf5018f2020-03-09 23:54:35 -040030Godoc is conceptually related to Python's [Docstring](http://www.python.org/dev/peps/pep-0257/)
31and Java's [Javadoc](http://www.oracle.com/technetwork/java/javase/documentation/index-jsp-135444.html),
Russ Cox482079d2020-03-09 22:11:04 -040032but its design is simpler.
33The comments read by godoc are not language constructs (as with Docstring)
34nor must they have their own machine-readable syntax (as with Javadoc).
35Godoc comments are just good comments, the sort you would want to read even
36if godoc didn't exist.
Andrew Gerranddb9a09f2013-03-08 11:17:09 +110037
Russ Cox482079d2020-03-09 22:11:04 -040038The convention is simple: to document a type,
39variable, constant, function, or even a package,
40write a regular comment directly preceding its declaration,
41with no intervening blank line.
42Godoc will then present that comment as text alongside the item it documents.
Russ Coxaf5018f2020-03-09 23:54:35 -040043For example, this is the documentation for the `fmt` package's [`Fprint`](https://golang.org/pkg/fmt/#Fprint) function:
Andrew Gerranddb9a09f2013-03-08 11:17:09 +110044
45 // Fprint formats using the default formats for its operands and writes to w.
46 // Spaces are added between operands when neither is a string.
47 // It returns the number of bytes written and any write error encountered.
48 func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
49
Russ Cox482079d2020-03-09 22:11:04 -040050Notice this comment is a complete sentence that begins with the name of
51the element it describes.
52This important convention allows us to generate documentation in a variety of formats,
53from plain text to HTML to UNIX man pages,
54and makes it read better when tools truncate it for brevity,
55such as when they extract the first line or sentence.
Andrew Gerranddb9a09f2013-03-08 11:17:09 +110056
Russ Cox482079d2020-03-09 22:11:04 -040057Comments on package declarations should provide general package documentation.
Russ Coxaf5018f2020-03-09 23:54:35 -040058These comments can be short, like the [`sort`](https://golang.org/pkg/sort/)
Russ Cox482079d2020-03-09 22:11:04 -040059package's brief description:
Andrew Gerranddb9a09f2013-03-08 11:17:09 +110060
61 // Package sort provides primitives for sorting slices and user-defined
62 // collections.
63 package sort
64
Russ Coxaf5018f2020-03-09 23:54:35 -040065They can also be detailed like the [gob package](https://golang.org/pkg/encoding/gob/)'s overview.
Russ Cox482079d2020-03-09 22:11:04 -040066That package uses another convention for packages that need large amounts
67of introductory documentation:
68the package comment is placed in its own file,
Russ Coxaf5018f2020-03-09 23:54:35 -040069[doc.go](https://golang.org/src/pkg/encoding/gob/doc.go),
Russ Cox482079d2020-03-09 22:11:04 -040070which contains only those comments and a package clause.
Andrew Gerranddb9a09f2013-03-08 11:17:09 +110071
Russ Cox482079d2020-03-09 22:11:04 -040072When writing package comments of any size,
Russ Coxaf5018f2020-03-09 23:54:35 -040073keep in mind that their first sentence will appear in godoc's [package list](https://golang.org/pkg/).
Andrew Gerranddb9a09f2013-03-08 11:17:09 +110074
Russ Cox482079d2020-03-09 22:11:04 -040075Comments that are not adjacent to a top-level declaration are omitted from godoc's output,
76with one notable exception.
77Top-level comments that begin with the word `"BUG(who)”` are recognized as known bugs,
78and included in the "Bugs” section of the package documentation.
79The "who part should be the user name of someone who could provide more information.
Russ Coxaf5018f2020-03-09 23:54:35 -040080For example, this is a known issue from the [bytes package](https://golang.org/pkg/bytes/#pkg-note-BUG):
Andrew Gerranddb9a09f2013-03-08 11:17:09 +110081
82 // BUG(r): The rule Title uses for word boundaries does not handle Unicode punctuation properly.
83
Andrew Gerrand257114a2016-01-27 16:38:22 +110084Sometimes a struct field, function, type, or even a whole package becomes
85redundant or unnecessary, but must be kept for compatibility with existing
86programs.
87To signal that an identifier should not be used, add a paragraph to its doc
88comment that begins with "Deprecated:" followed by some information about the
89deprecation.
Russ Coxaf5018f2020-03-09 23:54:35 -040090There are a few examples [in the standard library](https://golang.org/search?q=Deprecated:).
Andrew Gerrand257114a2016-01-27 16:38:22 +110091
Andrew Gerranddb9a09f2013-03-08 11:17:09 +110092There are a few formatting rules that Godoc uses when converting comments to HTML:
93
Russ Coxaf5018f2020-03-09 23:54:35 -040094 - Subsequent lines of text are considered part of the same paragraph;
95 you must leave a blank line to separate paragraphs.
Andrew Gerranddb9a09f2013-03-08 11:17:09 +110096
Russ Coxaf5018f2020-03-09 23:54:35 -040097 - Pre-formatted text must be indented relative to the surrounding comment
98 text (see gob's [doc.go](https://golang.org/src/pkg/encoding/gob/doc.go) for an example).
Andrew Gerranddb9a09f2013-03-08 11:17:09 +110099
Russ Coxaf5018f2020-03-09 23:54:35 -0400100 - URLs will be converted to HTML links; no special markup is necessary.
Andrew Gerranddb9a09f2013-03-08 11:17:09 +1100101
102Note that none of these rules requires you to do anything out of the ordinary.
103
Russ Cox482079d2020-03-09 22:11:04 -0400104In fact, the best thing about godoc's minimal approach is how easy it is to use.
105As a result, a lot of Go code, including all of the standard library,
106already follows the conventions.
Andrew Gerranddb9a09f2013-03-08 11:17:09 +1100107
Russ Cox482079d2020-03-09 22:11:04 -0400108Your own code can present good documentation just by having comments as described above.
109Any Go packages installed inside `$GOROOT/src/pkg` and any `GOPATH` work
110spaces will already be accessible via godoc's command-line and HTTP interfaces,
111and you can specify additional paths for indexing via the `-path` flag or
Russ Coxaf5018f2020-03-09 23:54:35 -0400112just by running `"godoc ."` in the source directory.
113See the [godoc documentation](https://golang.org/cmd/godoc/) for more details.