LESSON · 1 OF 25

Errors are values

Errors are values

This is the most important idea in the whole skill, and the thing that feels most different if you come from Python, Java, or JavaScript. Go has no exceptions. When something can fail, the function returns an error as an ordinary value.

Go has a built-in type called error. A function that might fail returns one as its last return value, alongside its real result:

go
func readConfig(path string) (string, error) {
    // returns the config text, and an error if it could not be read
}

If everything worked, the error is nil - Go's word for "no value, nothing here". If something failed, the error holds a value describing what went wrong. Nothing is thrown; the failure is just another value the function hands back.

The shape to remember

The error is always the last thing returned, and nil means success. That pairing - a real value plus an error, nil when all is well - is the foundation everything else in this topic builds on.

Spin up a fresh environment and practice live.
go · fresh machine · ready in under a minute