Packages in Go

How Go packages map to folders, how exported names work, and how the internal directory restricts imports.

Packages are the unit of code organization in Go. Each folder is a package, capital letters decide what it exposes, and internal decides who may import it.

Packages and Imports

import "example.com/shop/price"
fmt.Println(price.WithTax(100))

Import paths are the module path plus the folder. Unused imports and import cycles are compile errors.

Exported and Unexported Names

Capitalized names are exported, lowercase names are private to the package. encoding/json ignores lowercase fields for the same reason.

The internal Directory

Code under internal/ can only be imported from inside its parent folder.

use of internal package not allowed:

This error means code outside the parent of an internal folder tried to import it.

Summary

One folder per package, exported names for the API, and internal for everything that is not meant for outside use.

Packages and Imports in Go

How Go packages map to folders, how import paths are built from the module path, import aliases, blank imports, and the imported and not used error.

Exported and Unexported Names in Go

How Go uses capital letters for public and private names, what exported means for structs, fields and methods, and the errors you see when you get it wrong.

The internal Directory in Go

How Go's internal directory restricts which packages can import code, the use of internal package not allowed error, and when to use internal instead of pkg.