Go Packages
Every Go file belongs to a package. Packages are the unit of code organization and reuse. The naming convention is simple: package name = directory name, lowercase, no underscores.
// File: mathutil/calc.go
package mathutil
// Exported (uppercase) - accessible from other packages
func Add(a, b int) int { return a + b }
// Unexported (lowercase) - private to this package
func helper() int { return 42 }
Key Takeaway: Uppercase = exported (public). Lowercase = unexported (private to package). This is Go's only visibility mechanism — no public/private/protected keywords.
Go Modules
# Initialize a new module
go mod init github.com/yourname/myproject
# Add dependencies (auto-detected from imports)
go mod tidy
# Download dependencies
go mod download
# View dependency graph
go mod graph
Essential Standard Library
| Package | Purpose |
|---|---|
fmt | Formatted I/O (Printf, Println, Sprintf) |
strings | String manipulation |
strconv | String/number conversion |
os | OS functions, env vars, files |
io | I/O primitives (Reader, Writer) |
net/http | HTTP client and server |
encoding/json | JSON marshal/unmarshal |
sync | Mutex, WaitGroup, Once |
context | Cancellation and deadlines |
testing | Unit testing and benchmarks |
Practice Exercises
Medium Build a Mini Project
Combine concepts from this tutorial to build a small utility or tool.
Medium Debug Challenge
Introduce a bug in one of the code examples and practice finding and fixing it.
Hard Refactoring Exercise
Rewrite one example using a different approach and compare the tradeoffs.