Commit da5a251d authored by Russ Cox's avatar Russ Cox

doc: do not slice array literal

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
parent 4dfe976d
...@@ -343,19 +343,21 @@ Using slices one can write this function (from <code>sum.go</code>): ...@@ -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 Note how the return type (<code>int</code>) is defined for <code>sum</code> by stating it
after the parameter list. after the parameter list.
<p> <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 a simpler way in a moment) constructs
an array and slices it: an array and slices it:
<p> <p>
<pre> <pre>
s := sum([3]int{1,2,3}[:]) x := [3]int{1,2,3}
s := sum(x[:])
</pre> </pre>
<p> <p>
If you are creating a regular array but want the compiler to count the If you are creating a regular array but want the compiler to count the
elements for you, use <code>...</code> as the array size: elements for you, use <code>...</code> as the array size:
<p> <p>
<pre> <pre>
s := sum([...]int{1,2,3}[:]) x := [...]int{1,2,3}
s := sum(x[:])
</pre> </pre>
<p> <p>
That's fussier than necessary, though. That's fussier than necessary, though.
......
...@@ -288,19 +288,21 @@ Using slices one can write this function (from <code>sum.go</code>): ...@@ -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 Note how the return type (<code>int</code>) is defined for <code>sum</code> by stating it
after the parameter list. after the parameter list.
<p> <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 a simpler way in a moment) constructs
an array and slices it: an array and slices it:
<p> <p>
<pre> <pre>
s := sum([3]int{1,2,3}[:]) x := [3]int{1,2,3}
s := sum(x[:])
</pre> </pre>
<p> <p>
If you are creating a regular array but want the compiler to count the If you are creating a regular array but want the compiler to count the
elements for you, use <code>...</code> as the array size: elements for you, use <code>...</code> as the array size:
<p> <p>
<pre> <pre>
s := sum([...]int{1,2,3}[:]) x := [...]int{1,2,3}
s := sum(x[:])
</pre> </pre>
<p> <p>
That's fussier than necessary, though. That's fussier than necessary, though.
......
...@@ -15,6 +15,7 @@ func sum(a []int) int { // returns an int ...@@ -15,6 +15,7 @@ func sum(a []int) int { // returns an int
} }
func main() { 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") fmt.Print(s, "\n")
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment