1

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>
1

1 Answer 1

10

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.