The Go Programming Language Specification
The Go Programming Language Specification
1. 0
2. -Introduction 1
3. -Notation 2
4. -Source code representation 3
1. -Characters 3.1
2. -Letters and digits 3.2
5. -Lexical elements 4
1. -Tokens 4.1
2. -Semicolons 4.2
3. -Identifiers 4.3
4. -Keywords 4.4
5. -Operators and Delimiters 4.5
6. -Integer literals 4.6
7. -Floating-point literals 4.7
8. -Imaginary literals 4.8
9. Rune literals 4.9
10. -String literals 4.10
6. -Constants 5
7. -Variables 6
1. Type-Types 6.1
2. -Method sets 6.2
3. -Boolean types 6.3
4. -Numeric types 6.4
5. -String types 6.5
6. -Array types 6.6
7. -Slice types 6.7
8. -Struct types 6.8
9. -Pointer types 6.9
10. -Function types 6.10
11. -Interface types 6.11
12. -Map types 6.12
13. Channel-Channel types 6.13
14. Type-Properties of types and values 6.14
15. Type-Type identity 6.15
16. -Assignability 6.16
8. -Blocks 7
9. -Declarations and scope 8
1. -Label scopes 8.1
2. -Blank identifier 8.2
3. -Predeclared identifiers 8.3
4. -Exported identifiers 8.4
5. Uniqueness of identifiers 8.5
6. -Constant declarations 8.6
7. Iota-Iota 8.7
8. Type-Type declarations 8.8
9. -Variable declarations 8.9
10. -Short variable declarations 8.10
11. -Function declarations 8.11
12. -Method declarations 8.12
2
The Go Programming Language Specification
10. -Expressions 9
1. -Operands 9.1
2. Qualified identifiers 9.2
3. -Composite literals 9.3
4. Function literals 9.4
5. -Primary expressions 9.5
6. Selectors-Selectors 9.6
7. -Method expressions 9.7
8. -Method values 9.8
9. -Index expressions 9.9
10. -Slice expressions 9.10
11. type-Type assertions 9.11
12. -Calls 9.12
13. -Passing arguments to ... parameters 9.13
14. -Operators 9.14
15. -Arithmetic operators 9.15
16. -Comparison operators 9.16
17. -Logical operators 9.17
18. -Address operators 9.18
19. Receive-Receive operator 9.19
20. -Conversions 9.20
21. -Constant expressions 9.21
22. Order of evaluation 9.22
11. -Statements 10
1. -Terminating statements 10.1
2. -Empty statements 10.2
3. Labeled statements 10.3
4. -Expression statements 10.4
5. Send statements 10.5
6. -IncDec statements 10.6
7. Assignments 10.7
8. if-If statements 10.8
9. switch-Switch statements 10.9
10. for-For statements 10.10
11. go-Go statements 10.11
12. select-Select statements 10.12
13. return-Return statements 10.13
14. break-Break statements 10.14
15. contine-Continue statements 10.15
16. goto-Goto statements 10.16
17. falthrough-Fallthrough statements 10.17
18. defer-Defer statements 10.18
12. Built-in functions 11
1. Close 11.1
2. -Length and capacity 11.2
3. -Allocation 11.3
4. slicemapchannel-Making slices, maps and channels 11.4
5. -Appending to and copying slices 11.5
6. map-Deletion of map elements 11.6
7. -Manipulating complex numbers 11.7
8. panic-Handling panics 11.8
9. Bootstrapping 11.9
13. -Packages 12
3
The Go Programming Language Specification
4
The Go Programming Language Specification
The-Go-Programming-Language-Specification
Go
5
The Go Programming Language Specification
-Introduction
Introduction
This is a reference manual for the Go programming language. For more
information and other documents, see golang.org.
Go.golang.org
Go.
.,./
.
,,:.
-Introduction 6
The Go Programming Language Specification
-Notation
Notation
The syntax is specified using Extended Backus-Naur Form (EBNF):
Productions are expressions constructed from terms and the following operators, in increasing
precedence:
| alternation
() grouping
[] option (0 or 1 times)
{} repetition (0 to n times)
Lower-case production names are used to identify lexical tokens. Non-terminals are in CamelCase.
Lexical tokens are enclosed in double quotes "" or back quotes ``.
The form a b represents the set of characters from a through b as alternatives. The horizontal
ellipsis is also used elsewhere in the spec to informally denote various enumerations or code
snippets that are not further specified. The character (as opposed to the three characters ...) is not a
token of the Go language.
-Notation 7
The Go Programming Language Specification
UnicodeUTF-8(UTF-8Unicode).Go
(:Unicode),.
(: \u00e9\u0065\u0301 go,Googl Groups
).,Unicode.
Each code point is distinct; for instance, upper and lower case
letters are different characters.
:,NUL(U+0000)
:,BOM.BOM.
-Characters
Characters
The following terms are used to denote specific Unicode character
classes:
Unicode
-Characters 9
The Go Programming Language Specification
(U+005F)
-Lexical elements
Lexical elements
Comments
Comments serve as program documentation. There are two forms:
Line comments start with the character sequence // and stop at the end of the
General comments start with the character sequence /* and stop with the first
A comment cannot start inside a rune or string literal, or inside a comment. A
//
/ /
....
-Lexical elements 11
The Go Programming Language Specification
-Tokens
Tokens
Tokens form the vocabulary of the Go language. There are four classes: identif
Tokens Go.(,)
,(http://baike.baidu.com/view/1208327.htm)([
](http://baike.baidu.com/view/1208327.htm),go)
Go
-Tokens 12
The Go Programming Language Specification
-Semicolons
Semicolons
The formal grammar uses semicolons ";" as terminators in a number of productio
";"Go
-Semicolons 13
The Go Programming Language Specification
-Identifiers
Identifiers
Identifiers name program entities such as variables and types. An identifier i
= { | unicode } .
-Identifiers 14
The Go Programming Language Specification
-Keywords
Keywords
The following keywords are reserved and may not be used as
identifiers.
-Keywords 15
The Go Programming Language Specification
token
-Integer literals
Integer literals
An integer literal is a sequence of digits representing an integer constant. A
.0()0x0X()
a-f A-F 1015.
-Integer literals 17
The Go Programming Language Specification
-Floating-point literals
Floating-point literals
A floating-point literal is a decimal representation of a floating-point const
..
eEshi ji d s fa g d d
f (:.25 2. )(:2 1)
-Floating-point literals 18
The Go Programming Language Specification
-Imaginary literals
...
-Imaginary literals 19
The Go Programming Language Specification
Rune literals
...
Rune literals 20
The Go Programming Language Specification
-String literals
...
-String literals 21
The Go Programming Language Specification
-Constants
...
-Constants 22
The Go Programming Language Specification
-Variables
...
-Variables 23
The Go Programming Language Specification
Type-Types
...
Type-Types 24
The Go Programming Language Specification
-Method sets
...
-Method sets 25
The Go Programming Language Specification
-Boolean types
...
-Boolean types 26
The Go Programming Language Specification
-Numeric types
...
-Numeric types 27
The Go Programming Language Specification
-String types
...
-String types 28
The Go Programming Language Specification
-Array types
...
-Array types 29
The Go Programming Language Specification
-Slice types
...
-Slice types 30
The Go Programming Language Specification
-Struct types
...
-Struct types 31
The Go Programming Language Specification
-Pointer types
...
-Pointer types 32
The Go Programming Language Specification
-Function types
...
-Function types 33
The Go Programming Language Specification
-Interface types
...
-Interface types 34
The Go Programming Language Specification
-Map types
...
-Map types 35
The Go Programming Language Specification
Channel-Channel types
...
Channel-Channel types 36
The Go Programming Language Specification
Type-Type identity
...
Type-Type identity 38
The Go Programming Language Specification
-Assignability
...
-Assignability 39
The Go Programming Language Specification
-Blocks
...
-Blocks 40
The Go Programming Language Specification
-Label scopes
...
-Label scopes 42
The Go Programming Language Specification
-Blank identifier
...
-Blank identifier 43
The Go Programming Language Specification
-Predeclared identifiers
...
-Predeclared identifiers 44
The Go Programming Language Specification
-Exported identifiers
...
-Exported identifiers 45
The Go Programming Language Specification
Uniqueness of identifiers
...
Uniqueness of identifiers 46
The Go Programming Language Specification
-Constant declarations
...
-Constant declarations 47
The Go Programming Language Specification
Iota-Iota
...
Iota-Iota 48
The Go Programming Language Specification
Type-Type declarations
...
Type-Type declarations 49
The Go Programming Language Specification
-Variable declarations
...
-Variable declarations 50
The Go Programming Language Specification
-Function declarations
...
-Function declarations 52
The Go Programming Language Specification
-Method declarations
...
-Method declarations 53
The Go Programming Language Specification
-Expressions
...
-Expressions 54
The Go Programming Language Specification
-Operands
...
-Operands 55
The Go Programming Language Specification
Qualified identifiers
...
Qualified identifiers 56
The Go Programming Language Specification
-Composite literals
...
-Composite literals 57
The Go Programming Language Specification
Function literals
...
Function literals 58
The Go Programming Language Specification
-Primary expressions
...
-Primary expressions 59
The Go Programming Language Specification
Selectors-Selectors
...
Selectors-Selectors 60
The Go Programming Language Specification
-Method expressions
...
-Method expressions 61
The Go Programming Language Specification
-Method values
...
-Method values 62
The Go Programming Language Specification
-Index expressions
...
-Index expressions 63
The Go Programming Language Specification
-Slice expressions
...
-Slice expressions 64
The Go Programming Language Specification
type-Type assertions
...
type-Type assertions 65
The Go Programming Language Specification
-Calls
...
-Calls 66
The Go Programming Language Specification
-Operators
...
-Operators 68
The Go Programming Language Specification
-Arithmetic operators
...
-Arithmetic operators 69
The Go Programming Language Specification
-Comparison operators
...
-Comparison operators 70
The Go Programming Language Specification
-Logical operators
...
-Logical operators 71
The Go Programming Language Specification
-Address operators
...
-Address operators 72
The Go Programming Language Specification
Receive-Receive operator
...
Receive-Receive operator 73
The Go Programming Language Specification
-Conversions
...
-Conversions 74
The Go Programming Language Specification
-Constant expressions
...
-Constant expressions 75
The Go Programming Language Specification
Order of evaluation
...
Order of evaluation 76
The Go Programming Language Specification
-Statements
...
-Statements 77
The Go Programming Language Specification
-Terminating statements
...
-Terminating statements 78
The Go Programming Language Specification
-Empty statements
...
-Empty statements 79
The Go Programming Language Specification
Labeled statements
...
Labeled statements 80
The Go Programming Language Specification
-Expression statements
...
-Expression statements 81
The Go Programming Language Specification
Send statements
...
Send statements 82
The Go Programming Language Specification
-IncDec statements
...
-IncDec statements 83
The Go Programming Language Specification
Assignments
...
Assignments 84
The Go Programming Language Specification
if-If statements
...
if-If statements 85
The Go Programming Language Specification
switch-Switch statements
...
switch-Switch statements 86
The Go Programming Language Specification
for-For statements
...
for-For statements 87
The Go Programming Language Specification
go-Go statements
...
go-Go statements 88
The Go Programming Language Specification
select-Select statements
...
select-Select statements 89
The Go Programming Language Specification
return-Return statements
...
return-Return statements 90
The Go Programming Language Specification
break-Break statements
...
break-Break statements 91
The Go Programming Language Specification
contine-Continue statements
...
contine-Continue statements 92
The Go Programming Language Specification
goto-Goto statements
...
goto-Goto statements 93
The Go Programming Language Specification
falthrough-Fallthrough statements
...
falthrough-Fallthrough statements 94
The Go Programming Language Specification
defer-Defer statements
...
defer-Defer statements 95
The Go Programming Language Specification
Built-in functions
...
Built-in functions 96
The Go Programming Language Specification
Close
...
Close 97
The Go Programming Language Specification
-Allocation
...
-Allocation 99
The Go Programming Language Specification
panic-Handling panics
...
Bootstrapping
...
Bootstrapping 105
The Go Programming Language Specification
-Packages
...
-Packages 106
The Go Programming Language Specification
Package-clause
...
Package-clause 108
The Go Programming Language Specification
Import-Import declarations
...
-Program initialization
and execution
...
package-Package initialization
...
-Program execution
...
Error-Errors
...
Error-Errors 115
The Go Programming Language Specification
Run-time panics
...
-System considerations
...
-Package unsafe
...