Testing in Go is built right into the standard library with the package testing. It’s simple, convention-driven, and designed to run with the Go toolchain. Here’s how it works:


1. Test Files Naming


2. Test Functions

package math

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

3. Running Tests

Run inside your project/module with:

go test ./...

or in a single package:

go test

With verbose output:

go test -v

4. Table-Driven Tests (common Go pattern)