By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A bug is a question: the program did that, and you expected this. You do not need a flash of insight to answer it. You need a routine that makes the question smaller until the wrong answer is easy to see. Programmers with ten years of experience do not guess better than you; they have a shorter routine.
Every bug hunt follows the same five stages. The order matters: beginners jump from symptom straight to a guessed fix, then wonder why the bug is still there.
The diagram is the map. This is what you do, in order:
Reproduce it. Find an input and command that fail every time.
Isolate it. Delete code and shorten inputs until one small case remains.
Fix one thing. Change a line, then rerun the reproduction.
Verify it. Rerun the whole test suite, not only the failing case.
Keep the test. Turn the reproduction into a test.
This total function is wrong, and nothing in it looks wrong yet. It should sum 2+4+6+8 = 20. Add one print inside the loop and one in main, then run it:
package main
import "fmt"
func total(amounts []int) int {
sum := 0
for i := 0; i < len(amounts)-1; i++ {
sum += amounts[i]
fmt.Println("loop: i =", i, "sum =", sum)
}
return sum
}
func main() {
amounts := []int{2, 4, 6, 8}
fmt.Println("len =", len(amounts))
fmt.Println("total =", total(amounts))
}The output tells the story. The loop visits indexes 0, 1 and 2, then stops. len(amounts) is 4, so len(amounts)-1 is 3, and the condition i < 3 refuses the last item, the 8. One print inside the loop turned a mystery into a sentence.
Prints show data; deleting code shrinks the search space. Comment out the second half of a long function: if the bug remains, it lives in the first half. Repeat until one suspicious line is left. Each deletion removes lines you never need to read again.
The fix here is one character: drop the - 1. With the loop fixed, the prints have done their job and can be deleted as well:
package main
import "fmt"
func total(amounts []int) int {
sum := 0
for i := 0; i < len(amounts); i++ {
sum += amounts[i]
}
return sum
}
func main() {
amounts := []int{2, 4, 6, 8}
fmt.Println("total =", total(amounts))
fmt.Println("expected =", 20)
}The best bug report you can receive is a failing test. It reproduced the bug for you, on demand, with the code and the input attached. Here is one:
$ go test ./...
--- FAIL: TestTotalCents (0.00s)
--- FAIL: TestTotalCents/empty_list (0.00s)
penny_test.go:27: got 5; want 0
FAIL
exit status 1
FAIL penny 0.005sRead it in four pieces, from the top down:
TestTotalCents is the function that failed.empty_list is the table row.penny_test.go:27 called the shared check.Now the next action is mechanical. Open line 27, run that one case with go test -run TestTotalCents/empty_list ./..., and add a print inside totalCents that shows the total before it returns. The question is specific now: why does an empty list return 5?
Some bugs do not need reproducing, because a tool reads the source and finds them first. Two tools are already on your computer.
fmt.Println is fine for a short hunt. For a program that runs for a while, log.Printf is better: it writes to standard error by default, so debug lines stay separate from real output. It also adds a date and time unless you clear its flags:
package main
import (
"log"
"os"
)
func total(amounts []int) int {
sum := 0
for i := range amounts {
sum += amounts[i]
log.Printf("index %d: sum so far %d", i, sum)
}
return sum
}
func main() {
// log adds a date and time, and writes to stderr, by default. For this
// lesson we clear the date and send log output to stdout so the output
// pane matches what the program prints.
log.SetFlags(0)
log.SetOutput(os.Stdout)
amounts := []int{2, 4, 6, 8}
log.Printf("checking %d amounts", len(amounts))
log.Printf("total = %d", total(amounts))
}go vetgo vet reads your source and reports code that compiles but is probably a mistake. It catches things like a Println that looks like a Printf, a format verb that does not match its argument, or an impossible condition.
$ go vet ./...
# penny
./main.go:12:2: fmt.Println call has possible Printf formatting directive %dBreak Penny on purpose: in formatCents, change %02d to %d, so 1250 prints as $12.5. Run go test ./... and read the failure. Add a print inside formatCents that shows the value it received, run the test again, then delete the print and fix the format. Finally, make one more bug in totalCents and find it with the same loop: reproduce, isolate, fix, verify.
Key takeaways
A bug is a question: reproduce, isolate, fix one thing, verify.
A print inside a loop shows the real values as evidence.
Deleting code narrows where a bug can hide.
A failing test names the test, subtest, line and the got/want values.
log.Printf with SetFlags(0) is stable; go vet catches suspicious code.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.