Introduction
Every Go source file belongs to a package, declared with a package clause at the top of the file. Packages are the primary unit of code organization and reuse in Go: they group related functionality, control what is visible to other code, and form the building blocks that Go Modules assemble into dependency graphs. An executable program is built from a package main containing a func main() entry point, while reusable libraries use any other package name.
Cricket analogy: Like every player belonging to a specific team roster declared at signing, the national XI (package main) must have a designated captain (func main()) to take the field, while domestic squads (library packages) exist to be drawn upon.
Syntax
// file: mathutil/add.go
package mathutil
// Add is exported (capitalized) so other packages can call it.
func Add(a, b int) int {
return a + b
}
// subtract is unexported; only visible inside package mathutil.
func subtract(a, b int) int {
return a - b
}Explanation
Go's export rule is based purely on capitalization: identifiers (functions, types, variables, constants, struct fields) starting with an uppercase letter are exported and accessible from other packages; lowercase identifiers are unexported and stay private to their package. A module is one or more packages tracked together with a go.mod file, which records the module's import path and its dependencies. Running go mod init example.com/myapp creates the go.mod file, go get package@version adds or upgrades a dependency, and go mod tidy adds missing requirements and removes unused ones. A companion go.sum file stores cryptographic checksums of dependency contents to ensure reproducible, tamper-evident builds. Modules replaced the older GOPATH-based workflow starting in Go 1.11, removing the requirement that code live in a specific workspace directory.
Cricket analogy: Like a team's official squad list where capitalized names (exported) are eligible for international selection while lowercase reserve names stay domestic-only, and a signed contract (go.mod) records the roster and imported player agreements, with a verification ledger (go.sum) confirming no tampering.
Example
// go.mod
module example.com/greeter
go 1.21
require github.com/google/uuid v1.3.0
// file: main.go
package main
import (
"fmt"
"example.com/greeter/mathutil"
)
func main() {
sum := mathutil.Add(2, 3)
fmt.Println("2 + 3 =", sum)
}Output
$ go mod init example.com/greeter
$ go mod tidy
$ go run main.go
2 + 3 = 5Key Takeaways
- Every .go file must start with a package declaration; package main plus func main() defines an executable's entry point.
- Capitalized identifiers are exported (public); lowercase identifiers are unexported (package-private).
- go.mod defines the module path, Go version, and dependency requirements; go.sum stores checksums for verification.
- go mod init creates a new module; go get adds/upgrades dependencies; go mod tidy synchronizes go.mod with actual imports.
- Modules (Go 1.11+) replaced GOPATH, letting projects live anywhere on disk and use semantic-versioned dependencies.
Practice what you learned
1. Which file defines a Go module's path and its dependencies?
2. What determines whether an identifier is exported from a Go package?
3. Which command adds missing module requirements and removes unused ones based on actual imports?
4. What is the purpose of go.sum?
5. What must a package have to be compiled into a runnable executable?
Was this page helpful?
You May Also Like
Testing in Go
Write and run automated tests in Go using the built-in testing package, table-driven tests, and benchmarks.
Go Tooling: gofmt, go vet, and go build
Explore Go's built-in tools for formatting, static analysis, compiling, and running programs consistently.
Introduction to Go Programming
A first look at Go, a statically typed, compiled language built for simplicity, speed, and concurrency.
Setting Up a Go Environment
Step-by-step guide to installing Go, understanding modules, and running your first program.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics