You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (go env)?
linux amd64
What did you do?
Please have a look at the programm below.
I'm just building a simple list using an interface type as parent attribute.
When traversing the list the recursion base test checking this parent attribute against NIL does not work.
The recursion continues, with calling the method on a NIL pointer receiver.
Further investigations shows that all of the following changes would solve the problem:
a) don't use the interface type (Node) for the parent attribute, but the pointer to struct type (*_Node)
b) change the argument type of new from *_Nodeto Node
c) Not using the direct assigment this.parent = p, but the strange construct
This is really a strange behaviour. It seems not to be possible to assign a pointer variable containing nil to an interface variable reflecting nil after the assignment. Instead it points to a pointer pointing to nil which is evaluated with the == nil expression to false.
Ok, that's what I expected, but is there at least a possibility to check for a nil pointer (==nil does not work)
to be able to avoid the nil pointer dereference.
Checking for a nil pointer is usually not the right way to resolve this sort of panic. If a nil pointer is not a valid, usable instance of the interface, you should avoid passing it as that interface in the first place.
In your particular example, it's not obvious why the Parent field needs to be an interface at all, since you are only ever setting it to a *_Node. If you can, make the field a *_Node instead. (See also https://golang.org/wiki/CodeReviewComments#interfaces.)
What version of Go are you using (
go version
)?1.10.1 + 1.10.2
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?linux amd64
What did you do?
Please have a look at the programm below.
I'm just building a simple list using an interface type as parent attribute.
When traversing the list the recursion base test checking this parent attribute against NIL does not work.
The recursion continues, with calling the method on a NIL pointer receiver.
Further investigations shows that all of the following changes would solve the problem:
a) don't use the interface type (
Node
) for the parent attribute, but the pointer to struct type (*_Node
)b) change the argument type of
new
from*_Node
toNode
c) Not using the direct assigment
this.parent = p
, but the strange constructThis is really a strange behaviour. It seems not to be possible to assign a pointer variable containing
nil
to an interface variable reflectingnil
after the assignment. Instead it points to a pointer pointing tonil
which is evaluated with the== nil
expression tofalse
.This is really contra-intuitive.
What did you expect to see?
What did you see instead?
The text was updated successfully, but these errors were encountered: