Managing Dependencies and internal/ Packages
Organize multi-package Go modules with cmd/, internal/, go get, and go mod tidy. After this, private helpers stay isolated from external imports.
Course content
Sign in to save your progress.
-
Before You Write Code
-
Why Go Exists and When to Pick It
-
Run Your First Go Program
-
-
-
Values, Control Flow, and Functions
-
Variables, Types, and Zero Values
-
If, For Loops, and Multiple Return Values
-
-
-
Slices, Maps, and Structs
-
Arrays, Slices, and Why append Matters
-
Maps, Structs, and Pointer Receivers
-
-
-
Errors, Packages, and Testing
-
Handle Errors Without Hiding Them
-
Packages, Exports, and go test
-
-
-
Files, JSON, and a Small HTTP Server
-
Read Files and Decode JSON Structs
-
Build and Test a Small net/http Server
-
-
-
Pointers, Modules, and CLIs
-
Pointers and Memory Basics
-
Managing Dependencies and internal/ Packages
-
Building CLIs With flag, os.Getenv, and Exit Codes
-
-
Once a Go repository grows beyond a single main.go file, two problems show up immediately: keeping third-party dependencies reproducible across every teammate and CI runner, and stopping internal helper packages from becoming accidental public APIs that outside projects start importing.
Go solves both directly in the compiler and module toolchain: go.mod + go.sum lock versions and cryptographic checksums, while the special internal/ directory enforces private package visibility at compile time.
Skip ahead
If you already structure repos with cmd/ and internal/, manage dependencies with go get and go mod tidy, and avoid package import cycles, continue to "Building CLIs With flag, os.Getenv, and Exit Codes."
Experiment 1: Standard layout with cmd/ and internal/
Suppose you are building an order service with a money-formatting helper that should only be used inside your own repository:
ordersvc/
├── go.mod
├── cmd/
│ └── api/
│ └── main.go # entrypoint: allowed to import internal/money
└── internal/
└── money/
└── cents.go # private to module example.com/ordersvc
// 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
Experiment 2: What happens when an outside module tries to import internal/
Notice that internal/ is not just a naming convention—the Go compiler enforces it strictly. Any package inside a directory named internal can only be imported by code rooted at the parent of that internal directory. If a second repository tries to import example.com/ordersvc/internal/money, go build refuses immediately with:
use of internal package example.com/ordersvc/internal/money not allowed
That gives you freedom to rename functions, change struct layouts, or refactor packages under internal/ anytime without breaking external callers—while every binary inside your own repo (cmd/api, cmd/worker) can still share them freely.
Experiment 3: Keeping go.mod and go.sum clean
When you add or remove external libraries, three commands keep your build reproducible:
# Add or pin a specific release version:
$ go get golang.org/x/sync@v0.10.0
# Sync go.mod and go.sum with actual imports (adds missing, drops unused):
$ go mod tidy
# Verify local downloaded modules match go.sum SHA-256 hashes:
$ go mod verify
Commit both go.mod and go.sum to Git. go.mod records the minimum required version of each dependency; go.sum stores cryptographic checksums so nobody can tamper with a downloaded package silently between builds.
Experiment 4: Avoiding import cycles
Go strictly forbids circular imports: if internal/orders imports internal/users and internal/users imports internal/orders, the compiler refuses to build with import cycle not allowed. Whenever you hit a cycle, break it in one of two ways: move the shared domain types into a lower-level package imported by both, or define a small 1-method interface inside the consumer package instead of importing the concrete provider.
Common mistakes
- Deleting or gitignoring
go.sum: Strips cryptographic verification from CI and teammate builds. Always commitgo.sum. - Putting every package at the repository root: Turns internal database helpers into an accidental public API. Put application packages in
internal/by default. - Forgetting
/v2on major versions v2+: Go modules use semantic import versioning—importing version 2 of a library requires.../v2in the import path.
💡 Tip
Run go list -m all to audit every direct and transitive module version your binary actually compiles with, and run go mod why <pkg> when you want to see which dependency pulled in an unfamiliar package.
What happens if you skip this step
Without internal/ and go mod tidy, stale dependencies pile up in go.mod, clean CI builds fail on missing go.sum entries, and external code binds to private helpers you never meant to support.
You have two binaries in one repository—cmd/api and cmd/worker—that both need shared SQL helpers, and you do not want outside repositories importing them. Which directory should those SQL helpers live in?
About
Organize multi-package Go modules with cmd/, internal/, go get, and go mod tidy. After this, private helpers stay isolated from external imports.
- Duration
- 5 min
- Last update
- 11/27/2026
Comments
Sign in to commentNo comments yet.