From da5a251dde539c91c87caab4abe3d346f88e82fc Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Fri, 2 Dec 2011 12:30:37 -0500 Subject: [PATCH] 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 --- doc/go_tutorial.html | 8 +++++--- doc/go_tutorial.tmpl | 8 +++++--- doc/progs/sum.go | 3 ++- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/doc/go_tutorial.html b/doc/go_tutorial.html index 88785a2124..423abe8b0a 100644 --- a/doc/go_tutorial.html +++ b/doc/go_tutorial.html @@ -343,19 +343,21 @@ Using slices one can write this function (from sum.go): Note how the return type (int) is defined for sum by stating it after the parameter list.

-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:

-s := sum([3]int{1,2,3}[:])
+x := [3]int{1,2,3}
+s := sum(x[:])
 

If you are creating a regular array but want the compiler to count the elements for you, use ... as the array size:

-s := sum([...]int{1,2,3}[:])
+x := [...]int{1,2,3}
+s := sum(x[:])
 

That's fussier than necessary, though. diff --git a/doc/go_tutorial.tmpl b/doc/go_tutorial.tmpl index dfd818959c..d1219b47ed 100644 --- a/doc/go_tutorial.tmpl +++ b/doc/go_tutorial.tmpl @@ -288,19 +288,21 @@ Using slices one can write this function (from sum.go): Note how the return type (int) is defined for sum by stating it after the parameter list.

-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:

-s := sum([3]int{1,2,3}[:])
+x := [3]int{1,2,3}
+s := sum(x[:])
 

If you are creating a regular array but want the compiler to count the elements for you, use ... as the array size:

-s := sum([...]int{1,2,3}[:])
+x := [...]int{1,2,3}
+s := sum(x[:])
 

That's fussier than necessary, though. diff --git a/doc/progs/sum.go b/doc/progs/sum.go index e022195ed5..0f316bc012 100644 --- a/doc/progs/sum.go +++ b/doc/progs/sum.go @@ -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") } -- 2.50.0