Section 16
Error Handling
Errors are values that implement the error interface: one method, Error() string. No exceptions. No stack unwinding. Every error is explicit.
Java exceptions vs Go errors
The philosophical difference
Java: errors are exceptional events that interrupt the normal flow. They unwind the stack and propagate automatically until caught. Callers do not need to think about errors unless they declare
Go: errors are normal return values — just another thing the function returns. They do not propagate automatically. Every caller explicitly receives and handles (or passes up) the error. The code is more verbose, but every failure path is visible by reading top to bottom.
throws or catch them.Go: errors are normal return values — just another thing the function returns. They do not propagate automatically. Every caller explicitly receives and handles (or passes up) the error. The code is more verbose, but every failure path is visible by reading top to bottom.
Custom error types
☕ Java — checked exception class
◎ Go — error interface implementation
Wrapping errors — building a trace
fmt.Errorf %w — wrap, preserve, inspect