Package and import semantics
Package clauses and import declarations are language semantics, not just style; they define names, initialization dependencies, and file cohesion.
Canonical guidance
- each source file has a package clause
- import declarations bind package names into file scope
- blank and dot imports have specific semantics and should stay rare
Use when
- reviewing import forms
- explaining package-scope names
- diagnosing unused or conflicting imports
Avoid
- dot imports in ordinary production code
- blank imports without a clear registration or side-effect reason
- treating package naming as unrelated to import readability
Preferred pattern
import (
"context"
"net/http"
)
Anti-pattern
- keeping a blank import after the side effect it once needed is gone
Explanation: This is tempting because the build still passes for a while, but blank imports should represent deliberate package initialization dependencies.
Why
- import and package rules shape names, initialization order, and readability before higher-level style even begins
Related pages
Sources
- The Go Programming Language Specification - Go Team
- Effective Go - Go Team
- Google Go Style Decisions - Google
- Google Go Best Practices - Google