Error Handling and What’s Next
Errors as values
func divide(a, b float64) (float64, error) {n if b == 0 {n return 0, fmt.Errorf("cannot divide by zero")n }n return a / b, niln}nnresult, err := divide(10, 0)nif err != nil {n fmt.Println("Error:", err)n returnn}nfmt.Println(result)
Go has no exceptions in the sense Python, Java, or JavaScript do. Instead, a function that might fail simply returns an error value alongside its normal result, and the caller is expected to check it immediately with the if err != nil pattern shown above — you will see this exact shape repeated constantly throughout real Go code.
Why this design, briefly
This forces error handling to be visible and explicit at every single call site, rather than allowing an exception to silently propagate up through several layers of unrelated function calls before finally being caught somewhere far away. Some developers find it verbose; the Go team’s position is that visible error handling is worth the extra lines.
panic and recover — for truly exceptional cases
func safeDivide(a, b float64) (result float64) {n defer func() {n if r := recover(); r != nil {n fmt.Println("Recovered from:", r)n result = 0n }n }()n return a / b // dividing by zero in floats gives +Inf, not a panic, but illustrates the patternn}
panic and recover exist for genuinely exceptional situations (like a programming bug, not an expected failure) — they are Go’s closest equivalent to exceptions, but idiomatic Go reaches for the ordinary error return pattern for almost everything, and reserves panic for cases that indicate something has gone seriously, unexpectedly wrong.
Where to go from here
- net/http — Go’s standard library includes a full, production-capable HTTP server with no external framework required
- Testing — Go’s built-in
testingpackage andgo testcommand make writing tests a first-class, batteries-included part of the language - Modules —
go mod initsets up dependency management for a real, multi-file project
You have completed the course
You now know Go’s syntax, structs and methods, pointers, and — its signature strength — goroutines and channels for concurrent programming. Take the certification assessment next to prove it.