// f2函数的输出看不明白, &argError{arg, "can't work with it"}会自动调用argError 的Error 方法么,求解答
```go
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 {
return -1, &argError{arg, "can't work with it"}
}
return arg + 3, nil
}
func main() {
for _, i := range []int{7, 42} {
if r, e := f2(i); e != nil {
fmt.Println("f1 failed:", e)
} else {
fmt.Println("f1 worked:", r)
}
}
}
```
请问
`return -1, &argError{arg, "can't work with it"}`
中
`argError`为什么需要带`&`符号呢?
#4
更多评论