mirror of
https://github.com/golang/go
synced 2024-11-21 18:54:43 -07:00
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
This commit is contained in:
parent
ab224094d0
commit
cc06593c68
@ -1,6 +1,6 @@
|
|||||||
<!--{
|
<!--{
|
||||||
"Title": "The Go Programming Language Specification",
|
"Title": "The Go Programming Language Specification",
|
||||||
"Subtitle": "Version of September 12, 2012",
|
"Subtitle": "Version of September 13, 2012",
|
||||||
"Path": "/ref/spec"
|
"Path": "/ref/spec"
|
||||||
}-->
|
}-->
|
||||||
|
|
||||||
@ -781,19 +781,21 @@ particular architecture.
|
|||||||
|
|
||||||
<p>
|
<p>
|
||||||
A <i>string type</i> represents the set of string values.
|
A <i>string type</i> 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.
|
it is impossible to change the contents of a string.
|
||||||
The predeclared string type is <code>string</code>.
|
The predeclared string type is <code>string</code>.
|
||||||
|
</p>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
The elements of strings have type <code>byte</code> and may be
|
The length of a string <code>s</code> (its size in bytes) can be discovered using
|
||||||
accessed using the usual <a href="#Indexes">indexing operations</a>. It is
|
the built-in function <a href="#Length_and_capacity"><code>len</code></a>.
|
||||||
illegal to take the address of such an element; if
|
The length is a compile-time constant if the string is a constant.
|
||||||
<code>s[i]</code> is the <i>i</i>th byte of a
|
A string's bytes can be accessed by integer indices 0 through
|
||||||
string, <code>&s[i]</code> is invalid. The length of string
|
<code>len(s)-1</code> (§<a href="#Indexes">Indexes</a>).
|
||||||
<code>s</code> can be discovered using the built-in function
|
It is illegal to take the address of such an element; if
|
||||||
<code>len</code>. The length is a compile-time constant if <code>s</code>
|
<code>s[i]</code> is the <code>i</code>'th byte of a
|
||||||
is a string literal.
|
string, <code>&s[i]</code> is invalid.
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
|
||||||
@ -816,7 +818,7 @@ ElementType = Type .
|
|||||||
The length is part of the array's type and must be a
|
The length is part of the array's type and must be a
|
||||||
<a href="#Constant_expressions">constant expression</a> that evaluates to a non-negative
|
<a href="#Constant_expressions">constant expression</a> that evaluates to a non-negative
|
||||||
integer value. The length of array <code>a</code> can be discovered
|
integer value. The length of array <code>a</code> can be discovered
|
||||||
using the built-in function <a href="#Length_and_capacity"><code>len(a)</code></a>.
|
using the built-in function <a href="#Length_and_capacity"><code>len</code></a>.
|
||||||
The elements can be indexed by integer
|
The elements can be indexed by integer
|
||||||
indices 0 through <code>len(a)-1</code> (§<a href="#Indexes">Indexes</a>).
|
indices 0 through <code>len(a)-1</code> (§<a href="#Indexes">Indexes</a>).
|
||||||
Array types are always one-dimensional but may be composed to form
|
Array types are always one-dimensional but may be composed to form
|
||||||
@ -847,7 +849,7 @@ SliceType = "[" "]" ElementType .
|
|||||||
<p>
|
<p>
|
||||||
Like arrays, slices are indexable and have a length. The length of a
|
Like arrays, slices are indexable and have a length. The length of a
|
||||||
slice <code>s</code> can be discovered by the built-in function
|
slice <code>s</code> can be discovered by the built-in function
|
||||||
<a href="#Length_and_capacity"><code>len(s)</code></a>; unlike with arrays it may change during
|
<a href="#Length_and_capacity"><code>len</code></a>; unlike with arrays it may change during
|
||||||
execution. The elements can be addressed by integer indices 0
|
execution. The elements can be addressed by integer indices 0
|
||||||
through <code>len(s)-1</code> (§<a href="#Indexes">Indexes</a>). The slice index of a
|
through <code>len(s)-1</code> (§<a href="#Indexes">Indexes</a>). The slice index of a
|
||||||
given element may be less than the index of the same element in the
|
given element may be less than the index of the same element in the
|
||||||
@ -1249,7 +1251,7 @@ map[string]interface{}
|
|||||||
<p>
|
<p>
|
||||||
The number of map elements is called its length.
|
The number of map elements is called its length.
|
||||||
For a map <code>m</code>, it can be discovered using the
|
For a map <code>m</code>, it can be discovered using the
|
||||||
built-in function <a href="#Length_and_capacity"><code>len(m)</code></a>
|
built-in function <a href="#Length_and_capacity"><code>len</code></a>
|
||||||
and may change during execution. Elements may be added during execution
|
and may change during execution. Elements may be added during execution
|
||||||
using <a href="#Assignments">assignments</a> and retrieved with
|
using <a href="#Assignments">assignments</a> and retrieved with
|
||||||
<a href="#Indexes">index</a> expressions; they may be removed with the
|
<a href="#Indexes">index</a> expressions; they may be removed with the
|
||||||
|
Loading…
Reference in New Issue
Block a user