2 ms·
No, empty slices and empty maps in Go are not nil.
by stackedinserter 1y ago
No, empty slices and empty maps in Go are not nil.
- maleldil 1y agoThis is the idiomatic way of declaring empty slices in Go, and it prints true: var slice []string fmt.Println(slice == nil)
- tux3 1y agoThis is indeed a nil slice, and it does have len() == 0, but Go also has a concept of empty slices separate from nil slices The language just has a bad habit of confusing them some of the time, but not consistently, so you can still occasionally get bit by the difference As someone who uses Go a lot, it's just one of those things...
- everybodyknows 1y agoWhether to judge the line below idiomatic, or not, is a question I leave to the authorities -- but it is highly convenient, and prints "false". slice := []string{} fmt.Println(slice == nil)
- deleted 1y ago[deleted]
- stackedinserter 1y agoYes because it's nil. You declared it but not created. Same for map. Same for var something *string