Printing typed nil


(Sebastian Malton) #1

I ran into this https://play.golang.org/p/aJEJiZJDhYa today while debugging. Is this supposed to happen? I would have assumed that it printed something like nil or even nil{}.


(Johan Dahl) #2

The value is nil. But Println represent it as an empty map then printed. https://play.golang.org/p/Ey__aH1ZXtA shows it as nil if you uses Printed and the formatting code %#v


(Ali Koyuncu) #3

Map and channels are actually pointers to runtime types. When you initialize a map or a channel it creates a pointer that reference to runtime type which is nil. When you print it, it prints the pointer which is not nil, but when you compare it, it compares their referenced values, which is nil.