Errors in Real Go Programs
The most common Go error handling mistakes with their exact messages, plus practical patterns for HTTP handlers and file reading.
Knowing how errors.Is works is one thing. Using it well in a web server or a file processor is another. This section collects the mistakes people actually hit, with the exact compiler and runtime messages, and then shows complete patterns for two of the most common real-world jobs.
Common Error Handling Mistakes
Each mistake is listed with its message so you can match what you see in your terminal:
assignment mismatch: 1 variable but strconv.Atoi returns 2 values
declared and not used: err
not enough return values
panic: runtime error: invalid memory address or nil pointer dereference
It also covers bugs that compile cleanly, like logging and returning the same error, %v instead of %w, and unchecked Close on written files.
Errors in HTTP Handlers
Handlers cannot return errors, so each failure must become a status code. Map known errors with errors.Is and errors.As, hide internal details from clients, and use an error-returning handler type to keep the mapping in one place.
Always return after http.Error:
http.Error writes the response but does not stop the handler.
Errors When Reading Files
Detect missing files with errors.Is(err, fs.ErrNotExist), add line numbers to parse errors, check sc.Err() after scanning, and fix bufio.Scanner: token too long with a bigger buffer.
Summary
Real programs need the same few tools used consistently: check before use, wrap with context, map errors once at the edge, and never let a read error look like a normal end of input.
Common Go Error Handling Mistakes and Their Messages
The error handling mistakes Go beginners make most, with the exact compiler and runtime messages they produce and how to fix each one.
Handling Errors in Go HTTP Handlers
How to turn Go errors into the right HTTP status codes, keep internal details out of responses, and avoid repeating error handling in every handler.
Handling Errors When Reading Files in Go
How to handle missing files, permission errors, read errors and scanner errors when reading files in Go, with complete runnable examples.