go by example part 3

This commit is contained in:
2022-01-05 22:56:08 +01:00
parent c1b7c64da2
commit c5983176c5
10 changed files with 337 additions and 0 deletions

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