Marc-François
Marc-François

Reputation: 4070

How can v == nil return false and reflect.ValueOf(v).IsNil() return true at the same time?

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.

Upvotes: 2

Views: 864

Answers (1)

cshu
cshu

Reputation: 5954

From golang.org:

Under the covers, interfaces are implemented as two elements, a type and a value. The value, called the interface's dynamic value, is an arbitrary concrete value and the type is that of the value. For the int value 3, an interface value contains, schematically, (int, 3).

An interface value is nil only if the inner value and type are both unset, (nil, nil). In particular, a nil interface will always hold a nil type. If we store a nil pointer of type *int inside an interface value, the inner type will be *int regardless of the value of the pointer: (*int, nil). Such an interface value will therefore be non-nil even when the pointer inside is nil.

You can try fmt.Printf("(%v, %T)\n", v, v). It prints (<nil>, *string).

When you change first two lines to v := (*string)(nil), v is just a pointer, not an interface.

Upvotes: 4

Related Questions