diff --git a/doc/go_spec.html b/doc/go_spec.html index c7f2fdb4c5..18aeb09f1c 100644 --- a/doc/go_spec.html +++ b/doc/go_spec.html @@ -64,7 +64,8 @@ Open issues: - no mechanism to declare a local type name: type T P.T -Todo's: +Todo +[ ] clarify: two equal lowercase identifiers from different packages denote different objects [ ] need language about function/method calls and parameter passing rules [ ] need to say something about "scope" of selectors? [ ] clarify what a field name is in struct declarations @@ -78,7 +79,6 @@ Todo's: though obvious [ ] specify iteration direction for range clause [ ] review language on implicit dereferencing -[ ] document T.m mechanism to obtain a function from a method --> @@ -86,8 +86,7 @@ Todo's:

This is a reference manual for the Go programming language. For -more information and other documents, see the Go home page. +more information and other documents, see go/go.

@@ -258,9 +257,9 @@ The following character sequences represent operators,

Integer literals

-An integer literal is a sequence of one or more digits in the -corresponding base, which may be 8, 10, or 16. An optional prefix -sets a non-decimal base: 0 for octal, 0x or +An integer literal is a sequence of digits representing an +integer constant. +An optional prefix sets a non-decimal base: 0 for octal, 0x or 0X for hexadecimal. In hexadecimal literals, letters a-f and A-F represent values 10 through 15.

@@ -280,8 +279,9 @@ hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit } .

Floating-point literals

-A floating-point literal is a decimal representation of a floating-point -number. It has an integer part, a decimal point, a fractional part, +A floating-point literal is a decimal representation of a +floating-point constant. +It has an integer part, a decimal point, a fractional part, and an exponent part. The integer and fractional part comprise decimal digits; the exponent part is an e or E followed by an optionally signed decimal exponent. One of the @@ -306,28 +306,12 @@ exponent = ( "e" | "E" ) [ "+" | "-" ] decimals . .12345E+5 -

Ideal numbers

- -

-Integer literals represent values of arbitrary precision, or ideal -integers. Similarly, floating-point literals represent values -of arbitrary precision, or ideal floats. These ideal -numbers have no size or named type and cannot overflow. However, -when (used in an expression) assigned to a variable or typed constant, -the destination must be able to represent the assigned value. -

-

-Implementation restriction: A compiler may implement ideal numbers -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. -

Character literals

-A character literal represents an integer value, typically a -Unicode code point, as one or more characters enclosed in single +A character literal represents an integer constant, +typically a Unicode code point, as one or more characters enclosed in single quotes. Within the quotes, any character may appear except single quote and newline. A single quoted character represents itself, while multi-character sequences beginning with a backslash encode @@ -389,6 +373,7 @@ big_u_value = `\` "U" hex_digit hex_digit hex_digit hex_digit hex_digit hex_digit hex_digit hex_digit . escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) . +

 'a'
 'ä'
@@ -403,19 +388,13 @@ escaped_char     = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `
 '\U00101234'
 
-

-The value of a character literal is an ideal integer, just as with -integer literals. -

String literals

-String literals represent ideal string values. Ideal strings do not -have a named type but they are compatible with type string -(§Type identity and compatibility). -There are two forms: raw string literals and interpreted string -literals. +A string literal represents a string constant +obtained from concatenating a sequence of characters. There are two forms: +raw string literals and interpreted string literals.

Raw string literals are character sequences between back quotes @@ -486,14 +465,63 @@ point), and will appear as two code points if placed in a string literal.

-

Boolean literals

+ +

Constants

+ +

There are boolean constants, integer constants, floating-point constants, +and string constants. Integer and floating-point constants are +collectively called numeric constants. +

-A boolean literal is one of the predeclared constants -true or false. The value of a boolean -literal is an ideal bool. +A constant value is represented by an +integer, +floating-point, +character, or +string literal, +an identifier denoting a constant, +a constant expression, or +the result value of some built-in functions such as unsafe.Sizeof +and cap or len applied to an array, +or len applied to a string constant. +The boolean truth values are represented by the predeclared constants +true and false. The predeclared identifier +iota denotes an integer constant.

+

+Numeric constants represent values of arbitrary precision that +have no size and cannot overflow. +

+ +

+Constants may be typed or untyped. +Literal constants, true, false, iota, +and certain constant expressions +containing only untyped constant operands are untyped. +

+ +

+A constant may be given a type explicitly by a constant declaration +or conversion, or implicitly when used in a +variable declaration or an +assignment or as an +operand in an expression. +It is an error if the constant value +cannot be accurately represented as a value of the respective type. +For instance, 3.0 can be given any integer type but also any +floating-point type, while -1e12 can be given the types +float32, float64, or even int64 but +not uint64 or string. +

+ +

+Implementation restriction: A compiler may implement numeric constants by choosing +an internal representation with at least twice as many bits as any machine type; +for floating-point values, both the mantissa and exponent must be twice as large. +

+ +

Types

@@ -511,16 +539,14 @@ TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType

-Basic types such as int are predeclared (§Predeclared identifiers). -Other types may be constructed from these, recursively, -including arrays, structs, pointers, functions, interfaces, slices, maps, and -channels. +Named instances of the boolean, numeric, and string types are predeclared. +Array, struct, pointer, function, interface, slice, map, and channel types may be constructed using type literals.

A type may have a method set associated with it (§Interface types, §Method declarations). -The method set of an interface type (§Interface types) is its interface. +The method set of an interface type is its interface. The method set of any other named type T consists of all methods with receiver type T. @@ -532,23 +558,26 @@ Any other type has an empty method set.

The static type (or just type) of a variable is the type defined by its declaration. Variables of interface type -(§Interface types) also have a distinct dynamic type, which +also have a distinct dynamic type, which is the actual type of the value stored in the variable at run-time. -The dynamic type may vary during execution but is always compatible -with the static type of the interface variable. For non-interface +The dynamic type may vary during execution but is always assignment compatible +to the static type of the interface variable. For non-interface types, the dynamic type is always the static type.

-

Basic types

-

-Basic types include traditional numeric types, booleans, and strings. All are predeclared. -

+

Boolean types

+ +A boolean type represents the set of Boolean truth values +denoted by the predeclared constants true +and false. The predeclared boolean type is bool. +

Numeric types

-The architecture-independent numeric types are: +A numeric type represents sets of integer or floating-point values. +The predeclared architecture-independent numeric types are:

@@ -562,8 +591,8 @@ int16    the set of all signed 16-bit integers (-32768 to 32767)
 int32    the set of all signed 32-bit integers (-2147483648 to 2147483647)
 int64    the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
 
-float32  the set of all valid IEEE-754 32-bit floating point numbers
-float64  the set of all valid IEEE-754 64-bit floating point numbers
+float32  the set of all IEEE-754 32-bit floating-point numbers
+float64  the set of all IEEE-754 64-bit floating-point numbers
 
 byte     familiar alias for uint8
 
@@ -575,7 +604,7 @@ as the two's complement of its absolute value.

-There is also a set of numeric types with implementation-specific sizes: +There is also a set of predeclared numeric types with implementation-specific sizes:

@@ -595,19 +624,13 @@ are not the same type even though they may have the same size on a
 particular architecture.
 
 
-

Booleans

- -The type bool comprises the Boolean truth values -represented by the predeclared constants true -and false. - - -

Strings

+

String types

-The string type represents the set of string values. +A string type represents the set of string values. Strings behave like arrays of bytes but are immutable: once created, it is impossible to change the contents of a string. +The predeclared string type is string.

The elements of strings have type byte and may be @@ -616,7 +639,7 @@ 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). The length is a compile-time constant if s +len. The length is a compile-time constant if s is a string literal.

@@ -943,7 +966,7 @@ instance, all types implement the empty interface:

-interface { }
+interface{}
 

@@ -1002,7 +1025,7 @@ A map value may be nil.

-MapType     = "map" "[" KeyType "]" ValueType .
+MapType     = "map" "[" KeyType "]" ElementType .
 KeyType     = Type .
 ValueType   = Type .
 
@@ -1010,7 +1033,7 @@ ValueType = Type .

The comparison operators == and !=Comparison operators) must be fully defined for operands of the -key type; thus the key type must be a basic, pointer, interface, +key type; thus the key type must be a boolean, numeric, string, pointer, function, interface, map, or channel type. If the key type is an interface type, these comparison operators must be defined for the dynamic key values; failure will cause a run-time error. @@ -1109,9 +1132,7 @@ received, closed(c) returns true.

Two types may be identical, compatible, or incompatible. Two identical types are always compatible, but two compatible types may not be identical. -Go is type safe: a value of one type cannot be assigned to a variable of an -incompatible type, and two values of incompatible types cannot be mixed in -binary operations.

+

Type identity and compatibility

@@ -1212,34 +1233,46 @@ they have different field names.

Assignment compatibility

-Values of any type may always be assigned to variables -of compatible static type. Some types and values have conditions under which they may -be assigned to otherwise incompatible types: +A value v of static type V is assignment compatible +with a type T if one of the following conditions applies:

+ +

+An untyped constant v +is assignment compatible with type T if v +can be represented accurately as a value of type T. +

+ +

+The predeclared identifier nil is assignment compatible with any +pointer, function, slice, map, channel, or interface type and +represents the zero value for that type. +

+ +

+Any value may be assigned to the blank identifier. +

+

Comparison compatibility

@@ -1416,7 +1449,10 @@ Architecture-specific convenience types: float int uint uintptr Constants: - true false iota nil + true false iota + +Zero value: + nil Functions: cap close closed len make new panic panicln print println @@ -1448,7 +1484,7 @@ any other identifier but the declaration does not introduce a new binding.

-

Const declarations

+

Constant declarations

A constant declaration binds a list of identifiers (the names of @@ -1469,23 +1505,25 @@ ExpressionList = Expression { "," Expression } .

+If the type is present, all constants take the type specified, and +the expressions must be assignment compatible with that type. If the type is omitted, the constants take the -individual types of the corresponding expressions, which may be -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. +individual types of the corresponding expressions. +If the expression values are untyped constants, +the declared constants remain untyped and the constant identifiers +denote the constant values. For instance, if the expression is a +floating-point literal, the constant identifier denotes a floating-point +constant, even if the literal's fractional part is zero.

 const Pi float64 = 3.14159265358979323846
-const E = 2.718281828
+const zero = 0.0             // untyped floating-point constant
 const (
 	size int64 = 1024;
-	eof = -1;
+	eof = -1;            // untyped integer constant
 )
-const a, b, c = 3, 4, "foo"  // a = 3, b = 4, c = "foo"
+const a, b, c = 3, 4, "foo"  // a = 3, b = 4, c = "foo", untyped integer and string constants
 const u, v float = 0, 3      // u = 0.0, v = 3.0
 
@@ -1519,9 +1557,9 @@ const (

Within a constant declaration, the predeclared pseudo-constant -iota represents successive integers. It is reset to 0 -whenever the reserved word const appears in the source -and increments with each semicolon. It can be used to construct a +iota represents successive untyped integer +constants. It is reset to 0 whenever the reserved word const +appears in the source and increments with each semicolon. It can be used to construct a set of related constants:

@@ -1539,9 +1577,9 @@ const ( ) const ( - u = iota * 42; // u == 0 (ideal integer) - v float = iota * 42; // v == 42.0 (float) - w = iota * 42; // w == 84 (ideal integer) + u = iota * 42; // u == 0 (untyped integer constant) + v float = iota * 42; // v == 42.0 (float constant) + w = iota * 42; // w == 84 (untyped integer constant) ) const x = iota; // x == 0 (iota has been reset) @@ -1640,17 +1678,18 @@ of the expression list.

-If the type is absent and the corresponding expression is a constant -expression of ideal integer, float, string or bool type, the type of the -declared variable is int, float, -string, or bool respectively: +If the type is absent and the corresponding expression evaluates to an +untyped constant, the type of the declared variable +is bool, int, float, or string +respectively, depending on whether the value is a boolean, integer, +floating-point, or string constant:

+var b = true    // t has type bool
 var i = 0       // i has type int
-var f = 3.1415  // f has type float
+var f = 3.0     // f has type float
 var s = "OMDB"  // s has type string
-var t = true    // t has type bool
 

Short variable declarations

@@ -1792,8 +1831,7 @@ However, a function declared this way is not a method.

An expression specifies the computation of a value by applying -operators and functions to operands. An expression has a value -and a type. +operators and functions to operands.

Operands

@@ -1807,17 +1845,6 @@ BasicLit = int_lit | float_lit | char_lit | StringLit . -

Constants

- -

-A constant is a literal of a basic type -(including the predeclared constants true, false -and nil -and values denoted by iota) -or a constant expression (§Constant expressions). -Constants have values that are known at compile time. -

-

Qualified identifiers

@@ -2220,7 +2247,7 @@ or for a of type S where S is a string type: +where T is a string type: