Go Error Handling (Part 2) — The error Type

Image for post
Image for post
Photo by Vitaly Sacred on Unsplash

To understand what error types are and how they work, you first need to be familiar with Go structs, methods, interfaces, and pointers.

The error type is an interface type that comes built-in with Go.

type error interface {
Error() string
}

This essentially means that if we define a user-defined data type (i.e. a struct definition) along with a method for it called Error() (with zero input parameter and one string output parameter), then Go will treat any objects created from that struct as an object containing an error value:

Line 5: We defined a new user-defined (struct based) data type called In this example, we‘ve chosen to store a single key-value pair, with key-name of , and data type of string.

Line 9: We defined a method for our new data type. This method’s name and parameter signature matches up with the error type interface’s Error method and signature. Because of this match, Go will view variables as holding information about an error.

Line 23: We created a variable called.

Line 27: The fmt.Println package has done something interesting here. Under normal circumstances, you would expect it to print which would output the struct’s content, {network timeout} e.g. like this example. However, in this example, it actually printed the output fromMyError.Error() instead. That’s because, the fmt package also recognised that MyError is an error-object (due to lines 9–19) and so it’s trying to be more helpful and gives us the output from MyError.Error(), which is more likely what we’re interested in anyway. This behaviour is documented in the fmt package’s documentation. By the way, if we wanted to, we could have made this line more explicit, by rewriting it tofmt.Println(myErr.Error()) but the output is still the same since that is what’s implicitly implied anyway.

Now the above example is a little flawed because you can’t create a nil object, since structs aren’t allowed to be nil (btw, The closest thing to a nil struct object is a zero value struct).

We can fix this by incorporating pointers into our example, so, that our objects are now pointers to MyError structs (rather than directly being MyError struct objects).

Line 11: Our Error() has now been tweaked to accepted a pointer to a MyError struct, as indicated by the *. That now means that Go considers *MyError posts as an error object.

Line 25: Since our error objects are now supposed to be pointers objects, we can now create error objects with nil values. The fmt package recognises that this is a nil error object, so it prints <nil> (line 31). It didn’t run through the error method (lines 11–21) since a nil error, means there are no errors.

Line 27: We’re now creating a new object, but we’re storing the pointer to this object in myErr2, as indicated by &. This time the fmt package does apply the error method (lines 11–21) to this variable (line 32)

In the above example, we saw that before we can raise an error, we have to set up a bit scaffolding, i.e. we have to define a struct and an Error() method for that struct. However, you can bypass all that by using the errors package.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store