Functions, multiple returns and errors in Go
Learn Go · Beginner · 11 min · After Slices and maps in Go
How does a Go function tell you it failed?
1Guess
package main import ( "fmt" "strconv" ) func main() { n, err := strconv.Atoi("12a") fmt.Println(n, err) }2Read
Functions and results
A function lists its parameters with types and its results after them:
func add(a, b int) int. A function can return more than one value:func divide(a, b float64) (float64, error). The caller takes them withx, err := divide(8, 2). Use_for a value you really do not need.3Code
Return a value and an error
1 / 2package main import ( "errors" "fmt" ) func divide(a, b float64) (float64, error) { if b == 0 { return 0, errors.New("divide by zero") } return a / b, nil } func main() { for _, d := range []float64{4, 0} { result, err := divide(8, d) if err != nil { fmt.Println("error:", err) continue } fmt.Println("result:", result) } }Read the program. What will it print?
4Read
Errors are values
The
errortype is an ordinary interface: any value with anError() stringmethod is an error. Check it right after the call withif err != nil. To add context, wrap it:fmt.Errorf("parse age: %w", err). The%wverb keeps the original error inside, soerrors.Is(err, target)can still find it.5Code
Wrap an error and test it with errors.Is
1 / 2package main import ( "errors" "fmt" "strconv" ) var ErrEmpty = errors.New("empty input") func parseAge(s string) (int, error) { if s == "" { return 0, ErrEmpty } n, err := strconv.Atoi(s) if err != nil { return 0, fmt.Errorf("parse age: %w", err) } return n, nil } func main() { _, err := parseAge("") fmt.Println(err, errors.Is(err, ErrEmpty)) _, err = parseAge("x") fmt.Println(err, errors.Is(err, ErrEmpty)) }Read the program. What will it print?
6Fix
package main import ( "fmt" "strconv" ) func main() { input := "4o" n, _ := strconv.Atoi(input) fmt.Println("Doubled:", n*2) }7Say it back
8Mission · 10 min
Write a function that parses an age from text and returns an int and an error. Reject empty text with a sentinel error and wrap other failures. Call it with three inputs and print what happens.
go run .
Common questions
- How do I return an error in Go?
- Add error as the last result type, then return a value and an error. On success return the value and nil; on failure return a zero value and an error made with errors.New or fmt.Errorf. The caller checks the error right after the call with if err != nil, which keeps failures visible in the code instead of hidden.
- What does %w do in fmt.Errorf?
- The %w verb wraps an error: the new error prints your message with the original text, and it keeps the original inside. Later, errors.Is and errors.As can look through the wrapping to find it. Use it to add context, such as which step failed, without losing the original error value that callers may want to test.
- What is the difference between errors.New and fmt.Errorf?
- Both create an error value. The errors.New function takes a fixed message, which suits a sentinel error such as ErrEmpty that callers compare against. The fmt.Errorf function builds a message from a format string and values, and with %w it can wrap another error. Use New for fixed errors and Errorf when you add details or context.
Sources
- https://go.dev/blog/error-handling-and-go (read 2026-12-03)
- https://go.dev/doc/effective_go#errors (read 2026-12-03)
- https://pkg.go.dev/errors (read 2026-12-03)
Measured with go1.27.4 darwin/arm64 on 2026-12-03
Published Dec 3, 2026 · Updated Dec 3, 2026