# Error Handling In Go, Part I

- Published: October 13, 2014
- Author: William Kennedy
- URL: https://www.ardanlabs.com/blog/2014/10/error-handling-in-go-part-i.html

<span style="font-size: large;">Introduction</span><br />
It is idiomatic in Go to use the <span style="font-family: Courier New, Courier, monospace;">error</span> interface type as the return type for any error that is going to be returned from a function or method. This interface is used by all the functions and methods in the standard library that return errors. For example, here is the declaration for the <span style="font-family: Courier New, Courier, monospace;">Get</span> method from the <span style="font-family: Courier New, Courier,
 monospace;">http</span> package:
<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.1</b> &nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<i>http://golang.org/pkg/net/http/#Client.Get</i><br />
<br />
func (c &#42;Client) Get(url string) (resp &#42;Response, <b>err error</b>)
</div>
<br />
Listing 1.1 shows how the second return argument for the <span style="font-family: Courier New, Courier, monospace;">Get</span> method is an interface value of type <span style="font-family: Courier New, Courier, monospace;">error</span>. Handling errors that are returned from functions and methods starts by checking if the returned interface value of type <span style="font-family: Courier New, Courier, monospace;">error</span> is not <span style="font-family: Courier New, Courier, monospace;">nil</span>:<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.2</b> &nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
resp, err := c.Get(&quot;[https://www.ardanlabs.com/blog/index.xml](/broken-link) &quot;)<br />
if err != nil {<br />
&nbsp; &nbsp; log.Println(err)<br />
&nbsp; &nbsp; return<br />
}</div>
<br />
In listing 1.2, a call to <span style="font-family: Courier New, Courier, monospace;">Get</span> is performed and the return values are assigned to local variables. Then the value of the <span style="font-family: Courier New, Courier, monospace;">err</span> variable is compared to the value of <span style="font-family: Courier New, Courier, monospace;">nil</span>. If the value is not <span style="font-family: Courier New, Courier, monospace;">nil</span>, then there was an error.<br />
<br />
Because an interface is being used to handle error values, a concrete type needs to be declared that implements the interface. The standard library has declared and implemented this concrete type for us in the form of a struct called <span style="font-family: Courier New, Courier, monospace;">errorString</span>. In this post, we will explore the implementation and use of the <span style="font-family: Courier New, Courier, monospace;">error</span> interface and <span style="font-family: Courier New, Courier, monospace;">errorString</span> struct from the standard library.
<br />
<br />
<span style="font-size: large;">Error Interface and errorString Struct</span><br />
The declaration of the <span style="font-family: Courier New, Courier, monospace;">error</span> interface is provided to us by the language directly:
<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.3</b> &nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<i>http://golang.org/pkg/builtin/#error</i><br />
<br />
type error interface {<br />
&nbsp; &nbsp; Error() string<br />
}
</div>
<br />
In listing 1.3, we can see how the <span style="font-family: Courier New, Courier, monospace;">error</span> interface is declared with a single method called <span style="font-family: Courier New, Courier, monospace;">Error</span> that returns a <span style="font-family: Courier New, Courier, monospace;">string</span>. Therefore, any type that implements the <span style="font-family: Courier New, Courier, monospace;">Error</span> method will implement the interface and can be used as an interface value of type <span style="font-family: Courier New, Courier, monospace;">error</span>. If you are not familiar with how interfaces work in Go, read my post about <a href="https://www.ardanlabs.com/blog/2014/05/methods-interfaces-and-embedded-types.html" target="_blank">Interfaces, Methods and Embedded Types</a>.<br />
<br />
The standard library has also declared a struct type called <span style="font-family: Courier New, Courier, monospace;">errorString</span> that can be found in the <span style="font-family: Courier New, Courier, monospace;">errors</span> package:
<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.4</b> &nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<i>http://golang.org/src/pkg/errors/errors.go</i><br />
<br />
type errorString struct {<br />
&nbsp; &nbsp; s string<br />
}
</div>
<br />
In listing 1.4, we can see how the declaration of <span style="font-family: Courier New, Courier, monospace;">errorString</span> shows a single field named <span style="font-family: Courier New, Courier, monospace;">s</span> of type <span style="font-family: Courier New, Courier, monospace;">string</span>. Both the type and its single field are unexported, which means we can’t directly access the type or its field. To learn more about unexported identifiers in Go, read my post about <a href="https://www.ardanlabs.com/blog/2014/03/exportedunexported-identifiers-in-go.html" target="_blank">Exported/Unexported Identifiers in Go</a>.
<br />
<br />
The <span style="font-family: Courier New, Courier, monospace;">errorString</span> struct implements the error interface:
<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.5</b> &nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<i>http://golang.org/src/pkg/errors/errors.go</i><br />
<br />
func (e &#42;errorString) Error() string {<br />
&nbsp; &nbsp; return e.s<br />
}
</div>
<br />
The <span style="font-family: Courier New, Courier, monospace;">error</span> interface is implemented with a pointer receiver as seen in listing 1.5. This means only pointers of type <span style="font-family: Courier New, Courier, monospace;">errorString</span> can be used as an interface value of type <span style="font-family: Courier New, Courier, monospace;">error</span>. Also, since the <span style="font-family: Courier New, Courier, monospace;">errorString</span> type and its single field are unexported, we can’t perform a type assertion or conversion of the <span style="font-family: Courier New, Courier, monospace;">error</span> interface value. Our only access to the value of the concrete type is with the <span style="font-family: Courier New, Courier, monospace;">Error</span> method.<br />
<br />
The <span style="font-family: Courier New, Courier, monospace;">errorString</span> type is the most common type of error that is returned as an interface value of type <span style="font-family: Courier New, Courier, monospace;">error</span> within the standard library. Now that we know what these types look like, let’s learn how the standard library gives us the ability to create an interface value of type <span style="font-family: Courier New, Courier, monospace;">error</span> using the <span style="font-family: Courier New, Courier, monospace;">errorString</span> struct.<br />
<br />
<span style="font-size: large;">Creating Error Values</span><br />
The standard library provides two ways to create pointers of type <span style="font-family: Courier New, Courier, monospace;">errorString</span> for use as an interface value of type <span style="font-family: Courier New, Courier, monospace;">error</span>. When all you need for your error is a string with no special formatting, the <span style="font-family: Courier New, Courier, monospace;">New</span> function from the <span style="font-family: Courier New, Courier, monospace;">errors</span> package is the way to go:
<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.6</b>&nbsp;&nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
var ErrInvalidParam = errors.New(&quot;mypackage: invalid parameter&quot;)
</div>
<br />
Listing 1.6 shows a typical call to the <span style="font-family: Courier New, Courier, monospace;">New</span> function from the <span style="font-family: Courier New, Courier, monospace;">errors</span> package. In this example, an interface variable of type <span style="font-family: Courier New, Courier, monospace;">error</span> is declared and initialized after the call to <span style="font-family: Courier New, Courier, monospace;">New</span>. Let’s look at the declaration and implementation of the <span style="font-family: Courier New, Courier, monospace;">New</span> function:<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.7</b>&nbsp;&nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<i>http://golang.org/src/pkg/errors/errors.go</i><br />
<br />
// New returns an error that formats as the given text.<br />
func New(text string) error {<br />
&nbsp; &nbsp; return &amp;errorString{text}<br />
}
</div>
<br />
In the declaration of the <span style="font-family: Courier New, Courier, monospace;">New</span> function in listing 1.7, we see the function takes a <span style="font-family: Courier New, Courier, monospace;">string</span> as a parameter and returns an interface value of type <span style="font-family: Courier New, Courier, monospace;">error</span>. In the implementation of the function, a pointer of type <span style="font-family: Courier New, Courier, monospace;">errorString</span> is created. Then on the return statement, an interface value of type <span style="font-family: Courier New, Courier, monospace;">error</span> is created by the compiler and bound to the pointer to satisfy the return argument. The <span style="font-family: Courier New, Courier, monospace;">errorString</span> pointer becomes the underlying data value and type for the interface <span style="font-family: Courier New, Courier, monospace;">error</span> value that is returned.<br />
<br />
When you have an error message that requires formatting, use the <span style="font-family: Courier New, Courier, monospace;">Errorf</span> function from the <span style="font-family: Courier New, Courier, monospace;">fmt</span> package:
<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.8</b>&nbsp;&nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
var ErrInvalidParam = fmt.Errorf(&quot;invalid parameter [%s]&quot;, param)
</div>
<br />
Listing 1.8 shows a typical call to the <span style="font-family: Courier New, Courier, monospace;">Errorf</span> function. If you are familiar with using the other format functions from the <span style="font-family: Courier New, Courier, monospace;">fmt</span> package, then you will notice this works the same. Once again, an interface variable of type <span style="font-family: Courier New, Courier, monospace;">error</span> is declared and initialized after the call to <span style="font-family: Courier New, Courier, monospace;">Errorf</span>.<br />
<br />
Let’s look at the declaration and implementation of the <span style="font-family: Courier New, Courier, monospace;">Errorf</span> function:
<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.9</b>&nbsp;&nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<i>http://golang.org/src/pkg/fmt/print.go</i><br />
<br />
// Errorf formats according to a format specifier and returns the string<br />
// as a value that satisfies error.<br />
func Errorf(format string, a ...interface{}) error {<br />
&nbsp; &nbsp; return errors.New(Sprintf(format, a…))<br />
}
</div>
<br />
In the declaration of the <span style="font-family: Courier New, Courier, monospace;">Errorf</span> function in listing 1.9, we see the <span style="font-family: Courier New, Courier, monospace;">error</span> interface type is being used once again as the return type. In the implementation of the function, the <span style="font-family: Courier New, Courier, monospace;">New</span> function from the <span style="font-family: Courier New, Courier, monospace;">errors</span> package is used to create an interface value of type <span style="font-family: Courier New, Courier, monospace;">error</span> for the message that is formatted. So whether you use the <span style="font-family: Courier New, Courier, monospace;">errors</span> or <span style="font-family: Courier New, Courier, monospace;">fmt</span> package to create your interface value of type <span style="font-family: Courier New, Courier, monospace;">error</span>, the value underneath is always a pointer of type <span style="font-family: Courier New, Courier, monospace;">errorString</span>.<br />
<br />
Now we know the two different ways we can create interface values of type <span style="font-family: Courier New, Courier, monospace;">error</span> using a pointer of type <span style="font-family: Courier New, Courier, monospace;">errorString</span>. Next, let’s learn how packages in the standard library provide support for comparing unique errors that are returned from API calls.
<br />
<br />
<span style="font-size: large;">Comparing Error Values</span><br />
The <span style="font-family: Courier New, Courier, monospace;">bufio</span> package, like many other packages in the standard library, uses the <span style="font-family: Courier New, Courier, monospace;">New</span> function from the <span style="font-family: Courier New, Courier, monospace;">errors</span> package to create package level error variables:
<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.10</b>&nbsp;&nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<i>http://golang.org/src/pkg/bufio/bufio.go</i><br />
<br />
var (<br />
&nbsp; &nbsp; ErrInvalidUnreadByte = errors.New(&quot;bufio: invalid use of UnreadByte&quot;)<br />
&nbsp; &nbsp; ErrInvalidUnreadRune = errors.New(&quot;bufio: invalid use of UnreadRune&quot;)<br />
&nbsp; &nbsp; ErrBufferFull &nbsp; &nbsp; &nbsp; &nbsp;= errors.New(&quot;bufio: buffer full&quot;)<br />
&nbsp; &nbsp; ErrNegativeCount &nbsp; &nbsp; = errors.New(&quot;bufio: negative count&quot;)<br />
)
</div>
<br />
Listing 1.10 shows four package level error variables that are declared and initialized in the <span style="font-family: Courier New, Courier, monospace;">bufio</span> package. Notice each error variable starts with the prefix <span style="font-family: Courier New, Courier, monospace;">Err</span>. This is a convention in Go. Since these variables are declared as interface type <span style="font-family: Courier New, Courier, monospace;">error</span>, we can use these variables to identify specific errors that are returned by the different <span style="font-family: Courier New, Courier, monospace;">bufio</span> package API’s:<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.11</b>&nbsp;&nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
data, err := b.Peek(1)<br />
if err != nil {<br />
&nbsp; &nbsp; switch <b>err</b> {<br />
&nbsp; &nbsp; case <b>bufio.ErrNegativeCount</b>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; // Do something specific.<br />
&nbsp; &nbsp; &nbsp; &nbsp; return<br />
&nbsp; &nbsp; case <b>bufio.ErrBufferFull</b>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; // Do something specific.<br />
&nbsp; &nbsp; &nbsp; &nbsp; return<br />
&nbsp; &nbsp; default:<br />
&nbsp; &nbsp; &nbsp; &nbsp; // Do something generic.<br />
&nbsp; &nbsp; &nbsp; &nbsp; return<br />
&nbsp; &nbsp; }<br />
}
</div>
<br />
In listing 1.11, the code example calls into the <span style="font-family: Courier New, Courier, monospace;">Peek</span> method from a pointer variable of type <span style="font-family: Courier New, Courier, monospace;">bufio.Reader</span>. The <span style="font-family: Courier New, Courier, monospace;">Peek</span> method has the potential of returning both the <span style="font-family: Courier New, Courier, monospace;">ErrNegativeCount</span> and <span style="font-family: Courier New, Courier, monospace;">ErrBufferFull</span> error variables. Because these variables have been exported by the package, we now have the ability to use the variables to identify which specific error message was returned. These variable become part of the packages API for error handling.<br />
<br />
Imagine if the <span style="font-family: Courier New, Courier, monospace;">bufio</span> package did not declare these error variables. Now we would need to compare the actual error messages to determine which error we received:
<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.12</b>&nbsp;&nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
data, err := b.Peek(1)<br />
if err != nil {<br />
&nbsp; &nbsp; switch <b>err.Error()</b> {<br />
&nbsp; &nbsp; case <b>&quot;bufio: negative count&quot;</b>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; // Do something specific.<br />
&nbsp; &nbsp; &nbsp; &nbsp; return<br />
&nbsp; &nbsp; case <b>&quot;bufio: buffer full&quot;</b>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; // Do something specific.<br />
&nbsp; &nbsp; &nbsp; &nbsp; return<br />
&nbsp; &nbsp; default:<br />
&nbsp; &nbsp; &nbsp; &nbsp; // Do something specific.<br />
&nbsp; &nbsp; &nbsp; &nbsp; return<br />
&nbsp; &nbsp; }<br />
}
</div>
<br />
There are two problems with the code example in listing 1.12. First, the call to <span style="font-family: Courier New, Courier, monospace;">Error()</span> requires a copy of the error message to be made for the <span style="font-family: Courier New, Courier, monospace;">switch</span> statement. Second, if the package author ever changes these messages, this code breaks.<br />
<br />
The <span style="font-family: Courier New, Courier, monospace;">io</span> package is another example of a package that declares interface type <span style="font-family: Courier New, Courier, monospace;">error</span> variables for the errors that can be returned:
<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.13</b>&nbsp;&nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<i>http://golang.org/src/pkg/io/io.go</i><br />
<br />
var ErrShortWrite &nbsp; &nbsp;= errors.New(&quot;short write&quot;)<br />
var ErrShortBuffer &nbsp; = errors.New(&quot;short buffer&quot;)<br />
var EOF &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= errors.New(&quot;EOF&quot;)<br />
var ErrUnexpectedEOF = errors.New(&quot;unexpected EOF&quot;)<br />
var ErrNoProgress &nbsp; &nbsp;= errors.New(&quot;multiple Read calls return no data or error&quot;)
</div>
<br />
Listing 1.13 shows six package level error variables that are declared in the <span style="font-family: Courier New, Courier, monospace;">io</span> package. The third variable is the declaration of the <span style="font-family: Courier New, Courier, monospace;">EOF</span> error variable that is returned to indicate when there is no more input available. It is common to compare the error value from functions in this package with the <span style="font-family: Courier New, Courier, monospace;">EOF</span> variable.<br />
<br />
Here is the implementation of the <span style="font-family: Courier New, Courier, monospace;">ReadAtLeast</span> function from inside <span style="font-family: Courier New, Courier, monospace;">io</span> package:
<br />
<br />
<span style="background-color: #eeeeee;">&nbsp; <b>Listing 1.14</b>&nbsp;&nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<i>http://golang.org/src/pkg/io/io.go</i><br />
<br />
func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error) {<br />
&nbsp; &nbsp; if len(buf) &lt; min {<br />
&nbsp; &nbsp; &nbsp; &nbsp; return 0, <b>ErrShortBuffer</b><br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; for n &lt; min &amp;&amp; err == nil {<br />
&nbsp; &nbsp; &nbsp; &nbsp; var nn int<br />
&nbsp; &nbsp; &nbsp; &nbsp; nn, err = r.Read(buf[n:])<br />
&nbsp; &nbsp; &nbsp; &nbsp; n += nn<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; if n &gt;= min {<br />
&nbsp; &nbsp; &nbsp; &nbsp; err = nil<br />
&nbsp; &nbsp; } else if n &gt; 0 &amp;&amp; <b>err == EOF</b> {<br />
&nbsp; &nbsp; &nbsp; &nbsp; err = <b>ErrUnexpectedEOF</b><br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; return<br />
}
</div>
<br />
The <span style="font-family: Courier New, Courier, monospace;">ReadAtLeast</span> function in listing 1.14 shows the use of these error variables in action. Notice how the error variables <span style="font-family: Courier New, Courier, monospace;">ErrShortBuffer</span> and <span style="font-family: Courier New, Courier, monospace;">ErrUnexpectedEOF</span> are used as a return value. Also notice how the function compares the <span style="font-family: Courier New, Courier, monospace;">err</span> variable against the <span style="font-family: Courier New, Courier, monospace;">EOF</span> variable, just like we do in our own code.<br />
<br />
This pattern of creating error variables for the errors your API’s are going to return is something you should consider implementing yourself. It helps provide an API for errors and keeps error handling performant.
<br />
<br />
<span style="font-size: large;">Why Not a Named Type</span><br />
One question that comes up is why didn’t the language designers use a named type for&nbsp;<span style="font-family: Courier New, Courier, monospace;">errorString</span>?<br />
<br />
Let’s take a look at the implementation using a named type and compare it to using the struct type:<br />
<br />
<span style="background-color: #eeeeee;">&nbsp;&nbsp;<b>Listing 1.15</b>&nbsp;&nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<i>http://play.golang.org/p/uZPi4XKMF9</i><br />
<br />
01 package main<br />
02 <br />
03 import (<br />
04 &nbsp; &nbsp; &quot;errors&quot;<br />
05 &nbsp; &nbsp; &quot;fmt&quot;<br />
06 )<br />
07<br />
08 // Create a named type for our new error type.<br />
09 type errorString string<br />
10<br />
11 // Implement the error interface.<br />
12 func (e errorString) Error() string {<br />
13 &nbsp; &nbsp; return string(e)<br />
14 }<br />
15<br />
16 // New creates interface values of type error.<br />
17 func New(text string) error {<br />
18 &nbsp; &nbsp; return errorString(text)<br />
19 }<br />
20<br />
21 var ErrNamedType = New(&quot;EOF&quot;)<br />
22 var ErrStructType = errors.New(&quot;EOF&quot;)<br />
23<br />
24 func main() {<br />
25 &nbsp; &nbsp; if ErrNamedType == New(&quot;EOF&quot;) {<br />
26 &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(&quot;Named Type Error&quot;)<br />
27 &nbsp; &nbsp; }<br />
28<br />
29 &nbsp; &nbsp; if ErrStructType == errors.New(&quot;EOF&quot;) {<br />
30 &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(&quot;Struct Type Error&quot;)<br />
31 &nbsp; &nbsp; }<br />
32 }
<br />
<br />
Output:<br />
Named Type Error</div>
<br />
Listing 1.15 provides a sample program to show a problem surrounding the use of a named type for&nbsp;<span style="font-family: Courier New, Courier, monospace;">errorString</span>.&nbsp;The program on line 09 declares a named typed called&nbsp;<span style="font-family: Courier New, Courier, monospace;">errorString</span>&nbsp;of type&nbsp;<span style="font-family: Courier New, Courier, monospace;">string</span>. Then on line 12, the&nbsp;<span style="font-family: Courier New, Courier, monospace;">error</span>&nbsp;interface is implemented for the named type. To simulate the&nbsp;<span style="font-family: Courier New, Courier, monospace;">New</span>&nbsp;function from the&nbsp;<span style="font-family: Courier New, Courier, monospace;">errors</span>&nbsp;package, a function called&nbsp;<span style="font-family: Courier New, Courier, monospace;">New</span>&nbsp;is implemented on line 17.<br />
<br />
Then on lines 21 and 22, two error variables are declared and initialized. The&nbsp;<span style="font-family: Courier New, Courier, monospace;">ErrNamedType</span>&nbsp;variable is initialized using the&nbsp;<span style="font-family: Courier New, Courier, monospace;">New</span>&nbsp;function and the&nbsp;<span style="font-family: Courier New, Courier, monospace;">ErrStructType</span>&nbsp;is initialized using the&nbsp;<span style="font-family: Courier New, Courier, monospace;">errors.New</span>&nbsp;function. Finally in&nbsp;<span style="font-family: Courier New, Courier, monospace;">main()</span>, the variables are compared to new values created by the same functions.<br />
<br />
When you run the program, the output is interesting. The&nbsp;<span style="font-family: Courier New, Courier, monospace;">if</span>&nbsp;statement on line 25 is&nbsp;<span style="font-family: Courier New, Courier, monospace;">true</span>&nbsp;and the&nbsp;<span style="font-family: Courier New, Courier, monospace;">if</span>&nbsp;statement on line 29 is <span style="font-family: Courier New, Courier, monospace;">false</span>. By using the named type, we are able to create new interface values of type <span style="font-family: Courier New, Courier, monospace;">error</span> with the same error message and they match. This poses the same problem as in listing 1.12. We could create our own versions of the&nbsp;<span style="font-family: Courier New, Courier, monospace;">error</span>&nbsp;values and use them. If at any time the package author changes the messages, our code will break.<br />
<br />
The same problem can occur when&nbsp;<span style="font-family: Courier New, Courier, monospace;">errorString</span>&nbsp;is a struct type. Look at what happens when a value receiver is used for the implementation of the <span style="font-family: Courier New, Courier, monospace;">error</span> interface:<br />
<br />
<span style="background-color: #eeeeee;">&nbsp;&nbsp;<b>Listing 1.16</b>&nbsp;&nbsp;</span><br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
<i>http://play.golang.org/p/EMWPT-tWp4</i><br />
<br />
01 package main<br />
02<br />
03 import (<br />
04 &nbsp; &nbsp;&quot;fmt&quot;<br />
05 )<br />
06<br />
07 type errorString struct {<br />
08 &nbsp; &nbsp;s string<br />
09 }<br />
10<br />
11 func (e errorString) Error() string {<br />
12 &nbsp; &nbsp;return e.s<br />
13 }<br />
14<br />
15 func NewError(text string) error {<br />
16 &nbsp; &nbsp;return errorString{text}<br />
17 }<br />
18<br />
19 var ErrType = NewError(&quot;EOF&quot;)<br />
20<br />
21 func main() {<br />
22 &nbsp; &nbsp;if ErrType == NewError(&quot;EOF&quot;) {<br />
23 &nbsp; &nbsp; &nbsp; &nbsp;fmt.Println(&quot;Error:&quot;, ErrType)<br />
24 &nbsp; &nbsp;}<br />
25 }
<br />
<br />
Output:<br />
Error: EOF</div>
<br />
In listing 1.16 we have implemented the <span style="font-family: Courier New, Courier, monospace;">errorString</span> struct type using a value receiver for the implementation of the <span style="font-family: Courier New, Courier, monospace;">error</span> interface. This time we experience the same behavior as we did in listing 1.15 with the named type. When interface type values are compared, the values of the concrete type are compared underneath.<br />
<br />
By the standard library using a pointer receiver for the implementation of the <span style="font-family: Courier New, Courier, monospace;">error</span> interface for the <span style="font-family: Courier New, Courier, monospace;">errorString</span> struct, the <span style="font-family: Courier New, Courier, monospace;">errors.New</span> function is forced to return a pointer value. This pointer is what is bound to the interface value and will be unique every time. In these cases, pointer values are being compared and not the actual error messages.<br />
<br />
<span style="font-size: large;">Conclusion</span><br />
In this post we created a foundation for understanding what the <span style="font-family: Courier New, Courier, monospace;">error</span> interface is and how it is used in conjunction with the <span style="font-family: Courier New, Courier, monospace;">errorString</span> struct. Using the <span style="font-family: Courier New, Courier, monospace;">errors.New</span> and <span style="font-family: Courier New, Courier, monospace;">fmt.Errorf</span> functions for creating interface values of type <span style="font-family: Courier New, Courier, monospace;">error</span> is a very common practice in Go and highly recommended. Usually a simple string based error message with some basic formatting is all we need to handle errors.
<br />
<br />
We also explored a pattern that is used by the standard library to help us identify the different errors that are returned by API calls. Many packages in the standard library create these exported error variables which usually provide enough granularity to identify one particular error over the other.
<br />
<br />
There are times when creating your own custom error types make sense. This is something we will explore in part II of this post. For now, use the support provided to us by the standard library for handling errors and follow its example.<br />
<br />
Read Part II:&nbsp;<a href="https://www.ardanlabs.com/blog/2014/11/error-handling-in-go-part-ii.html"><span class="sr-only">External resource on ardanlabs.com: </span>https&#58;//www.ardanlabs.com/blog/2014/11/error-handling-in-go-part-ii.html</a>
