Skip to content

Commit

Permalink
spec: clarify that slice a expression shares underlying array with op…
Browse files Browse the repository at this point in the history
…erand

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 golang#31689.

Change-Id: Ic49351bec2033abd3f5428154ec3e9a7c2c9eaa5
Reviewed-on: https://go-review.googlesource.com/c/go/+/177139
Reviewed-by: Matthew Dempsky <[email protected]>
Reviewed-by: Rob Pike <[email protected]>
Reviewed-by: Keith Randall <[email protected]>
  • Loading branch information
griesemer committed May 14, 2019
1 parent 02d24fc commit 1e3ffb0
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion doc/go_spec.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--{
"Title": "The Go Programming Language Specification",
"Subtitle": "Version of May 13, 2019",
"Subtitle": "Version of May 14, 2019",
"Path": "/ref/spec"
}-->

Expand Down Expand Up @@ -3262,6 +3262,14 @@ <h4>Simple slice expressions</h4>
array with the operand.
</p>

<pre>
var a [10]int
s1 := a[3:7] // underlying array of s1 is array a; &s1[2] == &a[5]
s2 := s1[1:4] // underlying array of s2 is underlying array of s1 which is array a; &s2[1] == &a[5]
s2[1] = 42 // s2[1] == s1[2] == a[5] == 42; they all refer to the same underlying array element
</pre>


<h4>Full slice expressions</h4>

<p>
Expand Down

0 comments on commit 1e3ffb0

Please sign in to comment.