目前,我正在使用这个 helper 函数来检查 nil 和 nil 接口
func isNil(a interface{}) bool {
defer func() { recover() }()
return a == nil || reflect.ValueOf(a).IsNil()
}
因为如果值的 Kind 不是 Chan、 Func、 Map、 Ptr、 Interface或 Slice,那么 reflect.ValueOf(a).IsNil()会感到恐慌,所以我加入了延迟的 recover()来捕捉这些值。
有没有更好的方法来实现这种检查?它认为应该有一个更直接的方式来做到这一点。