Packages, modules and go test
Learn Go · Beginner · 15 min · After Context in Go: timeouts and cancellation
How do you split a Go program into packages and prove it works?
1Guess
package main import ( "fmt" "strings" ) func countWords(s string) int { return len(strings.Fields(s)) } func main() { cases := []struct { in string want int }{ {"go is fun", 3}, {" spaced out ", 2}, {"one,two", 2}, } for _, c := range cases { if countWords(c.in) == c.want { fmt.Printf("ok %q\n", c.in) } else { fmt.Printf("FAIL %q\n", c.in) } } }2Read
Packages and modules
A package is the Go files in one folder, all starting with the same
packageline. A module is a tree of packages with ago.modfile at the root. Create it withgo mod init example/app. The import path of a package is the module path plus its folder, such asexample/app/count. A name that starts with a capital letter is exported: other packages can use it.go mod tidyfetches the dependencies you import.3Code
The core of a small counting tool
1 / 2package main import ( "fmt" "strings" "unicode/utf8" ) const text = "one two\nthree four five\n" func countWords(s string) int { return len(strings.Fields(s)) } func countLines(s string) int { return strings.Count(s, "\n") } func main() { fmt.Println("lines:", countLines(text)) fmt.Println("words:", countWords(text)) fmt.Println("chars:", utf8.RuneCountInString(text)) }Read the program. What will it print?
4Read
Tests with go test
Tests live in files that end in
_test.go, next to the code. A test is a functionfunc TestXxx(t *testing.T). Run all of them withgo test ./...; add-vto see each one.t.Errorfmarks a failure and goes on;t.Fatalfstops. A table-driven test loops over cases and callst.Run(name, func(t *testing.T) {...})for each, so a failure names its case.5Code
A table-driven test in count_test.go
1 / 2package app import ( "strings" "testing" ) func CountWords(s string) int { return len(strings.Fields(s)) } func TestCountWords(t *testing.T) { cases := []struct { name string in string want int }{ {"empty", "", 0}, {"simple", "go is fun", 3}, {"extra spaces", " a b ", 2}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { if got := CountWords(c.in); got != c.want { t.Errorf("CountWords(%q) = %d, want %d", c.in, got, c.want) } }) } }Read the program. What will it print?
6Fix
package main import ( "fmt" "os" "strings" ) func main() { fmt.Println(strings.ToUpper("done")) }7Say it back
8Mission · 20 min
Build wc-lite: a module example/wc-lite with a package count (count.go with Words and Lines, and count_test.go with a table test) and a main.go that reads the file named in os.Args[1] and prints its lines, words and bytes.
mkdir wc-lite && cd wc-lite go mod init example/wc-lite go test ./... go run . main.go
Free AI coding tools to practise with
- AgentRouter Free Credits
Free AI coding credits: $25 per daily check-in and $50 per invite
No cardVerified
- Cline Free Models
Try select models free in Cline via rotating promotions with a usage quota
No cardVerified
Common questions
- What is the difference between a package and a module in Go?
- A package is a folder of Go files that share one package name; it is the unit of code you import. A module is a collection of packages released together, described by a go.mod file at its root. One module can hold many packages. The import path of a package is the module path followed by its folder.
- How does Go find test functions?
- The go test command looks at files whose names end in _test.go in the package folder. In them it runs every function named Test followed by an upper-case letter or digit, with one parameter of type *testing.T. These files are not part of a normal build, so they never end up in your program. Use go test ./... to run every package.
- What does go mod tidy do?
- It reads your imports and brings go.mod and go.sum in line with them. It adds modules your code needs, downloading them if necessary, and removes requirements that nothing uses any more. Run it after you add or delete an import of an outside package. For code that only uses the standard library, there is nothing for it to fetch.
Sources
- https://go.dev/doc/tutorial/add-a-test (read 2026-12-03)
- https://pkg.go.dev/testing (read 2026-12-03)
- https://go.dev/doc/modules/layout (read 2026-12-03)
Measured with go1.27.4 darwin/arm64 on 2026-12-03
Published Dec 3, 2026 · Updated Dec 3, 2026