Skip to content

Commit 1e3ffb0

Browse files
committed
spec: clarify that slice a expression shares underlying array with operand
The spec was not very precise as to what happens with respect to sharing if a sliced operand is (a pointer to) an array. Added a small clarification and a supporting example. Fixes #31689. Change-Id: Ic49351bec2033abd3f5428154ec3e9a7c2c9eaa5 Reviewed-on: https://go-review.googlesource.com/c/go/+/177139 Reviewed-by: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Keith Randall <khr@golang.org>
1 parent 02d24fc commit 1e3ffb0

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

doc/go_spec.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--{
22
"Title": "The Go Programming Language Specification",
3-
"Subtitle": "Version of May 13, 2019",
3+
"Subtitle": "Version of May 14, 2019",
44
"Path": "/ref/spec"
55
}-->
66

@@ -3262,6 +3262,14 @@ <h4>Simple slice expressions</h4>
32623262
array with the operand.
32633263
</p>
32643264

3265+
<pre>
3266+
var a [10]int
3267+
s1 := a[3:7] // underlying array of s1 is array a; &s1[2] == &a[5]
3268+
s2 := s1[1:4] // underlying array of s2 is underlying array of s1 which is array a; &s2[1] == &a[5]
3269+
s2[1] = 42 // s2[1] == s1[2] == a[5] == 42; they all refer to the same underlying array element
3270+
</pre>
3271+
3272+
32653273
<h4>Full slice expressions</h4>
32663274

32673275
<p>

0 commit comments

Comments
 (0)