You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For arrays or strings, the indices are in range if 0 <= low <= high <= len(a), otherwise they are out of range. For slices, the upper index bound is the slice capacity cap(a) rather than the length.
This means the following code is valid:
a:= [5]int{1, 2, 3, 4, 5}
s:=a[:1]
s2:=s[2:4]
Notably, the slice expression s[2:4] contains both a low and high indices that are > len(s).
So while this is explicitly permitted by the spec, the contents of s2 is not defined in the spec. We can infer from the definition of a Slice type that it contains the values of a at the requested indices, but this isn't actually specified. As such, given the following code:
a := [5]int{1, 2, 3, 4, 5}
s := a[:1]
s2 := s[0:5]
fmt.Println(s2)
My reading of the spec suggests that the output could be any of the following (non-exhaustive list):
[1 2 3 4 5]
[1 0 0 0 0]
[1 9999 9999 9999 9999]
IOW, strictly speaking, the contents of the slice elements which result from a slice expression whose indices exceed len(s) seems to be undefined.
Therefore I propose adding a small clarification to the spec, along the lines of:
For slices, it is the backing array which is consulted for the result elements, and upper index bound is the slice capacity cap(a) rather than the length.
The text was updated successfully, but these errors were encountered:
A slice, once initialized, is always associated with an underlying array that holds its elements. A slice therefore shares storage with its array and with other slices of the same array; by contrast, distinct arrays always represent distinct storage.
The array underlying a slice may extend past the end of the slice. The capacity is a measure of that extent: it is the sum of the length of the slice and the length of the array beyond the slice; a slice of length up to that capacity can be created by slicing a new one from the original slice. The capacity of a slice a can be discovered using the built-in function cap(a).
The contents are always known and defined (since array contents cannot arbitrarily change).
Agreed. But that's not the concern, either. The concern is that the spec doesn't make it clear that the underlying array is used when slicing a slice beyond the slice's length.
Quoting from the Go spec of of Aug 2, 2023 (emphasis added):
This means the following code is valid:
Notably, the slice expression
s[2:4]
contains both a low and high indices that are >len(s)
.So while this is explicitly permitted by the spec, the contents of
s2
is not defined in the spec. We can infer from the definition of a Slice type that it contains the values ofa
at the requested indices, but this isn't actually specified. As such, given the following code:My reading of the spec suggests that the output could be any of the following (non-exhaustive list):
IOW, strictly speaking, the contents of the slice elements which result from a slice expression whose indices exceed
len(s)
seems to be undefined.Therefore I propose adding a small clarification to the spec, along the lines of:
The text was updated successfully, but these errors were encountered: