Organize Go Modules With go mod tidy and internal/ Packages
Structure a multi-package Go repository with cmd/, internal/, go get, and go mod tidy. After this, private packages cannot be imported from outside your module.
Skip ahead
Already know go mod tidy? Jump to Case 2: how internal/ blocks external imports at compile time or Case 4: breaking import cycles.
Keeping an entire Go application in one flat folder works for a 100-line script, then falls apart as soon as you add a second binary (cmd/api and cmd/worker) or publish a module on GitHub. Without clear boundaries, external code starts importing unstable helper packages you never meant to support, and stale entries pile up in go.mod until clean CI builds fail.
Go solves both directly in the compiler and toolchain: the compiler-enforced internal/ directory hides private packages from external modules, and go mod tidy + go.sum keep dependencies reproducible and cryptographically verified.
Case 1: Standard layout with cmd/ and internal/
Place runnable binaries under cmd/<app>/main.go and private shared code under internal/. Both cmd/api and cmd/worker inside example.com/ordersvc can import internal/money freely:
ordersvc/
├── go.mod
├── cmd/
│ └── api/
│ └── main.go # allowed: shares module root example.com/ordersvc
└── internal/
└── money/
└── cents.go # blocked from any outside repository
// internal/money/cents.go
package money
import "fmt"
func FormatUSD(cents int64) string {
return fmt.Sprintf("$%d.%02d", cents/100, cents%100)
}
// cmd/api/main.go
package main
import (
"fmt"
"example.com/ordersvc/internal/money"
)
func main() {
fmt.Println(money.FormatUSD(525))
}
$ go mod tidy && go run ./cmd/api
$5.25
Case 2: How the Go compiler enforces internal/
Unlike Python's leading-underscore convention (which external callers can still ignore), Go's internal/ rule is enforced by go build itself. Any package inside a directory named internal can only be imported by packages sharing the parent directory immediately above internal. If another repository tries to import example.com/ordersvc/internal/money, compilation halts immediately:
use of internal package example.com/ordersvc/internal/money not allowed
You can even nest internal/ deeper (for example internal/billing/internal/tax) when you want a sub-package visible only to billing and hidden from the rest of your own repo.
Case 3: Dependency hygiene with go get, go mod tidy, and go.sum
Two files live at your module root and should both be committed to Git:
go.mod: Declares your module path, Go version, and minimum required versions of every dependency (Go uses Minimal Version Selection, so builds stay deterministic without a separate resolver lockfile).go.sum: Records SHA-256 cryptographic hashes of every downloaded module zip andgo.modso nobody can swap dependency source code silently.
# Pin or upgrade a specific dependency version:
$ go get golang.org/x/sync@v0.10.0
# Sync go.mod and go.sum with actual imports (adds missing, prunes unused):
$ go mod tidy
# Audit every direct and transitive module in the build:
$ go list -m all
# Verify local module cache matches go.sum hashes:
$ go mod verify
Case 4: Breaking circular imports (import cycle not allowed)
Go strictly forbids package A importing package B while package B imports package A. If internal/orders and internal/users try to import each other, break the cycle cleanly in one of two ways:
- Move shared domain structs (like
UserIDorMoney) into a small lower-level package imported by both. - Define a tiny 1-method interface inside the consumer package instead of importing the other concrete package directly.
💡 Tip
Run go mod why -m <module> whenever an unfamiliar transitive dependency appears in go.mod—it prints the exact import chain from your package to that module.
Common mistakes
- Adding
go.sumto.gitignore: Removes cryptographic checksum verification from CI and teammates' machines. Always commit bothgo.modandgo.sum. - Forgetting
/v2on major versions v2+: Go modules use semantic import versioning—publishing or importing v2 of a module requires/v2at the end of the module path. - Creating a
pkg/folder when everything is private: If your repo is an application rather than a shared library, keep packages underinternal/so you can refactor freely without breaking external callers.
⚠ Warning
Do not run go get -u ./... blindly right before a release—that upgrades every direct and transitive dependency to its latest minor/patch release at once. Upgrade specific modules deliberately and run go test -race ./... afterward.
What happens if you skip this
Without internal/ and go mod tidy, clean CI builds fail on missing or stale go.mod/go.sum entries, and internal helpers leak into public APIs that become painful to refactor.
Related course: Go for Beginners.
About
Structure a multi-package Go repository with cmd/, internal/, go get, and go mod tidy. After this, private packages cannot be imported from outside your module.
- Responsible
- Jay P
- Last Update
- 11/27/2026
- Reading Time
- 4 min
Reviews
Sign in to write a reviewComments
Sign in to commentNo comments yet.