News for hackers. Stuff that matters.
Re:Golang landmines by parenthephobia on Saturday September 17, 2016 @10:21PM (#12523623)
It's not clear which kind of nil interface you mean. :)

In Go, interface variables are a pair of values: a pointer to the concrete value stored in the variable, and a pointer to the type of that concrete value (i.e. the actual struct).

So there are essentially three kinds of nil in Go: a raw nil pointer, an interface variable with both nil value and type pointers, and an interface variable with a nil value pointer, but a non-nil type pointer.

One way that the two "interface nils" differ is that, to follow the article's example, if you have a Cat variable containing a nil *Tabby, you can invoke Meow on it, and it'll print meow. This works because Tabby's Meow doesn't try to dereference its pointer-to-Tabby. However, if you have a Cat variable that's completely empty, invoking Meow on it will fail.

Re:Golang landmines by thaumasiotes on Sunday September 18, 2016 @02:17AM (#12524175)
What is the conceptual difference between a raw nil pointer, e.g. var t *Tabby = nil, and an interface with nil value and non-nil type? The raw pointer also has nil value and non-nil type.
Re:Golang landmines by Matthias247 on Sunday September 18, 2016 @04:21AM (#12524411)
The difference is that on a nil-interface you can't call a method, it will panic. On a non-nil interface it will always call the method, wether a nil-pointer is stored as the receiver or not. It a nil pointer is stored it will call the method will it, and the method may still do something useful.

It can be theoretically be used to implement some interfaces without any backing object at all (singletons, pure functions, ...), which is a difference to most other programming languages.

Re:Golang landmines by thaumasiotes on Sunday September 18, 2016 @04:25AM (#12524421)
You're explaining the difference between two things, but not the two things I asked about.
Re:Golang landmines by Matthias247 on Sunday September 18, 2016 @04:56AM (#12524490)
Sorry - the question is not immediatly understandable. If you mean how 'var t *Tabby = nil' is different from the interface representation: For this one no type information is stored at runtime. It's only a single pointer. Whereas an interface at runtime is always represented by 2 fields, a pointer and a type field.
Re:Golang landmines by thaumasiotes on Sunday September 18, 2016 @07:19AM (#12524852)
Thanks. It didn't occur to me that non-interface types might not actually carry type information.
Re:Golang landmines by bluejekyll on Saturday September 17, 2016 @10:59PM (#12523728)
Thanks. This is a great succinct example.