---
title: "Packages, modules and go test"
slug: "packages-modules-and-tests"
description: "Know what a package and a module are, what makes a name exported, how to write table-driven tests with go test and how to build a small command-line tool."
published: "2026-12-03"
updated: "2026-12-03"
canonical: "https://aifree.dev/courses/learn-go/packages-modules-and-tests"
---

# Packages, modules and go test

How do you split a Go program into packages and prove it works?

## Predict

```go
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)
		}
	}
}
```

What does this program print?

- ok for all three lines
- ok twice, then FAIL for the third case
- It does not compile, because of the anonymous struct

Answer: ok twice, then FAIL for the third case — The first two cases give 3 and 2 words. The comma stays inside a word, so the third gives 1 and not 2.

Output:

```
ok "go is fun"
ok "  spaced   out  "
FAIL "one,two"
```

Fields splits on white space only. Table tests like this one are how Go programmers check many cases in one loop.

## Packages and modules

A **package** is the Go files in one folder, all starting with the same `package` line. A **module** is a tree of packages with a `go.mod` file at the root. Create it with `go mod init example/app`. The import path of a package is the module path plus its folder, such as `example/app/count`. A name that starts with a **capital letter** is exported: other packages can use it. `go mod tidy` fetches the dependencies you import.

## The core of a small counting tool

```go
package 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))
}
```

Output:

```
lines: 2
words: 5
chars: 24
```

The functions take a string, so you can test them without files or arguments. The mission tool reads a file and calls the same kind of functions.

Playground: https://go.dev/play/p/3UyeT2S3xhd

## Tests with go test

Tests live in files that end in `_test.go`, next to the code. A test is a function `func TestXxx(t *testing.T)`. Run all of them with `go test ./...`; add `-v` to see each one. `t.Errorf` marks a failure and goes on; `t.Fatalf` stops. A **table-driven test** loops over cases and calls `t.Run(name, func(t *testing.T) {...})` for each, so a failure names its case.

## A table-driven test in count_test.go

```go
package 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)
			}
		})
	}
}
```

Output:

```
=== RUN   TestCountWords
=== RUN   TestCountWords/empty
=== RUN   TestCountWords/simple
=== RUN   TestCountWords/extra_spaces
--- PASS: TestCountWords (0.00s)
    --- PASS: TestCountWords/empty (0.00s)
    --- PASS: TestCountWords/simple (0.00s)
    --- PASS: TestCountWords/extra_spaces (0.00s)
PASS
ok  	example/app	0.00s
```

In a real project `CountWords` lives in `count.go` and the test in `count_test.go`; here both share one file. Run it with `go test -v`. The times after each test differ on every run.

Playground: https://go.dev/play/p/PK-iPK5-Cu_Q

## Fix it

```go
package main

import (
	"fmt"
	"os"
	"strings"
)

func main() {
	fmt.Println(strings.ToUpper("done"))
}
```

The program does not compile. What is wrong?

- os is imported but never used, and Go rejects unused imports
- strings.ToUpper is not exported
- An import must be on one line

Answer: os is imported but never used, and Go rejects unused imports — Remove the import, or use the package. Go keeps imports honest on purpose.

Compiler message:

```
"os" imported and not used
```

Fixed:

```go
package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.ToUpper("done"))
}
```

Output:

```
DONE
```

## Say it back

In your own words: what makes a name exported in Go?

Our sentence: A name that starts with a capital letter is exported and can be used from other packages. A name that starts with a lower-case letter stays private to its package.

## Mission (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.

```sh
mkdir wc-lite && cd wc-lite
go mod init example/wc-lite
go test ./...
go run . main.go
```

## 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.
