Go organises code into packages. A file starts with a package clause that names its package, and an import declaration makes the exported identifiers of other packages available by a qualified name such as fmt.Println. The import path identifies where the package lives, and the package name is what you write in code.
Imports and package names look simple, but module paths, aliases, blank and dot imports, internal directories and import cycles all affect how a real project is laid out. This article covers each of them for current Go modules.
Every Go file begins with a package clause. All files in one directory must use the same package name (test files may add a _test suffix). The only special name is main, which together with a func main() produces an executable.
Visibility is decided only by the first letter of the identifier: upper case is exported, anything else is private to the package. By convention package names are short, lower case, a single word and without underscores, such as http, bytes or greeter. Avoid names like util or common, which say nothing about the content.
An import declaration lists the import paths that the file uses. Group them in one parenthesised block:
Go enforces two rules at compile time: an imported package must be used, and every identifier must come from a package that is imported in that file. An unused import is a compile error ("os" imported and not used), which keeps dependencies honest. Run gofmt or goimports to sort and clean the block, with standard library paths first by convention.
Since Go modules became the default (Go 1.16 and later), an import path is the module path from go.mod plus the directory of the package inside the module:
The last element of the path is usually, but not always, the package name. For a path such as math/rand/v2 the package name is rand, not v2. Module versions v2 and above add a major version suffix to the module path. To add a dependency, import it and run go mod tidy, which updates go.mod and go.sum. You can also use go get module@version.
You can change the name used for an imported package.
| Form | Example | Use |
|---|---|---|
| Alias | import str "strings" | Resolve a name clash or shorten a long name |
| Blank import | import _ "image/png" | Run the package's init for its side effects, such as registering a decoder or a database driver |
| Dot import | import . "math" | Put exported names in the file scope. Discouraged except in some tests |
The alias mrand lets two packages that are both named rand live in one file. Use blank imports sparingly and document why they are there, for example import _ "github.com/lib/pq" to register a SQL driver.
When a package is imported, Go initialises its dependencies first, then its package-level variables, then runs every init() function in the order the files were presented to the compiler. Each package is initialised once, no matter how many packages import it.
Import cycles are forbidden: if package a imports b, then b cannot import a, directly or indirectly. The compiler reports import cycle not allowed. The usual fixes are to move the shared types into a third package, or to define a small interface in the consumer package so it no longer needs to import the provider.
A directory named internal can only be imported by code rooted at the parent of internal. This gives you encapsulation that the compiler enforces:
Put application code under internal/ so other modules cannot depend on it, keep main packages in cmd/, and export only what you intend to support. Split packages by responsibility, not by type, so orders holds order logic rather than a generic models package.
No, but it should, because it avoids confusion. The directory forms the import path and the package clause forms the name you use in code. The main package and versioned paths such as /v2 are the usual exceptions.
Go treats unused imports as mistakes to keep builds fast and dependency lists accurate. If you need an import only for its side effects, use a blank import _.
The import path is the string in quotes and locates the package, for example net/http. The package name is the identifier declared in the file, for example http, and it is the prefix you type in code.
Set GOPRIVATE to the module prefix so Go skips the public proxy and checksum database, and make sure your Git credentials work for that host.
A package clause names the package, and exported names start with an upper-case letter. Imports use the module path plus a directory, must all be used, and can be aliased, blank or dot imported. Avoid import cycles with a third package or an interface, and use internal/ and cmd/ to keep a project's boundaries clear.