Taken from: https://blog.golang.org/error-handling-and-go
The error
interface requires only a Error
method; specific error implementations might have additional methods. For instance, the net package returns errors of type error
, following the usual convention, but some of the error implementations have additional methods defined by the net.Error
interface:
package net
type Error interface {
error
Timeout() bool // Is the error a timeout?
Temporary() bool // Is the error temporary?
}
Client code can test for a net.Error
with a type assertion and then distinguish transient network errors from permanent ones. For instance, a web crawler might sleep and retry when it encounters a temporary error and give up otherwise.
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
time.Sleep(1e9)
continue
}
if err != nil {
log.Fatal(err)
}
Play Blokr Now FREE!
blokr.io the web game where you can eat other blocks!