---
title: "Functions, multiple returns and errors in Go"
slug: "functions-and-errors"
description: "Write functions with several results, return an error next to the value, check it with if err != nil, wrap it with %w and test it with errors.Is."
published: "2026-12-03"
updated: "2026-12-03"
canonical: "https://aifree.dev/courses/learn-go/functions-and-errors"
---

# Functions, multiple returns and errors in Go

How does a Go function tell you it failed?

## Predict

```go
package main

import (
	"fmt"
	"strconv"
)

func main() {
	n, err := strconv.Atoi("12a")
	fmt.Println(n, err)
}
```

What does this program print?

- It crashes with a panic
- 0 and an error message that names the bad text
- 12 <nil>

Answer: 0 and an error message that names the bad text — On failure Atoi returns 0 and an error that says where parsing failed.

Output:

```
0 strconv.Atoi: parsing "12a": invalid syntax
```

The call returns two values: the number 0 and an error that describes the failure.

## 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 with `x, err := divide(8, 2)`. Use `_` for a value you really do not need.

## Return a value and an error

```go
package 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)
	}
}
```

Output:

```
result: 2
error: divide by zero
```

`errors.New` makes an error value. The function returns `nil` for the error when everything went well.

Playground: https://go.dev/play/p/w9qvJz2N3HN

## Errors are values

The `error` type is an ordinary interface: any value with an `Error() string` method is an error. Check it right after the call with `if err != nil`. To add context, wrap it: `fmt.Errorf("parse age: %w", err)`. The `%w` verb keeps the original error inside, so `errors.Is(err, target)` can still find it.

## Wrap an error and test it with errors.Is

```go
package 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))
}
```

Output:

```
empty input true
parse age: strconv.Atoi: parsing "x": invalid syntax false
```

`ErrEmpty` is a sentinel: one shared error value to compare with. The wrapped error prints the extra context and the original text.

Playground: https://go.dev/play/p/tGNYk91HvEl

## Fix it

```go
package main

import (
	"fmt"
	"strconv"
)

func main() {
	input := "4o"
	n, _ := strconv.Atoi(input)
	fmt.Println("Doubled:", n*2)
}
```

This program compiles but prints Doubled: 0 for the input 4o. What is the bug?

- The error from Atoi is thrown away with _, so bad input looks like 0
- Atoi cannot parse numbers at all
- n * 2 should be n + n

Answer: The error from Atoi is thrown away with _, so bad input looks like 0 — The failure was reported, but the program never looked at it.

Fixed:

```go
package main

import (
	"fmt"
	"strconv"
)

func main() {
	input := "4o"
	n, err := strconv.Atoi(input)
	if err != nil {
		fmt.Println("not a number:", input)
		return
	}
	fmt.Println("Doubled:", n*2)
}
```

Output:

```
not a number: 4o
```

## Say it back

In your own words: why does Go return errors next to the result?

Our sentence: A function can return a detailed error alongside its normal result, so the caller sees the failure at the call and handles it with an ordinary if.

## Mission (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.

```sh
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.
