It’s more than a reference to an array, you can think of it as a small struct. Nonetheless the default value is nil (nil array pointer, zero length and capacity) and if you print the comparison to nil you will get true. Println “pretty prints” it as the empty slice [] because it is for all intents and purposes equivalent to a non-nil but empty slice and this is more informative than a plain ”nil”.
Hello @calmh, thank you for your response and clarifying it. Sorry for asking this based on your response, why would it be equivalent to a non-nil? If I understand it, till this link it doesn’t allocate any memory for it, thereby when checked against == it give a nil. Which makes sense.
A slice has three components: a pointer (to values of underlying array), a length and a capacity.
Unlike arrays, slices length, capacity and values can be modified.
Slices are access windows to underlying arrays.
Let’s start with emptynil slice .
Zero value of a slice type is nil.
A nil slice has no underlying array. Iterations over a nil slice are legal but storing to nil slice will cause panic.
I have written a newbie guide/cheatsheet for Slices at link below if it can help: