Go Packages & Modules
As programs grow, you split code into packages, and packages live inside a module. Understanding the two is essential for any Go project bigger than a single file.
| Term | What it is | Defined by |
|---|---|---|
| Package | a folder of .go files that are compiled together |
package name at the top of each file |
| Module | a collection of packages versioned together | a go.mod file in the root folder |
| Import path | how you refer to a package | module path + folder, e.g. example.com/shop/cart |
Packages
Every .go file starts with a package clause. All files in one folder must use the same package name, and they can freely use each other's names — even unexported ones. The package main is special: it builds into an executable.
Identifiers are shared across packages only if they are exported (start with a capital letter):
package cart
// Item is exported: other packages can use cart.Item.
type Item struct {
Name string
Price float64
}
// Total is exported.
func Total(items []Item) float64 {
sum := 0.0
for _, it := range items {
sum += it.Price
}
return round(sum)
}
// round is unexported: only code inside package cart can call it.
func round(x float64) float64 {
return float64(int(x*100+0.5)) / 100
}A typical module layout
shop/
├── go.mod module example.com/shop
├── main.go package main
├── cart/
│ ├── cart.go package cart
│ └── cart_test.go package cart (tests)
└── internal/
└── pricing/
└── tax.go package pricing (only importable inside shop)The go.mod file at the root:
module example.com/shop
go 1.25.0And main.go imports the cart package by its full import path:
package main
import (
"fmt"
"example.com/shop/cart"
)
func main() {
items := []cart.Item{{Name: "Pen", Price: 1.2}, {Name: "Pad", Price: 3.45}}
fmt.Println("Total:", cart.Total(items))
}go run .Total: 4.65
Note: A folder named
internalis special: packages under it can only be imported by code rooted at the parent ofinternal. Use it for code you don't want other modules to depend on.
Simulating packages in one file
The playground runs a single file, but you can still see how exported names, unexported helpers and package-level state behave. Everything here lives in package main:
package main
import "fmt"
// Package-level variable, initialised before main runs.
var taxRate = 0.2
// init functions run automatically after package variables are set.
func init() {
fmt.Println("init: tax rate is", taxRate)
}
// Exported style name (would be visible to other packages).
func PriceWithTax(net float64) float64 {
return roundCents(net * (1 + taxRate))
}
// unexported helper
func roundCents(x float64) float64 {
return float64(int(x*100+0.5)) / 100
}
func main() {
fmt.Println("main starts")
fmt.Println(PriceWithTax(9.99))
}init: tax rate is 0.2
main starts
11.99
Each package may have one or more init() functions. They run once, in dependency order, before main — useful for setting up lookup tables, but avoid heavy work there.
Import forms
import (
"fmt" // normal: use as fmt.Println
str "strings" // alias: use as str.ToUpper
_ "image/png" // blank: run the package's init only
"example.com/shop/cart" // a package from your own module
"github.com/google/uuid" // a third-party package
)Standard-library imports come first, then a blank line, then other modules — goimports arranges this for you.
Adding a third-party dependency
go get github.com/google/uuid@v1.6.0go: downloading github.com/google/uuid v1.6.0
go: added github.com/google/uuid v1.6.0
go get records the dependency in go.mod and writes checksums to go.sum. Commit both files. Running go mod tidy afterwards removes anything you no longer import.
module example.com/shop
go 1.25.0
require github.com/google/uuid v1.6.0Package names and design
- Short, lower-case, single word:
cart,http,strconv— no underscores or camelCase. - The name is part of every call, so avoid stutter:
cart.Total, notcart.CartTotal. - Don't create grab-bag packages named
utilorcommon; name packages after what they provide.
Package-level names are shared across files
package main
import (
"fmt"
"strings"
)
// Imagine this block lives in file greet.go...
const defaultName = "friend"
func greeting(name string) string {
if strings.TrimSpace(name) == "" {
name = defaultName
}
return "Hello, " + name + "!"
}
// ...and this one in main.go. Same package, so no import is needed.
func main() {
fmt.Println(greeting("Ola"))
fmt.Println(greeting(" "))
}Hello, Ola!
Hello, friend!
Test yourself
Which file defines a Go module?
package.jsongo.modmain.gomodule.go
Function parse is in package config. Can main call config.parse()?
- Yes
- No, lower-case names are unexported
- Only with an alias
- Only in tests
When do init() functions run?
- When you call them
- Automatically, before
main - After
mainreturns - Only during
go test