From cc06593c681878a2a87f3612aa024e69a1ac074b Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Fri, 14 Sep 2012 11:31:56 -0700 Subject: [PATCH] spec: clarify section on string types Strings happen to be represented similarly to byte slices internally, but they don't quite behave like them: While strings can be indexed, sliced, and have their len() taken like byte slices, string elements are not addressable, make() and cap() is not supported, range loops operate differently, and they are immutable (and thus behave like values rather then references). Fixes #4018. R=r, rsc, iant, ken CC=golang-dev https://golang.org/cl/6503116 --- doc/go_spec.html | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/doc/go_spec.html b/doc/go_spec.html index 13e527c7b6e..72582d4a178 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -1,6 +1,6 @@ @@ -781,19 +781,21 @@ particular architecture.

A string type represents the set of string values. -Strings behave like slices of bytes but are immutable: once created, +A string value is a (possibly empty) sequence of bytes. +Strings are immutable: once created, it is impossible to change the contents of a string. The predeclared string type is string. +

-The elements of strings have type byte and may be -accessed using the usual indexing operations. It is -illegal to take the address of such an element; if -s[i] is the ith byte of a -string, &s[i] is invalid. The length of string -s can be discovered using the built-in function -len. The length is a compile-time constant if s -is a string literal. +The length of a string s (its size in bytes) can be discovered using +the built-in function len. +The length is a compile-time constant if the string is a constant. +A string's bytes can be accessed by integer indices 0 through +len(s)-1Indexes). +It is illegal to take the address of such an element; if +s[i] is the i'th byte of a +string, &s[i] is invalid.

@@ -816,7 +818,7 @@ 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). +using the built-in function len. The elements can be indexed by integer indices 0 through len(a)-1Indexes). Array types are always one-dimensional but may be composed to form @@ -847,7 +849,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; unlike with arrays it may change during execution. The elements can be addressed by integer indices 0 through len(s)-1Indexes). The slice index of a given element may be less than the index of the same element in the @@ -1249,7 +1251,7 @@ map[string]interface{}

The number of map elements is called its length. For a map m, it can be discovered using the -built-in function len(m) +built-in function len and may change during execution. Elements may be added during execution using assignments and retrieved with index expressions; they may be removed with the