go by example part 3
This commit is contained in:
59
5-go-by-example/22-errors.go
Normal file
59
5-go-by-example/22-errors.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// by convention, errors are the last return value and have type error, a built-in interface
|
||||
func f1(arg int) (int, error) {
|
||||
if arg == 42 {
|
||||
// errors.New constructs a basic error value with the given error message
|
||||
return -1, errors.New("can't work with 42")
|
||||
}
|
||||
// a nil value in the error position indicates that there was no error
|
||||
return arg + 3, nil
|
||||
}
|
||||
|
||||
// it’s possible to use custom types as errors by implementing the Error() method on them
|
||||
type argError struct {
|
||||
arg int
|
||||
prob string
|
||||
}
|
||||
|
||||
func (e *argError) Error() string {
|
||||
return fmt.Sprintf("%d - %s", e.arg, e.prob)
|
||||
}
|
||||
|
||||
func f2(arg int) (int, error) {
|
||||
if arg == 42 {
|
||||
// use &argError syntax to build a new struct
|
||||
return -1, &argError{arg, "can't work with it"}
|
||||
}
|
||||
return arg + 3, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
// loops below test out each of our error-returning functions
|
||||
for _, i := range []int{7, 42} {
|
||||
if r, e := f1(i); e != nil {
|
||||
fmt.Println("f1 failed:", e)
|
||||
} else {
|
||||
fmt.Println("f1 worked:", r)
|
||||
}
|
||||
}
|
||||
for _, i := range []int{7, 42} {
|
||||
if r, e := f2(i); e != nil {
|
||||
fmt.Println("f2 failed:", e)
|
||||
} else {
|
||||
fmt.Println("f2 worked:", r)
|
||||
}
|
||||
}
|
||||
|
||||
// programmatically use the data in a custom error
|
||||
_, e := f2(42)
|
||||
if ae, ok := e.(*argError); ok {
|
||||
fmt.Println(ae.arg)
|
||||
fmt.Println(ae.prob)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user