]> Cypherpunks repositories - gostls13.git/commitdiff
doc: do not slice array literal
authorRuss Cox <rsc@golang.org>
Fri, 2 Dec 2011 17:30:37 +0000 (12:30 -0500)
committerRuss Cox <rsc@golang.org>
Fri, 2 Dec 2011 17:30:37 +0000 (12:30 -0500)
The special case in the spec is that you can take the
address of a composite literal using the & operator.

A composite literal is not, however, generally addressable,
and the slice operator requires an addressable argument,
so [3]int{1,2,3}[:] is invalid.  This tutorial code and one bug
report are the only places in the tree where it appears.

R=r, gri
CC=golang-dev
https://golang.org/cl/5437120

doc/go_tutorial.html
doc/go_tutorial.tmpl
doc/progs/sum.go

index 88785a2124b5630a562421d23487da57f9d4b717..423abe8b0a44fa92f5fba241eead1d58d6b09720 100644 (file)
@@ -343,19 +343,21 @@ Using slices one can write this function (from <code>sum.go</code>):
 Note how the return type (<code>int</code>) is defined for <code>sum</code> by stating it
 after the parameter list.
 <p>
-To call the function, we slice the array.  This intricate call (we'll show
+To call the function, we slice the array.  This code (we'll show
 a simpler way in a moment) constructs
 an array and slices it:
 <p>
 <pre>
-s := sum([3]int{1,2,3}[:])
+x := [3]int{1,2,3}
+s := sum(x[:])
 </pre>
 <p>
 If you are creating a regular array but want the compiler to count the
 elements for you, use <code>...</code> as the array size:
 <p>
 <pre>
-s := sum([...]int{1,2,3}[:])
+x := [...]int{1,2,3}
+s := sum(x[:])
 </pre>
 <p>
 That's fussier than necessary, though.
index dfd818959c45b3b5de356cf32e188aed871a14e4..d1219b47edd6ac911bcf4c72796604a2d4b84b0e 100644 (file)
@@ -288,19 +288,21 @@ Using slices one can write this function (from <code>sum.go</code>):
 Note how the return type (<code>int</code>) is defined for <code>sum</code> by stating it
 after the parameter list.
 <p>
-To call the function, we slice the array.  This intricate call (we'll show
+To call the function, we slice the array.  This code (we'll show
 a simpler way in a moment) constructs
 an array and slices it:
 <p>
 <pre>
-s := sum([3]int{1,2,3}[:])
+x := [3]int{1,2,3}
+s := sum(x[:])
 </pre>
 <p>
 If you are creating a regular array but want the compiler to count the
 elements for you, use <code>...</code> as the array size:
 <p>
 <pre>
-s := sum([...]int{1,2,3}[:])
+x := [...]int{1,2,3}
+s := sum(x[:])
 </pre>
 <p>
 That's fussier than necessary, though.
index e022195ed5d8cb9ca2137b994702eb6e3b5d7353..0f316bc012a2a19dec53124d4309346fc5df8747 100644 (file)
@@ -15,6 +15,7 @@ func sum(a []int) int { // returns an int
 }
 
 func main() {
-       s := sum([3]int{1, 2, 3}[:]) // a slice of the array is passed to sum
+       x := [3]int{1, 2, 3}
+       s := sum(x[:]) // a slice of the array is passed to sum
        fmt.Print(s, "\n")
 }