Mahdi.Kh
September 21, 2026
An error represents a failure during a program's execution. To keep a program stable and working correctly, errors need to be properly detected, handled, and managed.
We use errors to represent an error state. In Go, the error data type can be used to represent an error state. In Go, when an error occurs, any function can return an error value as part of its output, so the calling function can handle the error.
So error is used for situations where we expect something might go wrong. A failure opening a file, or invalid input from a user, are two common uses for error. In such cases, the error is recoverable, and the program can keep running correctly.
For example, when the os.Open function runs into an error, it returns a non-nil error value as one of its output parameters.
In the example below, we use the os.Open function to open a file, and if an error occurs, we print the error text using log.Fatal and exit the program.
In cases where the program can't continue correctly, or an unrecoverable error has occurred, the error can be announced with panic. To get familiar with panic, you can read the piece titled Defer, Panic, and Recover.
error Data TypeThe error data type is an interface with one method, Error() string. A variable of type error represents any value that can describe itself as text! That is, any value for which the Error() function can return a matching text value.
The error data type is defined as a built-in type in the universal scope. So the error data type is accessible throughout an entire Go program.
The errors package is part of Go's standard packages. The errors package gives us some basic functions and constructs for working with errors. This package has an implementation of the error interface, as follows:
The errors package has a function named errors.New that returns a pointer to errorString as an error. The errors.New function is implemented like this:
So, using the errors.New function, we can create errors in our programs!
In the example above, we have a function named Sqrt. If this function's input parameter is less than zero (negative), an error is returned using the errors.New function, and finally we print the error's value to the screen using the fmt package.
Every error is responsible for briefly explaining what error occurred. The fmt package gets the error's explanation by calling the Error() function.
Using the fmt.Errorf function, you can also turn some text into an error. We can rewrite the example above using the fmt.Errorf function as follows:
error InterfaceIn most cases, program errors can be expressed as an error using errors.New or fmt.Errorf, but we might need to convey more detail through the error! In such cases, we can create a data type that implements the error interface. To implement the error interface, we just need to implement the Error function.
For example, the json package implements its syntax error like this:
The Offset value isn't shown in the error by default, but you can access it using a type assertion:
As another example, in the net package, the Error interface looks like this:
Besides the Error method, this interface also has two other methods, named Timeout and Temporary. Using net.Error, we can write code like this:
This way, for errors that are already known, you can have a data type and then, using a type assertion, check each error's type and access more details about that error.
We can rewrite the code for Sqrt as follows:
In the example above, we defined a data type named NegativeSqrtError. This data type implements the error interface.
Previous part: Introducing the Standard RPC Package | Golang basics
Next part: Packages and Modules | Golang basics