3 ms·
There is a nontransitive example heading this very comment section: var b *bytes.Buffer var r io.Reader = b fmt.Printf("r == nil: %v; b == nil: %v;
by morelisp 6y ago
There is a nontransitive example heading this very comment section:
var b *bytes.Buffer
var r io.Reader = b
fmt.Printf("r == nil: %v; b == nil: %v; r == b: %v\n", r == nil, b == nil, r == b)
r == nil: false; b == nil: true; r == b: true
- jerf 6y agoThank you, I missed that.
- cratermoon 6y agoIt's not confusing if you understand that r is pointer that points to something and b is that pointer, which is nil. So of course r (the variable containing a pointer) isn't nil, b (the pointer) is nil, and r is the pointer b.
- Dylan16807 6y agoPointers don't normally compare equal to the thing they're pointing to. If it's acting as a proxy it's confusing the specific way that some things get proxied and other things don't.
- catlifeonmars 6y agoAgree, r == b in this example is confusing because in all other instances, the interface{} is a kind of box type. That box type is implicitly unwrapped in the r == b comparison. Although more verbose, I’d be happy with having to do something like this for comparison: b2, ok := r.(*bytes.Buffer) equal := ok && b2 == b
- cratermoon 6y agolaughs in C