river
river

Reputation: 734

How to detect whether a struct pointer is nil in golang?

As is shown by the following code: n := localplugin.NewPluginNet(), the type of n is localnet.Net, which is a interface implemented by the struct pointer *localnet.DomainSocket. Function func NewPluginNet() localnet.Net returns a nil pointer to n.

var n localnet.Net    
n = localplugin.NewPluginNet()
fmt.Println("----> ", n)
if n == nil {
    fmt.Println("n is nil")
}else{
    fmt.Println("n is not nil : ",  n)
}
fmt.Println(reflect.TypeOf(n), reflect.TypeOf(nil), n)

The following is the output of the code above.

---->  <nil>
n is not nil :  <nil>
*localnet.DomainSocket <nil> <nil>

Why does n is not nil ?

===================================

var n1 *localnet.DomainSocket
n1 = nil
fmt.Println(">> ", n1)
if n1 == nil {
    fmt.Println(">>n1 is nil")
}else{
    fmt.Println(">>n1 is not nil : ",  n)
}
fmt.Println(reflect.TypeOf(n1), reflect.TypeOf(nil), n1)

However, it's correct for this case:

>>  <nil>
>>n1 is nil
*localnet.DomainSocket <nil> <nil>

Upvotes: 1

Views: 6047

Answers (1)

icza
icza

Reputation: 418595

Why isn't n nil? Type of n is an interface type (localnet.Net) which is a wrapper for a (type; value) pair. The interface value may be non-nil even if it wraps a nil value. See Hiding nil values, understanding why golang fails here

To answer the question in title: you may use reflection to tell if a non-nil interface value wraps a nil value (e.g. nil pointer):

reflect.ValueOf(someValue).IsNil()

Example:

type reader struct{}

func (*reader) Read(p []byte) (int, error) { return 0, nil }

func main() {
    var r io.Reader = (*reader)(nil)

    fmt.Println(r, r == nil)
    fmt.Println(reflect.ValueOf(r).IsNil())
}

Output (try it on the Go Playground):

<nil> false
true

Upvotes: 10

Related Questions