package main
import "fmt"
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) {
return -1, &argError{arg, "can't work with it"}
}
func main() {
_, e := f2(42)
if ae, ok := e.(*argError); ok {
fmt.Println(ae.arg)
fmt.Println(ae.prob)
}
}
in f2 we have pointer &argError... as return value, but method declared to return error. Why f2 signature is not defined as
func f2(arg int) (int, *error) instead? Or why do we return pointer not a value in this case?