]> Cypherpunks repositories - gostls13.git/commitdiff
spec: clarify that slice a expression shares underlying array with operand
authorRobert Griesemer <gri@golang.org>
Tue, 14 May 2019 17:22:04 +0000 (10:22 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 14 May 2019 22:30:48 +0000 (22:30 +0000)
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>
doc/go_spec.html

index dea3afe498d6cbdab13a02f8c6854a114acbb414..fb4341be1db3c195ce2f599e37bb3a65ebd71de8 100644 (file)
@@ -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"
 }-->
 
@@ -3262,6 +3262,14 @@ is a <code>nil</code> slice. Otherwise, if the result is a slice, it shares its
 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>