You are on page 1of 119

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

1. -Source file organization 12.1


2. Package-clause 12.2
3. Import-Import declarations 12.3
4. package-An example package 12.4
14. -Program initialization and execution 13
1. ""-The zero value 13.1
2. package-Package initialization 13.2
3. -Program execution 13.3
15. Error-Errors 14
16. Run-time panics 15
17. -System considerations 16
1. -Package unsafe 16.1
2. Size and alignment guarantees 16.2

4
The Go Programming Language Specification

The-Go-Programming-Language-Specification
Go

fmt.Println("Don't be shy, just try!")

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 is a general-purpose language designed with systems programming


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
compile/link model to generate executable binaries.

Go.
.,./
.

The grammar is compact and regular, allowing for easy analysis by


automatic tools such as integrated development environments.

,,:.

-Introduction 6
The Go Programming Language Specification

-Notation
Notation
The syntax is specified using Extended Backus-Naur Form (EBNF):

Production = production_name "=" [ Expression ] "." .


Expression = Alternative { "|" Alternative } .
Alternative = Term { Term } .
Term = production_name | token [ "" token ] | Group | Option | Repetit
Group = "(" Expression ")" .
Option = "[" Expression "]" .
Repetition = "{" Expression "}" .

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

-Source code representation


Source code representation
Source code is Unicode text encoded in UTF-8. The text is not
canonicalized, so a single accented code point is distinct from the
same character constructed from combining an accent and a letter;
those are treated as two code points. For simplicity, this document
will use the unqualified term character to refer to a Unicode code
point in the source text.

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.

Implementation restriction: For compatibility with other tools, a


compiler may disallow the NUL character (U+0000) in the source text.

:,NUL(U+0000)

Implementation restriction: For compatibility with other tools, a


compiler may ignore a UTF-8-encoded byte order mark (U+FEFF) if it
is the first Unicode code point in the source text. A byte order
mark may be disallowed anywhere else in the source.

:,BOM.BOM.

-Source code representation 8


The Go Programming Language Specification

-Characters
Characters
The following terms are used to denote specific Unicode character
classes:

Unicode

newline = /* the Unicode code point U+000A */ .


unicode_char = /* an arbitrary Unicode code point except newline */ .
unicode_letter = /* a Unicode code point classified as "Letter" */ .
unicode_digit = /* a Unicode code point classified as "Decimal Digit" */ .

In [The Unicode Standard 6.3](http://t.cn/zRz14zg), Section 4.5


"General Category" 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.

Unicode6.3,4.5 "General Category" .Go


UnicodeUnicode

-Characters 9
The Go Programming Language Specification

-Letters and digits


Letters and digits
The underscore character _ (U+005F) is considered a letter.

(U+005F)

letter = unicode_letter | "_" .


decimal_digit = "0" "9" .
octal_digit = "0" "7" .
hex_digit = "0" "9" | "A" "F" | "a" "f" .

-Letters and digits 10


The Go Programming Language Specification

-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

When the input is broken into tokens, a semicolon is automatically inserted in


an identifier
an integer, floating-point, imaginary, rune, or string literal
one of the keywords break, continue, fallthrough, or return
one of the operators and delimiters ++, --, ), ], or }
To allow complex statements to occupy a single line, a semicolon may be omitte
To reflect idiomatic use, code examples in this document elide semicolons usin

-Semicolons 13
The Go Programming Language Specification

-Identifiers
Identifiers
Identifiers name program entities such as variables and types. An identifier i

identifier = letter { letter | unicode_digit } .


a
_x9
ThisVariableIsExported

= { | unicode } .

Some identifiers are predeclared.

-Identifiers 14
The Go Programming Language Specification

-Keywords
Keywords
The following keywords are reserved and may not be used as
identifiers.

break default func interface select


case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var

-Keywords 15
The Go Programming Language Specification

-Operators and Delimiters


Operators and Delimiters
The following character sequences represent operators, delimiters, and other s

token

+ & += &= && == != ( )


- | -= |= || < <= [ ]
* ^ *= ^= <- > >= { }
/ << /= <<= ++ = := , ;
% >> %= >>= -- ! ... . :
&^ &^=

-Operators and Delimiters 16


The Go Programming Language Specification

-Integer literals
Integer literals
An integer literal is a sequence of digits representing an integer constant. A

.0()0x0X()
a-f A-F 1015.

int_lit = decimal_lit | octal_lit | hex_lit .


decimal_lit = ( "1" "9" ) { decimal_digit } .
octal_lit = "0" { octal_digit } .
hex_lit = "0" ( "x" | "X" ) hex_digit { hex_digit } .
42
0600
0xBadFace
170141183460469231731687303715884105727

-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)

float_lit = decimals "." [ decimals ] [ exponent ] |


decimals exponent |
"." decimals [ exponent ] .
decimals = decimal_digit { decimal_digit } .
exponent = ( "e" | "E" ) [ "+" | "-" ] decimals .
0.
72.40
072.40 // == 72.40
2.71828
1.e+0
6.67428e-11
1E6
.25
.12345E+5

-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-Properties of types and values


...

Type-Properties of types and values 37


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

-Declarations and scope


...

-Declarations and scope 41


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

-Short variable declarations


...

-Short variable declarations 51


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

-Passing arguments to ...


parameters
...

-Passing arguments to ... parameters 67


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

-Length and capacity


...

-Length and capacity 98


The Go Programming Language Specification

-Allocation
...

-Allocation 99
The Go Programming Language Specification

slicemapchannel-Making slices, maps


and channels
...

slicemapchannel-Making slices, maps and channels 100


The Go Programming Language Specification

-Appending to and copying


slices
...

-Appending to and copying slices 101


The Go Programming Language Specification

map-Deletion of map elements


...

map-Deletion of map elements 102


The Go Programming Language Specification

-Manipulating complex numbers


...

-Manipulating complex numbers 103


The Go Programming Language Specification

panic-Handling panics
...

panic-Handling panics 104


The Go Programming Language Specification

Bootstrapping
...

Bootstrapping 105
The Go Programming Language Specification

-Packages
...

-Packages 106
The Go Programming Language Specification

-Source file organization


...

-Source file organization 107


The Go Programming Language Specification

Package-clause
...

Package-clause 108
The Go Programming Language Specification

Import-Import declarations
...

Import-Import declarations 109


The Go Programming Language Specification

package-An example package


...

package-An example package 110


The Go Programming Language Specification

-Program initialization
and execution
...

-Program initialization and execution 111


The Go Programming Language Specification

""-The zero value


...

""-The zero value 112


The Go Programming Language Specification

package-Package initialization
...

package-Package initialization 113


The Go Programming Language Specification

-Program execution
...

-Program execution 114


The Go Programming Language Specification

Error-Errors
...

Error-Errors 115
The Go Programming Language Specification

Run-time panics
...

Run-time panics 116


The Go Programming Language Specification

-System considerations
...

-System considerations 117


The Go Programming Language Specification

-Package unsafe
...

-Package unsafe 118


The Go Programming Language Specification

Size and alignment guarantees


...

Size and alignment guarantees 119

You might also like