News for hackers. Stuff that matters.
Re:Nil in Go is typed in theory and sort of untyped in practice by waheoo on Wednesday March 31, 2021 @05:45PM (#26653547)
How does nil implement an interface?
Re:Nil in Go is typed in theory and sort of untyped in practice by eptcyka on Wednesday March 31, 2021 @06:06PM (#26653755)
A pointer to a type that implements an interface can be nil, but a function that takes said pointer as an interface type will not be able to distinguish between a pointer that points to an allocated object and a nil one.
Re:Nil in Go is typed in theory and sort of untyped in practice by siebenmann on Wednesday March 31, 2021 @07:02PM (#26654274)
Attempting to call a method on a nil interface value produces a runtime panic. Otherwise, it is the type that implements an interface, and it's possible that the type works even if the specific value of the type is nil. For instance, you could have a 'myIntPointer' type that is a pointer to an int, and implement a String() method for myIntPointer, and at that point a nil myIntPointer still has a String() method and implements the fmt.Stringer interface (although it may get a runtime panic if you actually call the method).