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?

  1. 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)
    		}
    	}
    }
    
    What does this program print?
  2. 2Read

    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.

  3. 3Code

    The core of a small counting tool

    1 / 2
    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))
    }
    

    Read the program. What will it print?

    Run it in the playground

  4. 4Read

    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.

  5. 5Code

    A table-driven test in count_test.go

    1 / 2
    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)
    			}
    		})
    	}
    }
    

    Read the program. What will it print?

    Run it in the playground

  6. 6Fix

    package main
    
    import (
    	"fmt"
    	"os"
    	"strings"
    )
    
    func main() {
    	fmt.Println(strings.ToUpper("done"))
    }
    
    The program does not compile. What is wrong?
  7. 7Say it back

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

  1. AgentRouter Free Credits

    Free AI coding credits: $25 per daily check-in and $50 per invite

    No cardVerified

  2. Cursor Free Plan

    Cursor Agent with limited agent and chat requests, $0/month

    No cardVerified

  3. Cline Free Models

    Try select models free in Cline via rotating promotions with a usage quota

    No cardVerified

All free ai coding tools →

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

Measured with go1.27.4 darwin/arm64 on 2026-12-03

Published Dec 3, 2026 · Updated Dec 3, 2026

Never miss new offers and news

New tools, credits, and AI news are added regularly. Pick how you want to hear about them.

Telegram channel

Join @aifree_dev and get new free AI offers and news right in your Telegram feed.

Browser notifications

Turn on push notifications and get an alert on this device whenever something new is posted. No account needed.

Email newsletter

A weekly digest of new, changed and expiring free AI offers. Double opt-in, one-click unsubscribe. See the latest issue.

Unsubscribe anytime. Privacy

Frequently asked questions

What is aifree.dev?

aifree.dev helps everyone access AI through a curated directory of free AI tools, API credits, free tiers, and startup programs for coding, image, voice, video, and students - verified regularly.

How are offers presented?

Each offer includes its access details, free models when available, and expiration information when provided. Limited-time deals stay visible so they are easy to compare.

Do education and startup programs need verification?

Yes. Providers usually ask students to verify their status with a school email, student ID, or a program like the GitHub Student Pack, and ask startups to verify company details such as age, funding, or website. Check each offer’s claim page for the exact requirements.

What kinds of free AI offers are listed?

Free tiers and free trials from AI providers, free API credits, startup and student programs, and free tools for coding, image generation, voice, and video.

How can I suggest a tool or report a broken link?

Use the submit form: paste the offer page URL and a short note. A person checks every submission before anything is listed. Broken links and changed terms go through the same form.

Know a good one?

Tell us about an AI tool worth knowing. Found a broken link? We want to hear that too.

Submit an offer