Here's some code:
var v interface{}
v = (*string)(nil)
// Reflect says it is nil
val := reflect.ValueOf(v)
if val.IsNil() {
fmt.Println("val is nil")
} else {
fmt.Println("val is not nil")
}
// This says it is not nil
if v == nil {
fmt.Println("v is nil")
} else {
fmt.Println("v is not nil")
}
https://play.golang.org/p/apyPa4CNZ6
The output is:
val is nil
v is not nil
How is this possible? Is v nil or not?
Also, if you change the first two lines with
v := (*string)(nil)
then the output clearly states that the variable is nil.
Right now in my project I have a function that accepts a argument of type interface{} and I can't reliably check if it is nil with a simple v == nil. I would like to avoid using the reflect package.