Skip to content

Commit 4387c16

Browse files
authored
Clarify examples of slice behaviour (#297)
This fixes the examples in the language definition and adds a few more examples to the test suite that mirror this documentation. The first example demonstrates the inclusive-exclusive nature of the indicies. The middle two examples demonstrate the property of `array[:i]` not overlapping with `array[i:]`.
1 parent 2ce7cb6 commit 4387c16

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

docs/Language-Definition.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ Example:
185185
Variable `array` is `[1,2,3,4,5]`.
186186

187187
```
188-
array[1:5] == [2,3,4]
188+
array[1:4] == [2,3,4]
189+
array[:3] == [1,2,3]
189190
array[3:] == [4,5]
190-
array[:4] == [1,2,3]
191191
array[:] == array
192192
```

expr_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,18 @@ func TestExpr(t *testing.T) {
867867
`Array[1:2]`,
868868
[]int{2},
869869
},
870+
{
871+
`Array[1:4]`,
872+
[]int{2, 3, 4},
873+
},
874+
{
875+
`Array[:3]`,
876+
[]int{1, 2, 3},
877+
},
878+
{
879+
`Array[3:]`,
880+
[]int{4, 5},
881+
},
870882
{
871883
`Array[0:5] == Array`,
872884
true,

0 commit comments

Comments
 (0)