The Error Interface

Go programs express errors with error values. An Error is any type that implements the simple built-in error interfacearrow-up-right:

type error interface {
    Error() string
}

When something can go wrong in a function, that function should return an error as its last return value. Any code that calls a function that can return an error should handle errors by testing whether the error is nil.

Atoi

Let's look at how the strconv.Atoi function uses this pattern. The function signature of Atoi is:

func Atoi(s string) (int, error)

This means Atoi takes a string argument and returns two values: an integer and an error. If the string can be successfully converted to an integer, Atoi returns the integer and a nil error. If the conversion fails, it returns zero and a non-nil error.

Here's how you would safely use Atoi:

// Atoi converts a stringified number to an integer
i, err := strconv.Atoi("42b")
if err != nil {
    fmt.Println("couldn't convert:", err)
    // because "42b" isn't a valid integer, we print:
    // couldn't convert: strconv.Atoi: parsing "42b": invalid syntax
    // Note:
    // 'parsing "42b": invalid syntax' is returned by the .Error() method
    return
}
// if we get here, then the
// variable i was converted successfully

A nil error denotes success; a non-nil error denotes failure.

1️⃣ What is the error Interface?

At its core, error is just an interface that looks like this:

type error interface { Error() string }

That means:

  • Any type that defines a method called Error() returning a string automatically implements the error interface.

  • You can return any type as an error as long as it has that method.

In plain English:

The error interface is how Go represents things that go wrong.

Instead of throwing exceptions like Python, Go returns errors as normal values that you check yourself.

2️⃣ A simple example

Let’s make our own custom error.

Usage:

Because myError implements Error() string, it satisfies the error interface.

3️⃣ Built-in errors in Go

Most Go packages return errors this way.

For example:

Here:

  • strconv.Atoi() returns (int, error)

  • If the conversion fails, it returns 0 and a non-nil error value

  • That error value is actually a struct that has an Error() method internally.

So:

fmt.Println(err.Error())

and

fmt.Println(err)

both print the same thing β€” Go automatically calls .Error() when you print an error.

4️⃣ Why return (value, error) instead of exceptions?

Go’s philosophy:

  • No hidden control flow. Errors are handled like normal data.

  • You must handle them explicitly.

  • A nil error means success.

  • A non-nil error means something failed.

That’s why the pattern looks like this everywhere:

5️⃣ Zero Values Rule

When you return an error, return the zero values for everything else.

That means if your function returns (int, error) and fails, you return 0 (the zero value for int) and the error.

Example:

6️⃣ The Assignment Explained

Complete the sendSMSToCouple function. It should send 2 messages, first to the customer and then to the customer's spouse.

1

Step

Use sendSMS() to send the msgToCustomer. If an error is encountered, return 0 and the error.

2

Step

Do the same for the msgToSpouse.

3

Step

If both messages are sent successfully, return the total cost of the messages added together.

Note: When you return a non-nil error in Go, it's conventional to return the zero values of all other return values.

7️⃣ Why it’s designed this way

Go’s approach
Compared to

Return error values explicitly

Python: raises exceptions

Check err != nil

Python: try/except

nil = success

None = no error

Works with multiple return values

Very lightweight and explicit

Go prefers explicit handling over exception throwing.

TL;DR Summary

Concept
Description

error

Built-in interface representing an error condition.

Method required

Error() string

Implemented by

Any type that defines Error()

nil error

No error occurred (success).

Non-nil error

Something went wrong.

Best practice

Return zero values with the error.

Example: All together

Output when input is "42b":

couldn't convert: strconv.Atoi: parsing "42b": invalid syntax

If the input was "42":

Converted: 42

Assignment

We offer a product that allows businesses to send pairs of messages to couples. It is mostly used by flower shops and movie theaters.

Complete the sendSMSToCouple function. It should send 2 messages, first to the customer and then to the customer's spouse.

  • Use sendSMS() to send the msgToCustomer. If an error is encountered, return 0 and the error.

  • Do the same for the msgToSpouse.

  • If both messages are sent successfully, return the total cost of the messages added together.

chevron-rightSolutionhashtag