mirror of
https://github.com/golang/go
synced 2024-11-21 21:44:40 -07:00
Allow optional second expression in slice expressions.
Built-in function copy. Addresses issue 203. R=rsc, r, iant https://golang.org/cl/156089
This commit is contained in:
parent
e1c5c3df77
commit
1a8ebcc4b8
@ -2097,7 +2097,7 @@ PrimaryExpr =
|
|||||||
|
|
||||||
Selector = "." identifier .
|
Selector = "." identifier .
|
||||||
Index = "[" Expression "]" .
|
Index = "[" Expression "]" .
|
||||||
Slice = "[" Expression ":" Expression "]" .
|
Slice = "[" Expression ":" [ Expression ] "]" .
|
||||||
TypeAssertion = "." "(" Type ")" .
|
TypeAssertion = "." "(" Type ")" .
|
||||||
Call = "(" [ ExpressionList ] ")" .
|
Call = "(" [ ExpressionList ] ")" .
|
||||||
</pre>
|
</pre>
|
||||||
@ -2330,28 +2330,39 @@ a regular assignment to an element of the map.
|
|||||||
<h3 id="Slices">Slices</h3>
|
<h3 id="Slices">Slices</h3>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
Strings, arrays, and slices can be <i>sliced</i> to construct substrings or descriptors
|
For a string, array, or slice <code>a</code>, the primary expression
|
||||||
of subarrays. The index expressions in the slice select which elements appear
|
|
||||||
in the result. The result has indexes starting at 0 and length equal to the
|
|
||||||
difference in the index values in the slice. After slicing the array <code>a</code>
|
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
a := [4]int{1, 2, 3, 4};
|
a[lo : hi]
|
||||||
s := a[1:3];
|
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
the slice <code>s</code> has type <code>[]int</code>, length 2, capacity 3, and elements
|
constructs a substring or slice. The index expressions <code>lo</code> and
|
||||||
|
<code>hi</code> select which elements appear in the result. The result has
|
||||||
|
indexes starting at 0 and length equal to
|
||||||
|
<code>hi</code> - <code>lo</code>.
|
||||||
|
After slicing the array <code>a</code>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
a := [5]int{1, 2, 3, 4, 5};
|
||||||
|
s := a[1:4];
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
the slice <code>s</code> has type <code>[]int</code>, length 3, capacity 4, and elements
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
s[0] == 2
|
s[0] == 2
|
||||||
s[1] == 3
|
s[1] == 3
|
||||||
|
s[2] == 4
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
The slice length must not be negative.
|
For convenience, the <code>hi</code> expression may be omitted; the notation
|
||||||
|
<code>a[lo :]</code> is shorthand for <code>a[lo : len(a)]</code>.
|
||||||
For arrays or strings, the indexes
|
For arrays or strings, the indexes
|
||||||
<code>lo</code> and <code>hi</code> must satisfy
|
<code>lo</code> and <code>hi</code> must satisfy
|
||||||
0 <= <code>lo</code> <= <code>hi</code> <= length;
|
0 <= <code>lo</code> <= <code>hi</code> <= length;
|
||||||
@ -2461,7 +2472,7 @@ assignment of regular parameters.
|
|||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
func Split(s string, pos int) (string, string) {
|
func Split(s string, pos int) (string, string) {
|
||||||
return s[0:pos], s[pos:len(s)]
|
return s[0:pos], s[pos:]
|
||||||
}
|
}
|
||||||
|
|
||||||
func Join(s, t string) string {
|
func Join(s, t string) string {
|
||||||
@ -4137,7 +4148,7 @@ The memory is initialized as described in the section on initial values
|
|||||||
(§<a href="#The_zero_value">The zero value</a>).
|
(§<a href="#The_zero_value">The zero value</a>).
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre class="grammar">
|
||||||
new(T)
|
new(T)
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
@ -4170,7 +4181,7 @@ The memory is initialized as described in the section on initial values
|
|||||||
(§<a href="#The_zero_value">The zero value</a>).
|
(§<a href="#The_zero_value">The zero value</a>).
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<pre>
|
<pre class="grammar">
|
||||||
make(T [, optional list of expressions])
|
make(T [, optional list of expressions])
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
@ -4199,6 +4210,34 @@ m := make(map[string] int, 100); // map with initial space for 100 elements
|
|||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
|
<h3 id="Copying_slices">Copying slices</h3>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
The built-in function <code>copy</code> copies array or slice elements from
|
||||||
|
a source <code>src</code> to a destination <code>dst</code> and returns the
|
||||||
|
number of elements copied. Source and destination may overlap.
|
||||||
|
Both arguments must have the same element type <code>T</code> and must be
|
||||||
|
<a href="#Assignment_compatibility">assignment compatible</a> to a slice
|
||||||
|
of type <code>[]T</code>. The number of arguments copied is the minimum of
|
||||||
|
<code>len(src)</code> and <code>len(dst)</code>.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<pre class="grammar">
|
||||||
|
copy(dst, src []T) int
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p>
|
||||||
|
Examples:
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
var a = [...]int{0, 1, 2, 3, 4, 5, 6, 7};
|
||||||
|
var s = make([]int, 6);
|
||||||
|
n1 := copy(s, &a); // n1 == 6, s == []int{0, 1, 2, 3, 4, 5}
|
||||||
|
n2 := copy(s, s[2:]); // n2 == 4, s == []int{2, 3, 4, 5, 4, 5}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
|
||||||
<h3 id="Bootstrapping">Bootstrapping</h3>
|
<h3 id="Bootstrapping">Bootstrapping</h3>
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
Loading…
Reference in New Issue
Block a user