Go Functions, Multiple Returns, and Why Errors Are Just Values
Write typed Go functions, return two values at once, and learn why Go handles errors with if err != nil instead of try/catch.
You'll learn how Go declares parameter and return types, how functions return multiple values, and why Go checks errors explicitly on the next line instead of throwing exceptions. Assumes basic Go variables and if.
1. Functions with types after the names
A function is a reusable block of code with inputs (parameters) and outputs (return values). In Go, types come after the parameter names—the opposite of C or Java, and much closer to how English reads ("a is an int"):
package main
import "fmt"
func add(a int, b int) int {
return a + b
}
func main() {
total := add(3, 4)
fmt.Println(total)
}7When two parameters in a row share a type, you can shorten (a int, b int) to (a, b int).
2. Returning multiple values (and errors!)
In Python or JavaScript, when something goes wrong (like dividing by zero or opening a missing file), the function throws an exception that jumps out of normal execution.
Go refuses to hide control flow like that. Instead, Go functions can return two values at once: the result, and an error:
package main
import (
"errors"
"fmt"
)
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("cannot divide by zero")
}
return a / b, nil
}
func main() {
result, err := divide(10, 0)
if err != nil {
fmt.Println("Oops:", err)
return
}
fmt.Println("Result:", result)
}Oops: cannot divide by zeroLook at what happened:
(float64, error)says this function returns two things: afloat64and anerror.nilis Go's word for "nothing here"—whendividesucceeds, the error isnil.- In
main(),if err != nilchecks "did an error happen?" If yes, we handle it andreturnearly so the rest of the function stays clean.
Change divide(10, 0) to divide(10, 4) and run go run . to verify err is nil and it prints Result: 2.5.
3. Discarding a return value with _
If a function returns two values and you genuinely don't need one of them, assign it to _ (the blank identifier), because Go won't let you leave an unused variable:
result, _ := divide(10, 2)
fmt.Println(result)Use _ sparingly—ignoring an error is usually how bugs sneak into production.
Common mistakes
| Error | What it means | Fix |
|---|---|---|
missing return | An if branch didn't return a value | Make sure every path ends in return |
assignment mismatch: 1 variable but divide returns 2 values | Captured only one value from a 2-return function | Write val, err := divide(...) |
err declared and not used | Captured err and never checked if err != nil | Always check if err != nil |
Practice
- What does
err == nilmean after calling a Go function? - Write the signature of a function
greetthat takes astringand returns astring.
Answers:
- Everything succeeded—no error occurred.
func greet(name string) string.
What you learned
- Go puts parameter types after names (
func add(a, b int) int). - Functions can return multiple values—most commonly
(value, error). - Check
if err != nilimmediately and return early instead of nesting happy-path code.
Video walkthrough
YouTubeClick play to load video (YouTube)