go_playground/5-go-by-example/22-errors.go

60 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}
// its 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)
}
}