Skip to content
public-library›Gotechs/go

Separate package documentation from file headers

Sign in to star Separate package documentation from file headers, 0 stars

Sign in to star rules

Star this rule if you find it useful.

Add to cart

What would you like to add?

At checkout, you'll pick which project these go into, and you can fork a rule instead of staying in sync.

When to apply

Before writing, changing, or reviewing a comment at the top of a Go file, above the package clause, or a package's doc.go.

Attach a comment directly to the package clause only when it documents the whole package, and start it with Package <name>. Separate a comment about one file from the package clause with a blank line.

Implementation

  • For package documentation, put the comment directly above package, starting with Package <name> ..., in doc.go or the package's central file. A main package is the exception: its comment describes the command, so it may start with Command <name> ... or another sentence about the program.
  • For a file header, leave exactly one blank line between the comment and package, and do not start it with Package <name>.
  • Keep package documentation in one file per package.
  • When there is no package-level documentation to add, write a file header rather than an accidental package doc.

Rationale

Go treats a comment immediately before the package clause as package documentation, shown by go doc and pkg.go.dev. A comment about one file attached there becomes the package's description, and if several files do it, their comments are combined. A package comment separated by a blank line is not documentation at all.

Examples

Application: A file header

Incorrect (counterexample):

// Mock fixtures as JSON, for local sample data only; production code never imports them.
package devseed

The comment describes one file, but Go treats it as the package's documentation.

Correct:

// Mock fixtures as JSON, for local sample data only; production code never imports them.

package devseed

Application: Package documentation

Incorrect (counterexample):

// Package devseed builds the sample data used in local development.

package devseed

The blank line detaches the package comment, so go doc shows none.

Correct:

// Package devseed builds the sample data used in local development.
package devseed

Validation

Run go doc ./path/to/package and check that it shows the intended package description and no file-specific text. Search for comments attached to package that do not start with Package <name>, except command documentation on package main.

A file header separated from package by a blank line is not a violation.