4 ms·
Go language uses the same keyword for - a typed nil: "I know the type, but there's no value" - an untyped nil: "I don't know the type and there's no value" B
by kubanczyk 3y ago
Go language uses the same keyword for
- a typed nil: "I know the type, but there's no value"
- an untyped nil: "I don't know the type and there's no value"
But when you compare these two, they are not equal. Sigh.
(Correcting my wording: "untyped" is not precise enough, because it's "I don't know the type exactly, but I know that it's one of the types that fit X interface". So maybe more like "nonconcrete nil".)
- bheadmaster 3y agoThere's no such thing as an "untyped nil" in Go - nil is simply a zero value for "reference types" (i.e. pointers, maps, slices, channels, functions, interfaces). It must be always typed. E.g., the following code: package main func main() { foo := nil _ = foo } Raises a compilation error: ./main.go:4:9: use of untyped nil in assignment It's just that zero value (nil) of interface is not the same as a zero value (nil) of a pointer. Casting a nil pointer to an interface does not create a nil interface, in the same way that casting a zero integer or an empty string to an interface{} (any) doesn't create a nil interface{} (any).