mirror of
https://github.com/golang/go
synced 2024-11-25 10:17:57 -07:00
slices and string/array concatenation
OCL=13382 CL=13382
This commit is contained in:
parent
f4dcf51846
commit
84c8d85fe6
@ -4,7 +4,7 @@ The Go Programming Language (DRAFT)
|
|||||||
Robert Griesemer, Rob Pike, Ken Thompson
|
Robert Griesemer, Rob Pike, Ken Thompson
|
||||||
|
|
||||||
----
|
----
|
||||||
(July 21, 2008)
|
(July 22, 2008)
|
||||||
|
|
||||||
This document is a semi-formal specification/proposal for a new
|
This document is a semi-formal specification/proposal for a new
|
||||||
systems programming language. The document is under active
|
systems programming language. The document is under active
|
||||||
@ -1226,15 +1226,17 @@ Expression syntax is based on that of C but with fewer precedence levels.
|
|||||||
|
|
||||||
PrimaryExpr =
|
PrimaryExpr =
|
||||||
identifier | Literal | "(" Expression ")" | "iota" |
|
identifier | Literal | "(" Expression ")" | "iota" |
|
||||||
Call | Conversion | Allocation |
|
Call | Conversion | Allocation | Index |
|
||||||
Expression "[" Expression [ ":" Expression ] "]" | Expression "." identifier |
|
Expression "." identifier | Expression "." "(" Type ")" .
|
||||||
Expression "." "(" Type ")" .
|
|
||||||
|
|
||||||
Call = Expression "(" [ ExpressionList ] ")" .
|
Call = Expression "(" [ ExpressionList ] ")" .
|
||||||
Conversion = "convert" "(" Type [ "," ExpressionList ] ")" |
|
Conversion = "convert" "(" Type [ "," ExpressionList ] ")" |
|
||||||
ConversionType "(" [ ExpressionList ] ")" .
|
ConversionType "(" [ ExpressionList ] ")" .
|
||||||
ConversionType = TypeName | ArrayType | MapType | StructType | InterfaceType .
|
ConversionType = TypeName | ArrayType | MapType | StructType | InterfaceType .
|
||||||
Allocation = "new" "(" Type [ "," ExpressionList ] ")" .
|
Allocation = "new" "(" Type [ "," ExpressionList ] ")" .
|
||||||
|
Index = SimpleIndex | Slice .
|
||||||
|
SimpleIndex = Expression "[" Expression"]" .
|
||||||
|
Slice = Expression "[" Expression ":" Expression "]" .
|
||||||
|
|
||||||
binary_op = log_op | comm_op | rel_op | add_op | mul_op .
|
binary_op = log_op | comm_op | rel_op | add_op | mul_op .
|
||||||
log_op = "||" | "&&" .
|
log_op = "||" | "&&" .
|
||||||
@ -1497,6 +1499,42 @@ to acccess this conversion in low-level code but it will not be available
|
|||||||
in general.
|
in general.
|
||||||
|
|
||||||
|
|
||||||
|
Slices and array concatenation
|
||||||
|
----
|
||||||
|
|
||||||
|
Strings and arrays can be ``sliced'' to construct substrings or 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
|
||||||
|
|
||||||
|
a := []int(1,2,3,4)
|
||||||
|
slice := a[1:3]
|
||||||
|
|
||||||
|
The array ``slice'' has length two and elements
|
||||||
|
|
||||||
|
slice[0] == 2
|
||||||
|
slice[1] == 3
|
||||||
|
|
||||||
|
The index values in the slice must be in bounds for the original
|
||||||
|
array (or string) and the slice length must be non-negative.
|
||||||
|
|
||||||
|
Slices are new arrays (or strings) storing copies of the elements, so
|
||||||
|
changes to the elements of the slice do not affect the original.
|
||||||
|
In the example, a subsequent assignment to element 0,
|
||||||
|
|
||||||
|
slice[0] = 5
|
||||||
|
|
||||||
|
would have no effect on ``a''.
|
||||||
|
|
||||||
|
Strings and arrays can also be concatenated using the ``+'' (or ``+='')
|
||||||
|
operator.
|
||||||
|
|
||||||
|
a += []int(5, 6, 7)
|
||||||
|
s := "hi" + string(c)
|
||||||
|
|
||||||
|
Like slices, addition creates a new array or string by copying the
|
||||||
|
elements.
|
||||||
|
|
||||||
The constant generator 'iota'
|
The constant generator 'iota'
|
||||||
----
|
----
|
||||||
|
|
||||||
@ -2109,7 +2147,7 @@ followed by a series of declarations.
|
|||||||
Program = PackageClause { ImportDecl [ ";" ] } { Declaration [ ";" ] } .
|
Program = PackageClause { ImportDecl [ ";" ] } { Declaration [ ";" ] } .
|
||||||
|
|
||||||
|
|
||||||
Initialization and Program Execution
|
Initialization and program execution
|
||||||
----
|
----
|
||||||
|
|
||||||
A package with no imports is initialized by assigning initial values to
|
A package with no imports is initialized by assigning initial values to
|
||||||
|
Loading…
Reference in New Issue
Block a user