Go Interfaces and Table-Driven Tests
How Go interfaces decouple code implicitly (no implements keyword needed), and how to write clean table-driven tests with go test.
You'll learn how Go's implicit interfaces let you swap real dependencies for test fakes cleanly, and how to write idiomatic table-driven tests using Go's built-in testing package. Assumes familiarity with Go structs and methods.
1. Duck typing, checked by the compiler
In Java or TypeScript, a class has to announce implements Notifier before it counts as a Notifier.
In Go, interfaces are satisfied implicitly. If an interface asks for Send(msg string) error, and your struct has a Send(msg string) error method, your struct automatically satisfies the interface. No implements keyword, no ceremony.
package main
import "fmt"
type Notifier interface {
Send(msg string) error
}
type EmailNotifier struct{}
func (EmailNotifier) Send(msg string) error {
fmt.Println("Sending email:", msg)
return nil
}Any function taking n Notifier can accept EmailNotifier in production—and a tiny FakeNotifier in tests that doesn't actually email your boss at 2 a.m.
2. Writing tests with zero external libraries
Go ships with a fast test runner built right in (go test). Any file ending in _test.go is compiled only during tests, never into your production binary.
Here is a function we want to test:
package main
func ApplyDiscount(price, percent int) int {
if percent <= 0 {
return price
}
if percent >= 100 {
return 0
}
return price - (price*percent)/100
}Instead of copy-pasting five nearly identical TestDiscountZero, TestDiscountTen, TestDiscountFull functions, Go developers use table-driven tests—a slice of test cases looped with t.Run:
package main
import "testing"
func TestApplyDiscount(t *testing.T) {
cases := []struct {
name string
price int
percent int
want int
}{
{"no discount", 100, 0, 100},
{"ten percent", 100, 10, 90},
{"negative clamped", 100, -5, 100},
{"over 100 clamped", 100, 150, 0},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := ApplyDiscount(tc.price, tc.percent)
if got != tc.want {
t.Errorf("ApplyDiscount(%d, %d) = %d; want %d", tc.price, tc.percent, got, tc.want)
}
})
}
}Run it with go test -v ./...:
=== RUN TestApplyDiscount
=== RUN TestApplyDiscount/no_discount
=== RUN TestApplyDiscount/ten_percent
=== RUN TestApplyDiscount/negative_clamped
=== RUN TestApplyDiscount/over_100_clamped
--- PASS: TestApplyDiscount (0.00s)
PASSBreak "ten percent" by changing want: 90 to want: 85 and run go test ./... to see the exact failing subtest name and diff. Change it back to 90.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
Test function named testDiscount (lowercase) | go test silently skips it | Start every test function with Test... (func TestX(t *testing.T)) |
| Huge 10-method interfaces | Hard to implement and mock | Keep interfaces tiny—1 to 3 methods max |
Practice
- What suffix must a Go test file have so
go testfinds it andgo buildexcludes it?
Answer: _test.go (e.g., discount_test.go).
What you learned
- Go interfaces are satisfied implicitly—any type with the right methods fits.
- Keep interfaces small and pass them into functions to make testing painless.
- Table-driven tests (
[]struct{...}+t.Run) keep test suites compact and easy to extend.
Video walkthrough
YouTubeClick play to load video (YouTube)