The zero value (a slice declared with var) is usabe... is missleading (could panic)
kdevb0x opened this issue · comments
The suggestion:
"The zero value (a slice declared with var) is usable immediately without make()."
Under "nil is a valid slice" giving example:
// GOOD
var nums []int
if add1 {
nums = append(nums, 1)
}
if add2 {
nums = append(nums, 2)
}
Is not entirely true. In this case it would work only because append creates and returns a new slice. However if you were to do:
var nums []int
nums[0] = 42
It would panic.
So perhaps the wording should somehow clarify this?
The same is true of a slice created with make([]int, 0)
, so I'm not sure if a nil
slice behaving in the same way is unexpected.
To make this more explicit, we can call out that nil
is a valid 0 length slice.
It looks like we already call that out,
`nil` is a valid slice of length 0