diff --git a/doc/go_spec.html b/doc/go_spec.html index d602168c672..d783a2e0afd 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -36,7 +36,7 @@ href="/">the Go home page.
Go is a general-purpose language designed with systems programming -in mind. It is strongly typed and garbage-collected, and has explicit +in mind. It is strongly typed and garbage-collected and has explicit support for concurrent programming. Programs are constructed from packages, whose properties allow efficient management of dependencies. The existing implementations use a traditional @@ -77,7 +77,7 @@ operators, in increasing precedence:
Lower-case production names are used to identify lexical tokens.
Non-terminals are in CamelCase. Lexical symbols are enclosed in
-double ""
or back quotes ``
.
+double quotes ""
or back quotes ``
.
@@ -112,7 +112,13 @@ unicode_letter = /* a Unicode code point classified as "Letter" */ . unicode_digit = /* a Unicode code point classified as "Digit" */ . -(The Unicode Standard, Section 4.5 General Category - Normative.) +
+In The Unicode Standard 5.0, +Section 4.5 General Category-Normative +defines a set of character categories. Go treats +those characters in category Lu, Ll, Lt, Lm, or Lo as Unicode letters, +and those in category Nd as Unicode digits. +
Implementation restriction: A compiler may implement ideal numbers -by choosing an internal representation with at least twice the precision -of any machine type. +by choosing an internal representation with at least twice as many +bits as any machine type; for floats, both the mantissa and exponent +must be twice as large.
Although these representations all result in an integer, they have
different valid ranges. Octal escapes must represent a value between
-0 and 255 inclusive. (Hexadecimal escapes satisfy this condition
-by construction). The `Unicode' escapes \u
and \U
+0 and 255 inclusive. Hexadecimal escapes satisfy this condition
+by construction. The escapes \u
and \U
represent Unicode code points so within them some values are illegal,
in particular those above 0x10FFFF
and surrogate halves.
+A boolean literal is one of the predeclared constants
+true
or false
. The value of a boolean
+literal is an ideal bool.
+
@@ -545,11 +560,11 @@ it is impossible to change the contents of a string.
The elements of strings have type byte
and may be
accessed using the usual indexing operations (§Indexes). It is
-illegal to take the address of such an element, that is, even if
-s[i]
is the i
th byte of a
+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(s)
. It is a compile-time constant if s
+len(s)
. The length is a compile-time constant if s
is a string literal.
The make()
call allocates a new, hidden array to which the returned
-slice value refers. That is, calling make
+slice value refers. That is, executing
@@ -710,8 +725,8 @@ struct {
-The unqualified type name of an anonymous field must not conflict with the -field identifier (or unqualified type name for an anonymous field) of any +The unqualified type name of an anonymous field must be distinct from the +field identifier (or unqualified type name for an anonymous field) of every other field within the struct. The following declaration is illegal:
@@ -749,17 +764,17 @@ a type namedT
:
A field declaration may be followed by an optional string literal tag,
which becomes an attribute for all the identifiers in the corresponding
field declaration. The tags are made
-visible through a reflection library (TODO: reference?)
+visible through a reflection library TODO: reference?
but are otherwise ignored.
-// A struct corresponding to the EventIdMessage protocol buffer. +// A struct corresponding to the TimeStamp protocol buffer. // The tag strings define the protocol buffer field numbers. struct { - time_usec uint64 "field 1"; - server_ip uint32 "field 2"; - process_id uint32 "field 3"; + microsec uint64 "field 1"; + serverIP6 uint64 "field 2"; + process string "field 3"; }@@ -810,8 +825,7 @@ one unnamed result that is not a function type it may writen as an unparenthesiz For the last parameter only, instead of a type one may write
...
to indicate that the function may be invoked with
zero or more additional arguments of any
-type. If parameters of such a function are named, the final identifier
-list must be a single name, that of the ...
parameter.
+type.
@@ -961,11 +975,11 @@ map [string] interface {} The number of elements is called the length and is never negative. The length of a mapm
can be discovered using the built-in functionlen(m)
and may change during execution. -The value of an uninitialized map isnil
. +Values may be added and removed +during execution using special forms of assignment.-Upon creation, a map is empty. Values may be added and removed -during execution using special forms of assignment (§Assignments). +The value of an uninitialized map is
nil
. A new, empty map value is made using the built-in functionmake
, which takes the map type and an optional capacity hint as arguments: @@ -1024,7 +1038,7 @@ make(chan int, 100)The capacity, in number of elements, sets the size of the buffer in the channel. If the -capacity is greater than zero, the channel is asynchronous and, provided the +capacity is greater than zero, the channel is asynchronous: provided the buffer is not full, sends can succeed without blocking. If the capacity is zero or absent, the communication succeeds only when both a sender and receiver are ready.
@@ -1073,7 +1087,7 @@ identical types. In detail:
type ( T0 []string; - T1 []string + T1 []string; T2 struct { a, b int }; T3 struct { a, c int }; - T4 func (int, float) *T0 - T5 func (x int, y float) *[]string + T4 func (int, float) *T0; + T5 func (x int, y float) *[]string; )@@ -1177,9 +1191,10 @@ A value can always be assigned to the blank identifi
-Values of any type may be compared to other values of compatible static -type. Values of numeric and string type may be compared using the -full range of comparison operators as described in §Comparison operators; +Except as noted, values of any type may be compared to other values of +compatible static type. +Values of numeric and string type may be compared using the +full range of comparison operators; booleans may be compared only for equality or inequality.
@@ -1195,15 +1210,13 @@ Arrays and structs may not be compared to anything.nil
.
A slice value is equal to nil
if it has been assigned the explicit
-value nil
or if it is a variable (or array element,
-field, etc.) that has not been modified since it was created
-uninitialized.
+value nil
, if it is uninitialized, or if it has
+been assigned another slice value equal to nil
·
nil
if it has
-been assigned the explicit value nil
or if it is a
-variable (or array element, field, etc.) that has not been modified
-since it was created uninitialized.
+been assigned the explicit value nil
, if it is uninitialized,
+or if it has been assigned another interface value equal to nil
.
nil
,
@@ -1369,7 +1382,7 @@ is exported if both:
@@ -1408,7 +1421,8 @@ ExpressionList = Expression { "," Expression } .
If the type is omitted, the constants take the
individual types of the corresponding expressions, which may be
-an ideal number or ideal string.
+an ideal number, ideal string,
+or ideal bool.
If the type is present, all constants take the type specified, and the types
of all the expressions must be assignment-compatible
with that type.
@@ -1429,7 +1443,7 @@ const u, v float = 0, 3 // u = 0.0, v = 3.0
Within a parenthesized const
declaration list the
expression list may be omitted from any but the first declaration.
Such an empty list is equivalent to the textual substitution of the
-first preceding non-empty expression list, and its type if any.
+first preceding non-empty expression list and its type if any.
Omitting the list of expressions is therefore equivalent to
repeating the previous list. The number of identifiers must be equal
to the number of expressions in the previous list.
@@ -1530,8 +1544,10 @@ type TreeNode struct {
value *Comparable;
}
-type Comparable interface {
- cmp(Comparable) int
+type Cipher interface {
+ BlockSize() int;
+ Encrypt(src, dst []byte);
+ Decrypt(src, dst []byte);
}
@@ -1551,7 +1567,7 @@ VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList
var i int
var U, V, W float
var k = 0
-var x, y float = -1.0, -2.0
+var x, y float = -1, -2
var (
i int;
u, v, s = 2.0, 3.0, "bar"
@@ -1562,7 +1578,8 @@ var _, found = entries[name]; // map lookup; only interested in "found"
If a list of expressions is given, the variables are initialized -by assigning those expressions to the variables (§Assignments). +by assigning the expressions to the variables (§Assignments) +in order; all expressions must be consumed and all variables initialized from them. Otherwise, each variable is initialized to its zero value.
@@ -1574,15 +1591,16 @@ of the expression list.
If the type is absent and the corresponding expression is a constant
-expression of ideal integer, float, or string type, the type of the
+expression of ideal integer, float, string or bool type, the type of the
declared variable is int
, float
,
-or string
respectively:
+string
, or bool
respectively:
var i = 0 // i has type int var f = 3.1415 // f has type float var s = "OMDB" // s has type string +var t = true // t has type bool
@@ -1694,7 +1712,8 @@ func (p *Point) Scale(factor float) {
-bind the methods Length
and Scale
+bind the methods Length
and Scale
,
+with receiver type *Point
,
to the base type Point
.
-Math.Sin +math.Sin
@@ -1788,7 +1807,9 @@ LiteralType = StructType | ArrayType | "[" "..." "]" ElementType | SliceType | MapType | TypeName . ElementList = Element { "," Element } [ "," ] . Element = [ Key ":" ] Value . -Key = Expression . +Key = FieldName | Index . +FieldName = identifier . +Index = Expression . Value = Expression . @@ -1800,7 +1821,7 @@ The types of the expressions must be assignm the respective field, element, and key types of the LiteralType; there is no additional conversion. The key is interpreted as a field name for struct literals, -an index for array and slice literals, and a key for map literals. +an index expression for array and slice literals, and a key for map literals. For map literals, all elements must have a key. It is an error to specify multiple elements with the same field name or constant key value. @@ -1810,13 +1831,13 @@ constant key value. For struct literals the following rules apply:
A slice literal describes the entire underlying array literal. -Thus, the length and capacity of a slice literal is the maximum +Thus, the length and capacity of a slice literal are the maximum element index plus one. A slice literal has the form
@@ -2058,7 +2079,7 @@ If no value ornil
was assigned to x
, x.f
In all other cases, x.f
is illegal.
-Selectors automatically dereference pointers as necessary.
+Selectors automatically dereference pointers.
If x
is of pointer type, x.y
is shorthand for (*x).y
; if y
is also of pointer type, x.y.z
is shorthand
@@ -2129,9 +2150,9 @@ a[x]
-denotes the array or map element of a
indexed by x
.
+denotes the element of the array, slice, string or map a
indexed by x
.
The value x
is called the
-array index or map key, respectively. The following
+index or map key, respectively. The following
rules apply:
A method call x.m()
is valid if the method set of
-(the type of) x
contains m
(and the
-argument list is compatible with the parameter list of m
).
-If x
is addressable and &x
's method
+(the type of) x
contains m
and the
+argument list is compatible with the parameter list of m
.
+If x
is addressable and &x
's method
set contains m
, x.m()
is shorthand
for (&x).m()
:
x < x + 1
is alw
-Comparison operators yield a boolean result. All comparison operators apply
-to basic types except bools.
+Comparison operators yield a boolean result.
The operators ==
and !=
apply, at least in some cases,
to all types except arrays and structs.
+All other comparison operators apply only
+to basic types except bool
.
@@ -2669,10 +2691,15 @@ The right operand is evaluated conditionally.Address operators
-The unary prefix address-of operator
@@ -2802,7 +2829,7 @@ of a result parameter (e.g.: func f() (x int, p *int) { return 2, &x }).&
generates the address of its operand, which must be a variable, -pointer indirection, field selector, or array or slice indexing operation. It is illegal to take the address of a function -result variable. -Given an operand of pointer type, the unary prefix pointer indirection operator*
retrieves the value pointed +The address-of operator&
generates the address of its operand, +which must be addressable, +that is, either a variable, pointer indirection, array or slice indexing +operation, +or a field selector of an addressable struct operand. +A function result variable is not addressable. +(TODO: remove this restriction.) +Given an operand of pointer type, the pointer indirection +operator*
retrieves the value pointed to by the operand.Communication operators
-The term channel means "variable of channel type" (§Channel types). +The term channel means "value of channel type".
The send operation uses the binary operator "<-", which operates on @@ -2817,9 +2844,9 @@ ch <- 3 The send operation sends the value on the channel. Both the channel and the expression are evaluated before communication begins. Communication blocks until the send can proceed, at which point the -value is transmitted on the channel. A send can proceed if the -channel is asynchronous and there is room in its buffer or the -channel is synchronous and a receiver is ready. +value is transmitted on the channel. +A send on an unbuffered channel can proceed if a receiver is ready. +A send on a buffered channel can proceed if there is room in the buffer.
If the send operation appears in an expression context, the value @@ -2879,7 +2906,7 @@ var x, ok = <-ch
the receive operation becomes non-blocking. -If the operation can proceeed, the boolean variable +If the operation can proceed, the boolean variable
ok
will be set totrue
and the value stored inx
; otherwiseok
is set @@ -2959,7 +2986,7 @@ uint8(100) * 100 // error, out of rangeThe mask used by the unary bitwise complement operator matches -the rule for non-constants: the mask is the all 1s for unsigned constants +the rule for non-constants: the mask is all 1s for unsigned constants and -1 for signed and ideal constants.
@@ -2972,9 +2999,11 @@ int8(^1) // same as int8(-2)
+ TODO: perhaps ^ should be disallowed on non-uints instead of assuming twos complement. Also it may be possible to make typed constants more like variables, at the cost of fewer overflow etc. errors being caught. +
-Each left-hand side operand must be a variable, pointer indirection, -field selector, index expression, or blank identifier. +Each left-hand side operand must be addressable, +a map index expresssion, +or the blank identifier.
@@ -3124,7 +3154,6 @@ x = 1 *p = f() a[i] = 23 k = <-ch -i &^= 1<<n
@@ -3133,10 +3162,13 @@ An assignment operation x
op=
to x
=
x
op
y
but evalutates x
only once. The op=
construct is a single token.
+In assignment operations, both the left- and right-hand expression lists
+must contain exactly one single-valued expression.
a[i] <<= 2 +i &^= 1<<n
@@ -3156,7 +3188,7 @@ x, y = f()
assigns the first value to x
and the second to y
.
-The blank identifier provides a convenient
+The blank identifier provides a
way to ignore values returned by a multi-valued expression:
@@ -3314,7 +3346,8 @@ in the type assertion. TypeSwitchStmt = "switch" [ SimpleStmt ";" ] TypeSwitchGuard "{" { TypeCaseClause } "}" . TypeSwitchGuard = [ identifier ":=" ] Expression "." "(" "type" ")" . TypeCaseClause = TypeSwitchCase ":" [ StatementList ] . -TypeSwitchCase = "case" Type | "default" . +TypeSwitchCase = "case" TypeList | "default" . +TypeList = Type { "," Type } .
@@ -3418,7 +3451,7 @@ for a < b {
-A "for" statement with a "for" clause is also controlled by its condition, but
+A "for" statement with a ForClause is also controlled by its condition, but
additionally it may specify an init
and a post statement, such as an assignment,
an increment or decrement statement. The init statement may be a
@@ -3442,14 +3475,14 @@ If non-empty, the init statement is executed once before evaluating the
condition for the first iteration;
the post statement is executed after each execution of the block (and
only if the block was executed).
-Any element of the "for" clause may be empty but the semicolons are
+Any element of the ForClause may be empty but the semicolons are
required unless there is only a condition.
If the condition is absent, it is equivalent to true
.
-for ; cond ; { S() } is the same as for cond { S() } -for true { S() } is the same as for { S() } +for cond { S() } is the same as for ; cond ; { S() } +for { S() } is the same as for true { S() }
@@ -3467,7 +3500,7 @@ RangeClause = ExpressionList ( "=" | ":=" ) "range" Expression .
The type of the right-hand expression in the "range" clause must be an -array, slice, string or map, or a pointer to an array, slice, string or map; +array, slice, string or map, or a pointer to an array; or it may be a channel. Except for channels, the identifier list must contain one or two expressions @@ -3485,7 +3518,7 @@ must be assignment compatible to the ite
For strings, the "range" clause iterates over the Unicode code points
in the string. On successive iterations, the index variable will be the
-index of successive UTF-8-encoded code points in the string, and
+index of the first byte of successive UTF-8-encoded code points in the string, and
the second variable, of type int
, will be the value of
the corresponding code point. If the iteration encounters an invalid
UTF-8 sequence, the second variable will be 0xFFFD
,
@@ -3519,8 +3552,8 @@ for i, s := range a {
var key string;
var val interface {}; // value type of m is assignment-compatible to val
-for key, value = range m {
- h(key, value)
+for key, val = range m {
+ h(key, val)
}
// key == last map key encountered in iteration
// val == map[key]
@@ -3731,7 +3764,7 @@ L: for i < n {
A "continue" statement begins the next iteration of the -innermost "for" loop at the post statement (§For statements). +innermost "for" loop at its post statement (§For statements).
@@ -3771,7 +3804,7 @@ L:is erroneous because the jump to label
L
skips the creation ofv
. -(TODO: Eliminate in favor of used and not set errors?) +(TODO: Eliminate in favor of used and not set errors?)Fallthrough statements
@@ -3803,10 +3836,10 @@ DeferStmt = "defer" Expression . The expression must be a function or method call. Each time the "defer" statement executes, the parameters to the function call are evaluated and saved anew but the -function is not invoked. Immediately before the innermost function surrounding -the "defer" statement returns, but after its return value (if any) is evaluated, -each deferred function is executed with its saved parameters. Deferred functions -are executed in LIFO order. +function is not invoked. +Deferred function calls are executed in LIFO order +immediately before the surrounding function returns, +but after the return values, if any, have been evaluated.@@ -3843,8 +3876,8 @@ Call Argument type Result len(s) string string length (in bytes) [n]T, *[n]T array length (== n) []T slice length - map[K]T map length - chan T number of elements in channel buffer + map[K]T map length (number of defined keys) + chan T number of elements sent queued in channel buffer cap(s) [n]T, *[n]T array length (== n) []T slice capacity @@ -3907,10 +3940,12 @@ The conversion always yields a valid value; there is no indication of overflow.
When memory is allocated to store a value, either through a declaration
-or new()
, and no explicit initialization is provided, the memory is
+or make()
or new()
call,
+and no explicit initialization is provided, the memory is
given a default initialization. Each element of such a value is
set to the zero value for its type: false
for booleans,
0
for integers, 0.0
for floats, ""
-for strings, and nil
for pointers and interfaces.
+for strings, and nil
for pointers, interfaces, slices, channels, and maps.
This initialization is done recursively, so for instance each element of an
array of structs will have its fields zeroed if no value is specified.
A package with no imports is initialized by assigning initial values to -all its package-level variables in declaration order and then calling any +all its package-level variables in data-dependency order +(TODO: clarify) +and then calling any package-level function with the name and signature of
@@ -4324,9 +4362,11 @@ type Pointer *ArbitraryType func Alignof(variable ArbitraryType) int func Offsetof(selector ArbitraryType) int -func Reflect(i interface {}) (value uint64, typestring string, indir bool) func Sizeof(variable ArbitraryType) int -func Unreflect(value uint64, typestring string, indir bool) interface {} + +func Reflect(val interface {}) (typ runtime.Type, addr uintptr) +func Typeof(val interface {}) reflect.Type +func Unreflect(typ runtime.Type, addr uintptr) interface{}
@@ -4340,7 +4380,8 @@ variable of any type and returns the size of the variable in bytes.
The function Offsetof
takes a selector (§Selectors) denoting a struct
field of any type and returns the field offset in bytes relative to the
-struct's address. For a struct s
with field f
:
+struct's address.
+For a struct s
with field f
:
@@ -4362,10 +4403,24 @@ uintptr(unsafe.Pointer(&x)) % uintptr(unsafe.Alignof(x)) == 0Calls to
Alignof
,Offsetof
, and -Sizeof
are constant expressions of typeint
. +Sizeof
are compile-time constant expressions of typeint
.-TODO describe Reflect, Unreflect +The functions
unsafe.Typeof
, +unsafe.Reflect
, +andunsafe.Unreflect
allow access at run time to the dynamic +types and values stored in interfaces. +Typeof
returns a representation of +val
's +dynamic type as aruntime.Type
. +Reflect
allocates a copy of +val
's dynamic +value and returns both the type and the address of the copy. +Unreflect
invertsReflect
, +creating an +interface value from a type and address. +Thereflect
package built on these primitives +provides a safe, more convenient way to inspect interface values.