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

  1. 01

    Import path

    go
    example.com/project/cache

    Locates the package

  2. 02

    Package name

    go
    package cache

    Supplies the default qualifier

  3. 03

    Public name

    go
    cache.Store

    The 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.

Official reference ↗

Read and predict

go
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?

Try it yourself

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.