我们在用Go中判断是否存在err,一般是:
if err != nil {
fmt.Println(err)
}
但是为何不使用这样判断呢?
if err {
fmt.Println(err)
}
我们在用Go中判断是否存在err,一般是:
if err != nil {
fmt.Println(err)
}
但是为何不使用这样判断呢?
if err {
fmt.Println(err)
}
go语言中的error部分的源码如下(其实源码上的英文注释已经解答了楼主的疑问,go语言就是这么设计来判断错误的):
// The error built-in interface type is the conventional interface for
// representing an error condition, with the nil value representing no error.
type error interface {
Error() string
}
说明error是接口类型。
源码参考:https://github.com/golang/go/...
2 回答1.3k 阅读
2 回答1.1k 阅读
2 回答1.1k 阅读
2 回答855 阅读
3 回答673 阅读
997 阅读
1 回答776 阅读
在 golang 里 err (一般为 error 类型)不能转换为 bool 类型。