From what I understand, you want something like:
func IsZeroOfUnderlyingType(x interface{}) bool {
return x == reflect.Zero(reflect.TypeOf(x)).Interface()
}
When talking about interfaces and nil, people always get confused with two very different and unrelated things:
- A
nilinterface value, which is an interface value that doesn’t have an underlying value. This is the zero value of an interface type. - A non-
nilinterface value (i.e. it has an underlying value), but its underlying value is the zero value of its underlying type. e.g. the underlying value is anilmap,nilpointer, or 0 number, etc.
It is my understanding that you are asking about the second thing.
Update: Due to the above code using ==, it won’t work for types that are not comparable. I believe that using reflect.DeepEqual() instead will make it work for all types:
func IsZeroOfUnderlyingType(x interface{}) bool {
return reflect.DeepEqual(x, reflect.Zero(reflect.TypeOf(x)).Interface())
}