liveBook · Manning

Error variable

As explained in Go in Practice, Second Edition

An error variable is a package-scoped variable whose value identifies a particular error condition and can be compared or returned by Go code.

An error variable is a package-scoped sentinel error representing a meaningful failure condition. A complex function can return different variables, allowing callers to distinguish cases such as timeouts and rejected requests without defining a custom error type for each one. section 4.1.3

Listing 4.8 Handling two different errors
package main

import (
    "errors"
    "fmt"
    "math/rand"
    "time"
)
const MAX_TIMEOUTS = 5
var ErrTimeout = errors.New("The request timed out")      #1
var ErrRejected = errors.New("The request was rejected")     #2

func init() {
    rand.Seed(time.Now().UnixNano())    #3
}

func main() {
    response, err := SendRequest("Hello")     #4

    if errors.Is(err, ErrTimeout) {
        timeouts := 0
        for err == errors.Is(err, ErrTimeout) {     #5
            timeouts++
            fmt.Println("Timeout. Retrying.")      
            if timeouts == MAX_TIMEOUTS {
                panic("too many timeouts!")
            }
            response, err = SendRequest("Hello")   
        }
    }

    if err != nil {               #6
        fmt.Println(err)          #6
    } else {                          #7
        fmt.Println(response)         #7
    }                                 #7
} #7
func SendRequest(req string) (string, error) {       #8
    switch rand.Intn(3) % 3 {           #9
    case 0:                          #10
        return "Success", nil          
    case 1:                         
        return "", ErrRejected         
    default:                        
        return "", ErrTimeout     #11
    }                             #11
}

Defines ErrTimeout and ErrRejected with errors.New, returns them from a simulated request, detects ErrTimeout with errors.Is, retries, and reports the final result. section 4.1.3

The pattern supports practical error handling:

  • Declare descriptive package-scoped variables such as ErrTimeout and ErrRejected. section 4.1.3
  • Return the variable whenever its associated condition occurs, then let callers choose separate handling for each case. section 4.1.3
  • Use errors.Is to recognize a target error, retry bounded timeout failures, and handle any remaining error. section 4.1.3

How Go in Practice, Second Edition covers it

How deep it goes
Demonstrates declarations, returns, errors.Is checks, bounded retries, and final reporting without adding error-specific information.
Examples
Uses io.EOF, io.ErrNoProgress, ErrTimeout, ErrRejected, and a simulated message-sending request.
What you can do after
Apply error variables to classify failures, retry selected cases, and report unrecoverable results.
How it connects
Connects error variables with sentinel errors, the error interface, errors.Is, custom error types, and panics.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.