Functions
Functions and error handling
Introduction
In this lesson, you'll learn about functions in Go. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In Python, you're familiar with functions and error handling.
Go has its own approach to functions and error handling, which we'll explore step by step.
The Go Way
Let's see how Go handles this concept. Here's a typical example:
package main
import (
"errors"
"fmt"
)
func add(a, b int) int { return a + b }
// No default params
func greet(name string) string { return "Hello, " + name + "!" }
// Multiple return values (like Python tuple return)
func minMax(nums []int) (int, int) {
lo, hi := nums[0], nums[0]
for _, n := range nums {
if n < lo { lo = n }
if n > hi { hi = n }
}
return lo, hi
}
lo, hi := minMax([]int{3, 1, 4, 1, 5})
// Returns error instead of raising
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}Comparing to Python
Here's how you might have written similar code in Python:
def add(a: int, b: int) -> int:
return a + b
def greet(name: str = "World") -> str:
return f"Hello, {name}!"
def minmax(nums: list[int]) -> tuple[int, int]:
return min(nums), max(nums)
lo, hi = minmax([3, 1, 4, 1, 5])
# Raises on error
def divide(a: float, b: float) -> float:
if b == 0:
raise ValueError("division by zero")
return a / bYou may be used to different syntax or behavior.
Go returns errors as values; Python raises exceptions
You may be used to different syntax or behavior.
Go multiple return values mirror Python tuple returns
You may be used to different syntax or behavior.
Go has no default parameters; Python does
You may be used to different syntax or behavior.
Go error must be checked at call site; Python exceptions can propagate
Step-by-Step Breakdown
1. Error Values vs Exceptions
Go's most important difference: errors are return values, not exceptions. Every call site must handle the error explicitly.
try:
r = divide(a, b)
except ValueError: ...r, err := divide(a, b)
if err != nil { /* handle */ }2. Multiple Returns
Go's multiple return values work just like Python's tuple returns — the calling syntax is similar.
def minmax(nums): return min(nums), max(nums)
lo, hi = minmax(nums)func minMax(nums []int) (int, int) { ... }
lo, hi := minMax(nums)3. No Default Parameters
Go has no default parameter values. Use separate functions or the functional options pattern for configurable behavior.
def connect(host="localhost", port=8080): ...func connect(host string, port int) { ... }
func connectDefault() { connect("localhost", 8080) }Common Mistakes
When coming from Python, developers often make these mistakes:
- Go returns errors as values; Python raises exceptions
- Go multiple return values mirror Python tuple returns
- Go has no default parameters; Python does
Key Takeaways
- Go returns errors; Python raises exceptions
- Multiple returns like Python tuple returns
- No default parameters in Go
- nil is Go's None for error checks