The interface is like an envelope. You “open it” to see what’s inside. Sending 
nothing (no envelope arrives) is not the same as sending an empty envelope 
(i.e., interface is non-nil, pointer within is nil). 

It never confused me, beyond my general confusion from thinking in similes and 
metaphors. ;-)

On 8/9/16, 6:43 AM, "Ian Lance Taylor" <golang-nuts@googlegroups.com on behalf 
of i...@golang.org> wrote:

    On Tue, Aug 9, 2016 at 6:23 AM, Sam Salisbury <samsalisb...@gmail.com> 
wrote:
    >
    > All this gets me thinking, is there any use case where this fact is 
useful?
    > (I.e. a nil-valued interface not being equal to nil via the == operator.)
    
    This has been discussed several times on the mailing list.  An
    interface value == nil if it is the zero value of the interface type.
    It does not == nil if it holds the zero value of some other type, even
    if the zero value of that other type is nil.  Many people would be
    surprised if
        var v interface{} = 0
        fmt.Printf("%t\n", v == nil)
    printed true.  It should be equally surprising if
        var v interface{} = (*byte)(nil)
        fmt.Printf("%t\n", v == nil)
    printed true.
    
    The confusion results because Go, perhaps unfortunately, uses the name
    nil to designate both the zero value of an interface and the zero
    value of a pointer (and a slice or map or channel too, for that
    matter).  If the names were different, this would be less confusing.
    
    > Also, should the reflect.Value.IsNil documentation be updated? It doesn't
    > mention this case where it differs*, only the one about it panicking on a
    > zero reflect.Value.
    >
    > * IsNil returns true for an interface{} with a nil value pointer, even
    > though '== nil' return false.
    
    What update do you suggest?  You say there is a "case where it
    differs", but by my reading there is not.  It may help to read
    https://blog.golang.org/laws-of-reflection .
    
    Ian
    
    -- 
    You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an 
email to golang-nuts+unsubscr...@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to