Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(595)

Unified Diff: doc/go_spec.html

Issue 156089: code review 156089: Allow optional second expression in slice expressions. (Closed)
Patch Set: code review 156089: Allow optional second expression in slice expressions. Created 14 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: doc/go_spec.html
===================================================================
--- a/doc/go_spec.html
+++ b/doc/go_spec.html
@@ -2097,7 +2097,7 @@
Selector = "." identifier .
Index = "[" Expression "]" .
-Slice = "[" Expression ":" Expression "]" .
+Slice = "[" Expression ":" [ Expression ] "]" .
TypeAssertion = "." "(" Type ")" .
Call = "(" [ ExpressionList ] ")" .
</pre>
@@ -2330,28 +2330,39 @@
<h3 id="Slices">Slices</h3>
<p>
-Strings, arrays, and slices can be <i>sliced</i> to construct substrings or descriptors
-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>
-
-<pre>
-a := [4]int{1, 2, 3, 4};
-s := a[1:3];
-</pre>
-
-<p>
-the slice <code>s</code> has type <code>[]int</code>, length 2, capacity 3, and elements
+For a string, array, or slice <code>a</code>, the primary expression
+</p>
+
+<pre>
+a[lo : hi]
+</pre>
+
+<p>
+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>&nbsp;-&nbsp;<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>
<pre>
s[0] == 2
s[1] == 3
-</pre>
-
-<p>
-The slice length must not be negative.
+s[2] == 4
+</pre>
+
+<p>
+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
<code>lo</code> and <code>hi</code> must satisfy
0 &lt;= <code>lo</code> &lt;= <code>hi</code> &lt;= length;
@@ -2461,7 +2472,7 @@
<pre>
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 {
@@ -4137,7 +4148,7 @@
(§<a href="#The_zero_value">The zero value</a>).
</p>
-<pre>
+<pre class="grammar">
new(T)
</pre>
@@ -4170,7 +4181,7 @@
(§<a href="#The_zero_value">The zero value</a>).
</p>
-<pre>
+<pre class="grammar">
make(T [, optional list of expressions])
</pre>
@@ -4199,6 +4210,34 @@
</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, &amp;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>
<p>
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b