Packages, modules and tooling · 3 MIN READ
The package name is half the API
Name a package and its exported function without repeating yourself.
A package's name becomes the qualifier callers read: http.Server is clearer than http.HTTPServer. Prefer short, descriptive lowercase names. Singular names are common, but useful standard-library packages such as strings, errors and slices are plural.
Avoid needless repetition between package and exported name. chart.New can be clear for a package centered on one chart type; a package with several constructors may need more specific names. Read the whole selector to judge it.
SEE THE IDEA
The package name becomes part of every selector
- 01
Import path
goexample.com/project/cacheLocates the package
- 02
Package name
gopackage cacheSupplies the default qualifier
- 03
Public name
gocache.StoreThe two words are read together
Read exported names with their package qualifier before deciding whether they are clear.
In practice
Prefer short, specific package names that describe the responsibility. Avoid repeating the package name in every exported identifier: cache.Store is usually clearer than cache.CacheStore. An import alias resolves a local naming conflict without changing the package’s own public name.
Watch out
A vague package name such as utils tends to gather unrelated responsibilities and makes the dependency direction harder to understand.
Read and predict
package main
import (
"fmt"
"strings"
)
func main() {
// Every one of these reads as package.Identifier at the call site.
fmt.Println(strings.ToUpper("go"))
fmt.Println(strings.HasPrefix("gopher", "go"))
r := strings.NewReplacer("a", "4", "e", "3")
fmt.Println(r.Replace("beware"))
}What does this print?
Trace each value and write down your reasoning. Copy the source into GopherForge or your local Go environment to compile and check it. This web page does not execute your code or send it to a compiler service.
Progress stays in this browser. No account needed.