I believe there is a slight problem with the Go specification. The following complains that the array literal is not addressable:
print([4]int{2,3}[:2])
I know why an array returned from a function is not addressable (eg Error addressing the returned slice of a function) but why is an array literal like [4]int{2,3}
not addressable? Especially when slice and string literals are - eg these work fine:
print([]int{2,3,0,0}[:2])
print("2300"[:2])
Moreover, array literals do seem to be addressable since &[4]int{42,43}
is a valid expression.
I know I can use a slice
print([]int{2,3,0,0}[:2])
but what if I wanted the capacity (array length) to be a compile-time constant.
const maxLength = 4
...
print([maxLength]int{2,3}[:2])
And yes I can assign to a temporary array variable, but why do I need to?