skip to content

Doc Comments

A doc comment is the sentence above the declaration, and gofmt canonicalises its headings, lists and [pkg.Name] links, so go doc and pkg.go.dev render the same text you read in the source.

part ofGo (Golang)overview, primer and where to startread it →
on this pageshow

questions

4

Where must a Go doc comment sit for `go doc` to pick it up as documentation?

level: juniorimportance: should knowfreq 48%

answer

  1. no marker, no annotation, just position
  2. one blank line changes everything
  3. directly above the declaration
  4. a comment above a const group covers the block

basics

~20 s

A doc comment must sit immediately above the declaration it documents, with no blank line between them. Go has no annotation syntax at all: adjacency is the whole rule, so a single blank line turns documentation into an ordinary comment.

solid answer

~40 s

Go has no documentation markers. A doc comment is just a comment group placed directly before a `package` clause or a top-level `const`, `var`, `func` or `type` declaration, with nothing between them — one blank line and it is no longer attached to anything, so `go doc` and the rendered package page show that declaration as undocumented. A comment above a grouped `const (` or `var (` block documents the whole group, and each spec inside can also carry its own comment. Convention is to open with the name being documented, since the first sentence is used as the one-line summary in indexes. Unexported declarations can be documented too; `go doc -u` is how you read those.

code

go · 10 lines
go
// Fetch returns the current price for id.
func Fetch(id string) (int, error) {
	return 0, nil
}

// This sentence documents nothing: the blank line below detaches it.

func Store(id string, price int) error {
	return nil
}

go deeper

for a junior

Be ready to write one on the spot: a complete sentence directly above the exported declaration, no blank line, opening with the name. Know that a comment inside the body is not documentation.

for a middle

Explain that position alone attaches the comment, that a comment above a grouped const or var block documents the whole group, and that unexported declarations can be documented and read with go doc -u.

for a senior

Show how you catch detached or missing comments in review — reading what go doc prints for a changed declaration rather than trusting the diff, since a detached comment looks attached in a patch.

for a principal

Take the position that the rendered page, not the source, is what strangers consume, and decide what your team treats as done for an exported declaration: a first sentence that stands alone away from the code.

## Go has no documentation syntax — it has a placement rule Most languages mark documentation with a special token: a `/**` opener, a `"""` docstring, an attribute. Go does none of that. A **doc comment** is an ordinary comment — a run of `//` lines, or a single `/* */` block — that sits **immediately before** the thing it documents, with **no blank line** in between. That adjacency is the entire attachment mechanism. When the toolchain builds documentation it parses the file, and for each declaration it takes the comment group that ends on the line directly above it. Anything separated by a blank line is a free-floating comment: valid Go, invisible to documentation. ```go // Fetch returns the current price for id. func Fetch(id string) (int, error) { ... } // Not documentation — the blank line below detaches this. func Store(id string, price int) error { ... } ``` Run `go doc` on that package and `Fetch` has a description while `Store` has none. Nothing warns you; the comment is still there in the source, doing nothing for the reader who only ever sees the rendered page. ## What can carry a doc comment - **The package clause.** A comment group immediately above `package name` is the package comment — the opening paragraph of the whole package's page. - **Top-level declarations:** `func`, `type`, `var`, `const`, and methods. - **Grouped declarations.** A comment above `const (` or `var (` documents the group as a unit. Individual specs inside the parentheses may carry their own comments, which are shown per constant. This is why a block of related status codes usually has one paragraph above the group explaining the set, plus short comments per value. - **Struct fields and interface methods.** Comments above a field, or on the same line after it, are kept and displayed when the type is printed. Comments **inside a function body** are never documentation. They are notes to the next person editing the code, and no tool surfaces them. ## The identifier-first convention By convention a doc comment starts with the name it documents, as a complete sentence: `// Fetch returns the current price for id.` This is not enforced by the compiler, but it matters for two practical reasons. First, the **first sentence is used as the summary** wherever a list of symbols is shown, so it has to stand alone away from the declaration. Second, a reader scanning a rendered page sees the sentence, not your source layout, so a summary beginning with *It* or *This function* reads as an orphan. ## Exported versus unexported Documentation is not limited to exported names. You can — and on a package other people maintain, should — document unexported helpers. They are simply hidden by default: `go doc ./pkg` shows exported symbols only, and `go doc -u ./pkg` includes unexported ones. The public documentation site never shows them at all, which is one reason a doc comment on an unexported function is aimed at your colleagues reading source, while a doc comment on an exported one is aimed at a stranger who will read nothing else. ## Why there is no `@param` Go's documentation is deliberately prose. There are no structured tags for parameters, returns or errors; you write sentences that name the parameters instead: *reports whether*, *returns ErrNotFound if*, *the caller must call Close*. Only a handful of conventions are machine-read at all: the leading identifier name, the small formatting syntax for headings, lists and links, and a paragraph beginning `Deprecated:`. Everything else is text for a human. ## The failure this prevents The package page is often the **only** onboarding a stranger gets — they will not open your source. Detached comments, comments inside the function body, and exported functions with nothing above them all produce the same result: a page listing signatures with no prose. When you review a change to an exported declaration, read what `go doc` prints for it rather than what the diff shows, because the diff makes a detached comment look attached.

  • What does a comment placed immediately above a `const (` group document?
    The whole group. It is shown once as the block's description, which is the right place to explain what the set of values means. Individual constants inside the parentheses can still carry their own comments, and those are displayed per value. This is the usual shape for a family of related status or option constants.
  • Do comments inside a function body ever appear in the package documentation?
    No. Only comment groups directly above a package clause, a top-level declaration, a method, a struct field or an interface method are collected. Comments inside a body are for whoever edits the code next. If an explanation matters to a caller — a precondition, an error the function returns, a resource the caller must release — it belongs above the declaration.
  • Why does Go have no `@param` or `@return` tags?
    Documentation is deliberately plain prose, so a doc comment reads as English rather than as a form. You name the parameters in sentences instead: reports whether, returns ErrNotFound when, the caller must close the returned reader. Only the leading identifier name, a small formatting syntax and a Deprecated paragraph are machine-read; the rest is text for a human.

saying these in an interview costs you the question

  • Thinks any comment near a function becomes its documentation
  • Expects a special opener such as /** to start a doc comment
  • Leaves a blank line between the comment and the declaration
  • Expects @param and @return tags to be parsed
  • Believes unexported declarations cannot carry doc comments
  • Puts the explanation a caller needs inside the function body
open as a page

What is a Go package comment, and why do large packages keep it in doc.go?

level: middleimportance: should knowfreq 50%

basics

~20 s

A package comment is the comment block immediately above a package clause, and it is the first thing a reader sees on the package's documentation page. One file should carry it; a long one usually lives in doc.go.

open as a page

Why can `go doc ./pkg` and a package's pkg.go.dev page show different documentation?

level: seniorimportance: should knowfreq 38%

basics

~20 s

go doc reads source on your machine, so it shows your working tree as it is now. pkg.go.dev renders a published module version fetched through the public proxy, so it lags your code and never shows a private module.

open as a page

A Go doc comment renders as one run-on paragraph — which doc comment syntax rules were missed?

level: middleimportance: nice to knowfreq 26%

basics

~20 s

Go doc comments use a tiny syntax: an empty comment line separates paragraphs, indented lines become a code block or list, hash-space makes a heading, brackets around a name make a link. Other markdown is literal.

open as a page