From: Robert Griesemer
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,
-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:
-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
-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.
-
-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" | `\` | "'" | `"` ) .
+
-The value of a character literal is an ideal integer, just as with
-integer literals.
-
-String literals represent ideal string values. Ideal strings do not
-have a named type but they are compatible with type
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.
There are boolean constants, integer constants, floating-point constants,
+and string constants. Integer and floating-point constants are
+collectively called numeric constants.
+
+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
-A boolean literal is one of the predeclared constants
-
+Constants may be typed or untyped.
+Literal constants,
+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,
+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.
+
@@ -511,16 +539,14 @@ TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType
-Basic types such as
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
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 include traditional numeric types, booleans, and strings. All are predeclared.
-
-The architecture-independent numeric types are:
+A numeric type represents sets of integer or floating-point values.
+The predeclared architecture-independent numeric types are:
Integer literals
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.
Floating-point literals
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
-
-Character literals
'a'
'ä'
@@ -403,19 +388,13 @@ escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `
'\U00101234'
-String literals
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.
Boolean literals
+
+Constants
+
+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.
+true
or false
. The value of a boolean
-literal is an ideal bool.
+Numeric constants represent values of arbitrary precision that
+have no size and cannot overflow.
true
, false
, iota
,
+and certain constant expressions
+containing only untyped constant operands are untyped.
+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
.
+Types
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.
T
consists of all methods with receiver
type T
.
@@ -532,23 +558,26 @@ Any other type has an empty method set.
Basic types
-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
@@ -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 typebool
comprises the Boolean truth values -represented by the predeclared constantstrue
-andfalse
. - - -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 isstring
.The elements of strings have type
@@ -943,7 +966,7 @@ instance, all types implement the empty interface:byte
and may be @@ -616,7 +639,7 @@ illegal to take the address of such an element; ifs[i]
is the ith byte of a string,&s[i]
is invalid. The length of strings
can be discovered using the built-in function -len(s)
. The length is a compile-time constant ifs
+len
. The length is a compile-time constant ifs
is a string literal.-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 typeV
is assignment compatible +with a typeT
if one of the following conditions applies:
V
is compatible with T
.
nil
can be assigned to any
-pointer, function, slice, map, channel, or interface variable.
-p
to an array can be assigned to a slice variable
-v
with compatible element type
-if the type of p
or v
is unnamed.
-The slice variable then refers to the original array; the data is not copied.
+T
is an interface type and
+V
implements T
.
c
can be assigned to a channel variable
-v
with compatible channel value type
-if the type of c
or v
is unnamed.
+V
is a pointer to an array and T
is a slice type
+with compatible element type and at least one of V
or T
is unnamed.
+After assignment, the slice variable refers to the original array; the elements are not
+copied.
V
is a bidirectional channel and T
is a channel type
+with compatible element type and at least one of V
or T
is unnamed.
+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. +
+@@ -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.
-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:
-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
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.
-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.
-
@@ -2220,7 +2247,7 @@ or for a
of type S
where S
is a string type:
+where T
is a string type:
x
must be an integer value and 0 <= x < len(a)
@@ -2307,15 +2334,18 @@ s[1] == 3
-The slice length must be non-negative.
+The slice length must not be negative.
For arrays or strings, the indexes
lo
and hi
must satisfy
0 <= lo
<= hi
<= length;
for slices, the upper bound is the capacity rather than the length.
+
-If the sliced operand is a string, the result of the slice operation is another, new -string. If the sliced operand is an array or slice, the result -of the slice operation is a slice. +If the sliced operand is a string or slice, the result of the slice operation +is a string or slice of the same type. +If the sliced operand is an array, the result of the slice operation is a slice +with the same element type as the array.
@@ -2482,13 +2512,12 @@ unary_op = "+" | "-" | "!" | "^" | "*" | "&" | "<-" .-Comparisons are discussed elsewhere -(§Comparison compatibility). -For other binary operators, the -operand types must be identical +Comparisons are discussed elsewhere. +For other binary operators, the operand types must be identical (§Properties of types and values) -unless the operation involves -channels, shifts, or ideal constants. +unless the operation involves channels, shifts, or untyped constants. +For operations involving constants only, see the section on +constant expressions.
@@ -2498,24 +2527,20 @@ second is a value of the channel's element type.
Except for shift operations,
-if one operand has ideal type and the other operand does not,
-the ideal operand is converted to match the type of
-the other operand (§Expressions).
-If both operands are ideal numbers and one is an
-ideal float, the other is converted to ideal float
-(relevant for /
and %
).
+if one operand is an untyped constant
+and the other operand is not, the constant is converted
+to the type of the other operand.
The right operand in a shift operation must have unsigned integer type -or be an ideal number that can be converted to unsigned integer type -(§Arithmetic operators). +or be an untyped constant that can be converted to unsigned integer type.
-If the left operand of a non-constant shift operation is an ideal number, -the type of the ideal number -is what it would be if the shift operation were replaced by the left operand alone. +If the left operand of a non-constant shift operation is an untyped constant, +the type of constant is what it would be if the shift operation were replaced by +the left operand alone.
@@ -2568,11 +2593,11 @@ x == y+1 && <-chan_ptr > 0Arithmetic operators
-Arithmetic operators apply to numeric types and yield a result of the same +Arithmetic operators apply to numeric values and yield a result of the same type as the first operand. The four standard arithmetic operators (
+
, --
,*
,/
) apply both to integer and -floating point types, while+
applies also -to strings; all other arithmetic operators apply to integers only. +-
,*
,/
) apply to integer and +floating-point types;+
also applies +to strings. All other arithmetic operators apply to integers only.@@ -2663,7 +2688,7 @@ follows:-For floating point numbers, +For floating-point numbers,
@@ -2692,11 +2717,10 @@ not occur. For instance, it may not assume that+x
is the same asx
, while-x
is the negation ofx
.x < x + 1
is alwComparison operators
-Comparison operators yield a boolean result. +Comparison operators yield a value of type
bool
. 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 exceptbool
. +to operands of all types except arrays and structs. +All other comparison operators apply only to numeric and string values.@@ -2709,13 +2733,14 @@ to basic types exceptbool
.-Numeric basic types are compared in the usual way. +Operands of numeric type are compared in the usual way.
-Strings are compared byte-wise (lexically). +Operands of string type are compared byte-wise (lexically).
-Booleans are equal if they are either both "true" or both "false". +Operands of boolean type are equal if they are either both
true
+or bothfalse
.The rules for comparison of composite types are described in the @@ -2726,7 +2751,8 @@ section on §Comparison compatibility.
Logical operators
-Logical operators apply to boolean operands and yield a boolean result. +Logical operators apply to boolean values +and yield a result of the same type as the operands. The right operand is evaluated conditionally.
@@ -2972,36 +2998,31 @@ The resulting function takes an explicit receiver of that interface type.Constant expressions
-Constant expressions may contain only constants,
iota
, -numeric literals, string literals, and -some constant-valued built-in functions such asunsafe.Sizeof
-andlen
applied to an array. -In practice, constant expressions are those that can be evaluated at compile time. --The type of a constant expression is determined by the type of its -elements. If it contains only numeric literals, its type is ideal -integer or ideal float (§Ideal numbers). Whether a literal -is an integer or float depends on the syntax of the literals (123 vs. 123.0). -The nature of the arithmetic -operations within the expression depends, elementwise, on the values; -for example, 3/2 is an integer division yielding 1, while 3./2. is -a floating point division yielding 1.5. Thus +Constant expressions may contain only constant +operands and are evaluated at compile-time.
--const x = 3./2. + 3/2; ---yields a floating point constant of ideal float value 2.5 (1.5 + -1); its constituent expressions are evaluated using distinct rules -for division. +Untyped boolean, numeric, and string constants may be used as operands +wherever it is legal to use an operand of boolean, numeric, or string type, +respectively. Except for shift operations, if the operands of a binary operation +are an untyped integer constant and an untyped floating-point constant, +the integer constant is converted to an untyped floating-point constant +(relevant for
/
and%
). ++ +
+Applying an operator to untyped constants results in an untyped +constant of the same kind (that is, a boolean, integer, floating-point, or +string constant), except for +comparison operators which result in +a constant of typebool
.-Intermediate values and the constants themselves -may require precision significantly larger than any concrete type -in the language. The following are legal declarations: +Constant expressions are always evaluated exactly; intermediate values and the +constants themselves may require precision significantly larger than supported +by any predeclared type in the language. The following are legal declarations:
@@ -3010,38 +3031,26 @@ const Four int8 = Huge >> 98;-A constant expression may appear in any context, such as assignment -to a variable of any numeric type, as long as the value of the -expression can be represented accurately in that context. -It is erroneous to assign a value with a non-zero fractional part -to an integer, or if the assignment would overflow or underflow, -or in general if the value cannot be represented by the type of -the variable. -For -instance,
- -3
can be assigned to any integer variable but also to any -floating point variable, while-1e12
can be assigned to a -float32
,float64
, or evenint64
-but notuint64
orstring
. --If a typed constant expression evaluates to a value that is not -representable by that type, the compiler reports an error. +The values of typed constants must always be accurately representable as values +of the constant type. The following constant expressions are illegal:
-uint8(-1) // error, out of range -uint8(100) * 100 // error, out of range +uint(-1) // -1 overflows uint +int(3.14) // 3.14 truncated to integer +int64(Huge) // 1<<100 overflows int64 +Four * 300 // 300 overflows int8 +Four * 100 // 400 overflows int8-The mask used by the unary bitwise complement operator matches +The mask used by the unary bitwise complement operator
^
matches the rule for non-constants: the mask is all 1s for unsigned constants -and -1 for signed and ideal constants. +and -1 for signed and untyped constants.-^1 // ideal constant, equal to -2 +^1 // untyped integer constant, equal to -2 uint8(^1) // error, same as uint8(-2), out of range ^uint8(1) // typed uint8 constant, same as 0xFF ^ uint8(1) = uint8(0xFE) int8(^1) // same as int8(-2) @@ -3056,6 +3065,7 @@ overflow etc. errors being caught. +Order of evaluation
@@ -3164,8 +3174,9 @@ f(x+y)
The "++" and "--" statements increment or decrement their operands -by the ideal numeric value 1. As with an assignment, the operand -must be a variable, pointer indirection, field selector or index expression. +by the untyped constant
1
. +As with an assignment, the operand must be a variable, pointer indirection, +field selector or index expression.@@ -3259,9 +3270,13 @@ a, b = b, a // exchange a and b-In assignments, the type of each value must be +In assignments, each value must be assignment compatible with the type of the -operand to which it is assigned. +operand to which it is assigned. If an untyped constant +is assigned to a variable of interface type, the constant is converted +to type
@@ -3599,7 +3614,7 @@ for i, s := range a { } var key string; -var val interface {}; // value type of m is assignment-compatible to val +var val interface {}; // value type of m is assignment compatible to val for key, val = range m { h(key, val) } @@ -3967,32 +3982,26 @@ The following conversion rules apply:bool
,int
,float
, orstring
+respectively, depending on whether the value is a boolean, integer, floating-point, +or string constant.
T
.
uint8(0xFF)
is legal but int8(0xFF)
is not.
+2) The conversion succeeds if the value would be assignment compatible
+with type T
if the value's type, or T
, or any of their component
+types were unnamed (§Type identity and compatibility).
x := uint16(0x10F0)
, then uint32(int8(x)) == 0xFFFFFFF0
.
The conversion always yields a valid value; there is no indication of overflow.
There is no linguistic mechanism to convert between pointers and integers.
-The unsafe
package
+The package unsafe
implements this functionality under
-restricted circumstances (§Package unsafe
).
+restricted circumstances.
false
for booleans,
0
for integers, 0.0
for floats, ""
-for strings, and nil
for pointers, interfaces, slices, channels, and maps.
+for strings, and nil
for pointers, functions, 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.