Skip to content

errors Module

The errors module provides functions for creating error objects.

Functions

New(message)

Creates a new error with a given message.

Parameters: - message: The error message.

Returns: - (error, None): A new error object.

Example:

Create Error
1
2
3
4
5
6
package main
import fmt
import errors

var err, _ = errors.New("something went wrong")
fmt.Println(err)


Errorf(format, args...)

Creates a new error with a formatted error message.

Parameters: - format: A string that contains format specifiers. - args...: The arguments to be formatted.

Returns: - (error, None): A new error object.

Example:

Wrap Error
package main
import fmt
import errors

function divide(a int, b int) (int, error) {
    if b == 0 {
        // errors.Errorf returns (error, error); destructure to get the error value
        var e, _ = errors.Errorf("division by zero: %d / %d", a, b)
        return 0, e
    }
    return a / b, None
}

var result, err = divide(10, 0)
if err != None {
    fmt.Println(err)
}