From: Robert Griesemer Date: Tue, 13 Jul 2010 18:54:57 +0000 (-0700) Subject: go spec: specify len/cap for nil slices, maps, and channels X-Git-Tag: weekly.2010-07-14~16 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=0c2e6b363794f953d7d689a3f857ab663114db67;p=gostls13.git go spec: specify len/cap for nil slices, maps, and channels Fixes #891. R=r, rsc CC=golang-dev https://golang.org/cl/1760043 --- diff --git a/doc/go_spec.html b/doc/go_spec.html index 48e4432e0d..13c4c2452c 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,5 +1,5 @@ - + @@ -755,7 +756,8 @@ ElementType = Type . The length is part of the array's type and must be a constant expression that evaluates to a non-negative integer value. The length of array a can be discovered -using the built-in function len(a). The elements can be indexed by integer +using the built-in function len(a). +The elements can be indexed by integer indices 0 through the len(a)-1 (§Indexes). Array types are always one-dimensional but may be composed to form multi-dimensional types. @@ -785,7 +787,7 @@ SliceType = "[" "]" ElementType .

Like arrays, slices are indexable and have a length. The length of a slice s can be discovered by the built-in function -len(s); unlike with arrays it may change during +len(s); unlike with arrays it may change during execution. The elements can be addressed by integer indices 0 through len(s)-1 (§Indexes). The slice index of a given element may be less than the index of the same element in the @@ -804,18 +806,14 @@ the length of the slice and the length of the array beyond the slice; a slice of length up to that capacity can be created by `slicing' a new one from the original slice (§Slices). The capacity of a slice a can be discovered using the -built-in function cap(a) and the relationship between -len(a) and cap(a) is: +built-in function cap(a).

-
-0 <= len(a) <= cap(a)
-
-

-The length and capacity of a nil slice -are 0. A new, initialized slice value for a given element type T is -made using the built-in function make, which takes a slice type +A new, initialized slice value for a given element type T is +made using the built-in function +make, +which takes a slice type and parameters specifying the length and optionally the capacity:

@@ -1155,16 +1153,16 @@ map [string] interface {}

-The number of elements is called the length and is never negative. -The length of a map m can be discovered using the -built-in function len(m) and may change during execution. -Values may be added and removed +The number of map elements is called its length. +For a map m, it can be discovered using the +built-in function len(m) +and may change during execution. Values may be added and removed during execution using special forms of assignment.

A new, empty map value is made using the built-in -function make, which takes the map type and an optional -capacity hint as arguments: +function make, +which takes the map type and an optional capacity hint as arguments:

@@ -4378,6 +4376,10 @@ At any time the following relationship holds:
 0 <= len(s) <= cap(s)
 
+

+The length and capacity of a nil slice, map, or channel are 0. +

+

The expression len(s) is a