Chapter 13: Go Modules, Testing & Benchmarking
Chapter 13: Go Modules, Testing & Benchmarking
In this chapter, you’ll learn how to manage dependencies with Go modules and ensure your code works correctly using Go’s built-in testing tools. We’ll also explore benchmarking so you can measure performance and optimize your programs effectively.

What You Will Learn
By the end of this chapter, you will be able to:
- Write unit tests using Go’s built-in
testingpackage - Use table-driven testing, Go’s recommended style
- Mock dependencies and interfaces for isolated tests
- Measure and check test coverage
- Write and run benchmarks using
go test -bench - Use pprof for CPU and memory profiling
- Debug Go applications using real debugging tools
Testing in Go is built into the language — no separate package managers or test runners required.
13.1 Writing Tests with the testing Package
A test file must:
- End with
_test.go - Use functions with signature:
func TestSomething(t *testing.T)
Example:
// math.go
package mathutils
func Add(a, b int) int {
return a + b
}
Test file:
// math_test.go
package mathutils
import "testing"
func TestAdd(t *testing.T) {
got := Add(2, 3)
want := 5
if got != want {
t.Errorf("Add(2,3) = %d; want %d", got, want)
}
}
Run tests:
go test
🧮 13.2 Table-Driven Tests
Go strongly encourages reusable patterns through tables.
func TestAdd(t *testing.T) {
tests := []struct {
a, b int
want int
}{
{1, 1, 2},
{5, 3, 8},
{-1, 1, 0},
}
for _, tt := range tests {
got := Add(tt.a, tt.b)
if got != tt.want {
t.Errorf("Add(%d,%d) = %d; want %d", tt.a, tt.b, got, tt.want)
}
}
}
This approach scales well for APIs, validation, logic, and edge cases.
13.3 Mocking Techniques
Go does not ship a mocking library — it encourages using:
- Interfaces
- Dependency injection
Example dependency:
type Storage interface {
Save(data string) error
Mock version:
type MockStorage struct {
Called bool
}
func (m *MockStorage) Save(data string) error {
m.Called = true
return nil
}
Test using mock:
func TestSave(t *testing.T) {
mock := &MockStorage{}
service := NewService(mock)
service.Store("hello")
if !mock.Called {
t.Error("expected Storage.Save to be called")
}
}
External mocking frameworks (optional): gomock, testify.
13.4 Test Coverage
Check what percentage of your code is tested:
go test -cover
Generate detailed HTML report:
go test -coverprofile=coverage.out
go tool cover -html=coverage.out
Opens a visual view showing which lines are tested or missing tests.
13.5 Benchmarks (go test -bench)
Benchmark functions must start with:
func BenchmarkXxx(b *testing.B)
Example:
func BenchmarkAdd(b *testing.B) {
for i := 0; i < b.N; i++ {
Add(10, 20)
}
}
Run benchmarks:
go test -bench=.
Output example:
BenchmarkAdd-8 200000000 6.20 ns/op
Meaning: 6.2 nanoseconds per operation.
13.6 Profiling with pprof
Profiling helps find:
- Slow functions
- Memory leaks
- Hot loops
Run with profiling:
go test -bench=. -cpuprofile cpu.out
Analyze:
go tool pprof cpu.out
Common commands inside pprof:
CommandPurposetopShow most expensive callswebGenerate graph (SVG)list funcnameShow breakdown in code
13.7 Debugging Tools
Go includes built-in debugging support through Delve (dlv).
Install (if needed):
go install github.com/go-delve/delve/cmd/dlv@latest
Debug a program:
dlv debug main.go
Useful commands:
| Command | Action |
| ---------- | ---------------- |
| b <line> | Set breakpoint |
| n | Next line |
| s | Step into |
| c | Continue |
| print x | Inspect variable |
Delve integrates with VS Code & JetBrains GoLand.
Summary of Chapter 13
In this chapter you learned:
✔ How to write tests using the testing package
✔ Table-driven test patterns (Go standard style)
✔ Mocking using interfaces and test doubles
✔ How to measure coverage and generate reports
✔ How to benchmark code using Go’s built-in tools
✔ How to profile CPU and memory usage with pprof
✔ How to debug code using Delve
메타데이터
- post_id
- 78d08fc4becd
- slug
- chapter-13-go-modules-testing-benchmarking-78d08fc4becd
- url
- https://medium.com/@imadityarathore/chapter-13-go-modules-testing-benchmarking-78d08fc4becd
- canonical_url
- https://medium.com/@imadityarathore/chapter-13-go-modules-testing-benchmarking-78d08fc4becd
- author_url
- https://medium.com/@imadityarathore
- status
- ok
- fetched_at
- 2026-08-11 23:29:03