- Choosing descriptive package names
- Avoiding redundant identifiers
- Aligning package clauses and directory names
- Export rules for identifiers
1. Prefer descriptive package names
Choose package names that describe the responsibility or behavior the package provides. Avoid generic names likeutil, helpers, or common because they hide intent and make it harder to find functionality.
Prefer small, focused packages whose names make their purpose obvious. For example, instead of one generic util package containing unrelated helpers for name extraction and string formatting, split them into two purpose-driven packages:
extract.Names and format.Names, which is clearer than util.ExtractNames or util.FormatString.
Use focused, descriptive package names. The package name plus the identifier should read naturally at the call site (for example,
extract.Names or format.Names).2. Avoid repeating the package name inside identifiers
Do not repeat the package name inside exported identifiers. The package name already provides context at the call site, so including it in function names makes names redundant and verbose. Prefer:extract.Names(notextract.ExtractNames)format.Names(notformat.FormatString)
Names).
Avoid redundant naming like
extract.ExtractNames. Repetition makes APIs noisy and harder to use.3. Package clause and directory name conventions
Files in a single directory should normally use the same package clause. For discoverability and consistency:- Make the package name match the directory name.
- Keep all non-test files in the directory using the same
packageclause.
package main— used for executables; packages namedmainare not importable.- Test packages — tests may use
package foo_testto test the package from the external perspective.
Quick reference table
Summary
- Choose short, descriptive package names that clearly convey responsibility.
- Let package names disambiguate identifiers — avoid repeating the package name inside function names.
- Export names by starting them with an initial capital letter only when they must be visible outside the package.
- Keep the package clause consistent across files in a directory and match it to the directory name, except for valid exceptions like
mainand_testpackages.
Links and references
- Effective Go — Packages
- The Go Programming Language Specification — Packages
- Go Blog: How to Write Go Code