Go has a builtin type for errors. In Go, an error is the last return value (They have type error).

The line errors.New creates a new error with a message. If there is no error, you can return the nil value.



The function do() is called, which returns an error. To use Go errors, you must include the package errors: import "errors".

package main

import "errors"
import "fmt"

func do() (int, error) {
return -1, errors.New("Something wrong")

func main() {
fmt.Println( do() )

$ go run example.go
-1 Something wrong

You can combine both the return values:

r, e := do()
if r == -1 {
} else {
fmt.Print("Everything went fine\n")