<p>
Lower-case production names are used to identify lexical tokens.
Non-terminals are in CamelCase. Lexical symbols are enclosed in
-double quotes <code>""</code> (the double quote symbol is written as
-<code>'"'</code>).
+double <code>""</code> or back quotes <code>``</code>.
</p>
<p>
-The form <code>"a ... b"</code> represents the set of characters from
+The form <code>a ... b</code> represents the set of characters from
<code>a</code> through <code>b</code> as alternatives.
</p>
<p>
The following terms are used to denote specific Unicode character classes:
</p>
-<ul>
- <li>unicode_char an arbitrary Unicode code point</li>
- <li>unicode_letter a Unicode code point classified as "Letter"</li>
- <li>capital_letter a Unicode code point classified as "Letter, uppercase"</li>
- <li>unicode_digit a Unicode code point classified as "Digit"</li>
-</ul>
+<pre class="ebnf">
+unicode_char = /* an arbitrary Unicode code point */ .
+unicode_letter = /* a Unicode code point classified as "Letter" */ .
+capital_letter = /* a Unicode code point classified as "Letter, uppercase" */ .
+unicode_digit = /* a Unicode code point classified as "Digit" */ .
+</pre>
(The Unicode Standard, Section 4.5 General Category - Normative.)
<p>
The underscore character <code>_</code> (U+005F) is considered a letter.
-</>
-<pre class="grammar">
+</p>
+<pre class="ebnf">
letter = unicode_letter | "_" .
decimal_digit = "0" ... "9" .
octal_digit = "0" ... "7" .
An identifier is a sequence of one or more letters and digits.
The first character in an identifier must be a letter.
</p>
-<pre class="grammar">
+<pre class="ebnf">
identifier = letter { letter | unicode_digit } .
</pre>
<pre>
<code>0X</code> for hexadecimal. In hexadecimal literals, letters
<code>a-f</code> and <code>A-F</code> represent values 10 through 15.
</p>
-<pre class="grammar">
+<pre class="ebnf">
int_lit = decimal_lit | octal_lit | hex_lit .
decimal_lit = ( "1" ... "9" ) { decimal_digit } .
octal_lit = "0" { octal_digit } .
integer part or the fractional part may be elided; one of the decimal
point or the exponent may be elided.
</p>
-<pre class="grammar">
+<pre class="ebnf">
float_lit = decimals "." [ decimals ] [ exponent ] |
decimals exponent |
"." decimals [ exponent ] .
<p>
All other sequences are illegal inside character literals.
</p>
-<pre class="grammar">
+<pre class="ebnf">
char_lit = "'" ( unicode_value | byte_value ) "'" .
unicode_value = unicode_char | little_u_value | big_u_value | escaped_char .
byte_value = octal_byte_value | hex_byte_value .
-octal_byte_value = "\" octal_digit octal_digit octal_digit .
-hex_byte_value = "\" "x" hex_digit hex_digit .
-little_u_value = "\" "u" hex_digit hex_digit hex_digit hex_digit .
-big_u_value = "\" "U" hex_digit hex_digit hex_digit hex_digit
+octal_byte_value = `\` octal_digit octal_digit octal_digit .
+hex_byte_value = `\` "x" hex_digit hex_digit .
+little_u_value = `\` "u" hex_digit hex_digit hex_digit hex_digit .
+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" | "\" | "'" | """ ) .
+escaped_char = `\` ( "a" | "b" | "f" | "n" | "r" | "t" | "v" | `\` | "'" | `"` ) .
</pre>
<pre>
'a'
A sequence of string literals is concatenated to form a single string.
</p>
-<pre class="grammar">
+<pre class="ebnf">
StringLit = string_lit { string_lit } .
string_lit = raw_string_lit | interpreted_string_lit .
raw_string_lit = "`" { unicode_char } "`" .
which composes a new type from previously declared types.
</p>
-<pre class="grammar">
+<pre class="ebnf">
Type = TypeName | TypeLit | "(" Type ")" .
TypeName = QualifiedIdent.
TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType |
negative.
</p>
-<pre class="grammar">
+<pre class="ebnf">
ArrayType = "[" ArrayLength "]" ElementType .
ArrayLength = Expression .
ElementType = CompleteType .
A slice value may be <code>nil</code>.
</p>
-<pre class="grammar">
+<pre class="ebnf">
SliceType = "[" "]" ElementType .
</pre>
must be unique and field types must be complete (§Types).
</p>
-<pre class="grammar">
+<pre class="ebnf">
StructType = "struct" "{" [ FieldDeclList ] "}" .
FieldDeclList = FieldDecl { ";" FieldDecl } [ ";" ] .
FieldDecl = (IdentifierList CompleteType | [ "*" ] TypeName) [ Tag ] .
A pointer value may be <code>nil</code>.
</p>
-<pre class="grammar">
+<pre class="ebnf">
PointerType = "*" BaseType .
BaseType = Type .
</pre>
A function value may be <code>nil</code>.
</p>
-<pre class="grammar">
+<pre class="ebnf">
FunctionType = "func" Signature .
Signature = Parameters [ Result ] .
Result = Parameters | CompleteType .
<i>implement the interface</i>. An interface value may be <code>nil</code>.
</p>
-<pre class="grammar">
+<pre class="ebnf">
InterfaceType = "interface" "{" [ MethodSpecList ] "}" .
MethodSpecList = MethodSpec { ";" MethodSpec } [ ";" ] .
MethodSpec = IdentifierList Signature | InterfaceTypeName .
</p>
-<pre class="grammar">
+<pre class="ebnf">
MapType = "map" "[" KeyType "]" ValueType .
KeyType = CompleteType .
ValueType = CompleteType .
A value of channel type may be <code>nil</code>.
</p>
-<pre class="grammar">
+<pre class="ebnf">
ChannelType = Channel | SendChannel | RecvChannel .
Channel = "chan" ValueType .
SendChannel = "chan" "<-" ValueType .
Every identifier in a program must be declared.
</p>
-<pre class="grammar">
+<pre class="ebnf">
Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | MethodDecl .
</pre>
right.
</p>
-<pre class="grammar">
+<pre class="ebnf">
ConstDecl = "const" ( ConstSpec | "(" [ ConstSpecList ] ")" ) .
ConstSpecList = ConstSpec { ";" ConstSpec } [ ";" ] .
ConstSpec = IdentifierList [ [ CompleteType ] "=" ExpressionList ] .
to a new type. <font color=red>TODO: what exactly is a "new type"?</font>
</p>
-<pre class="grammar">
+<pre class="ebnf">
TypeDecl = "type" ( TypeSpec | "(" [ TypeSpecList ] ")" ) .
TypeSpecList = TypeSpec { ";" TypeSpec } [ ";" ] .
TypeSpec = identifier ( Type | "struct" | "interface" ) .
gives it a type and optionally an initial value.
The type must be complete (§Types).
</p>
-<pre class="grammar">
+<pre class="ebnf">
VarDecl = "var" ( VarSpec | "(" [ VarSpecList ] ")" ) .
VarSpecList = VarSpec { ";" VarSpec } [ ";" ] .
VarSpec = IdentifierList ( CompleteType [ "=" ExpressionList ] | "=" ExpressionList ) .
A <i>short variable declaration</i> uses the syntax
-<pre class="grammar">
+<pre class="ebnf">
SimpleVarDecl = IdentifierList ":=" ExpressionList .
</pre>
A function declaration binds an identifier to a function (§Function types).
</p>
-<pre class="grammar">
+<pre class="ebnf">
FunctionDecl = "func" identifier Signature [ Block ] .
</pre>
A method declaration binds an identifier to a method,
which is a function with a <i>receiver</i>.
</p>
-<pre class="grammar">
+<pre class="ebnf">
MethodDecl = "func" Receiver identifier Signature [ Block ] .
Receiver = "(" [ identifier ] [ "*" ] TypeName ")" .
</pre>
Operands denote the elementary values in an expression.
-<pre class="grammar">
+<pre class="ebnf">
Operand = Literal | QualifiedIdent | "(" Expression ")" .
Literal = BasicLit | CompositeLit | FunctionLit .
BasicLit = int_lit | float_lit | char_lit | StringLit .
A qualified identifier is an identifier qualified by a package name prefix.
</p>
-<pre class="grammar">
+<pre class="ebnf">
QualifiedIdent = [ ( LocalPackageName | PackageName ) "." ] identifier .
LocalPackageName = identifier .
PackageName = identifier .
a single expression or a key-value pair.
</p>
-<pre class="grammar">
+<pre class="ebnf">
CompositeLit = LiteralType "{" [ ElementList ] "}" .
LiteralType = StructType | ArrayType | "[" "..." "]" ElementType |
SliceType | MapType | TypeName .
It consists of a specification of the function type and a function body.
</p>
-<pre class="grammar">
+<pre class="ebnf">
FunctionLit = FunctionType Block .
Block = "{" StatementList "}" .
</pre>
<h3>Primary expressions</h3>
-<pre class="grammar">
+<pre class="ebnf">
PrimaryExpr =
Operand |
PrimaryExpr Selector |
Operators combine operands into expressions.
</p>
-<pre class="grammar">
+<pre class="ebnf">
Expression = UnaryExpr | Expression binary_op UnaryExpr .
UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
Statements control execution.
</p>
-<pre class="grammar">
+<pre class="ebnf">
Statement =
Declaration | EmptyStmt | LabeledStmt |
SimpleStmt | GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
SimpleStmt = ExpressionStmt | IncDecStmt | Assignment | SimpleVarDecl .
StatementList = Statement { Separator Statement } .
-Separator = [ ";" ]
+Separator = [ ";" ] .
</pre>
<p>
The empty statement does nothing.
</p>
-<pre class="grammar">
+<pre class="ebnf">
EmptyStmt = .
</pre>
<code>break</code> or <code>continue</code> statement.
</p>
-<pre class="grammar">
+<pre class="ebnf">
LabeledStmt = Label ":" Statement .
Label = identifier .
</pre>
</p>
-<pre class="grammar">
+<pre class="ebnf">
ExpressionStmt = Expression .
</pre>
must be a variable, pointer indirection, field selector or index expression.
</p>
-<pre class="grammar">
+<pre class="ebnf">
IncDecStmt = Expression ( "++" | "--" ) .
</pre>
<h3>Assignments</h3>
-<pre class="grammar">
+<pre class="ebnf">
Assignment = ExpressionList assign_op ExpressionList .
assign_op = [ add_op | mul_op ] "=" .
is equivalent to <code>true</code>.
</p>
-<pre class="grammar">
+<pre class="ebnf">
IfStmt = "if" [ [ SimpleStmt ] ";" ] [ Expression ] Block [ "else" Statement ] .
</pre>
to execute.
</p>
-<pre class="grammar">
+<pre class="ebnf">
SwitchStmt = ExprSwitchStmt | TypeSwitchStmt .
</pre>
the expression <code>true</code>.
</p>
-<pre class="grammar">
+<pre class="ebnf">
ExprSwitchStmt = "switch" [ [ SimpleStmt ] ";" ] [ Expression ] "{" { ExprCaseClause } "}" .
ExprCaseClause = ExprSwitchCase ":" [ StatementList ] .
ExprSwitchCase = "case" ExpressionList | "default" .
in the type assertion.
</p>
-<pre class="grammar">
+<pre class="ebnf">
TypeSwitchStmt = "switch" [ [ SimpleStmt ] ";" ] TypeSwitchGuard "{" { TypeCaseClause } "}" .
TypeSwitchGuard = identifier ":=" Expression "." "(" "type" ")" .
TypeCaseClause = TypeSwitchCase ":" [ StatementList ] .
controlled by a condition, a "for" clause, or a "range" clause.
</p>
-<pre class="grammar">
+<pre class="ebnf">
ForStmt = "for" [ Condition | ForClause | RangeClause ] Block .
Condition = Expression .
</pre>
(§Declarations and scope rules).
</p>
-<pre class="grammar">
+<pre class="ebnf">
ForClause = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] .
InitStmt = SimpleStmt .
PostStmt = SimpleStmt .
of iteration variables - and then executes the block.
</p>
-<pre class="grammar">
+<pre class="ebnf">
RangeClause = ExpressionList ( "=" | ":=" ) "range" Expression .
</pre>
within the same address space.
</p>
-<pre class="grammar">
+<pre class="ebnf">
GoStmt = "go" Expression .
</pre>
cases all referring to communication operations.
</p>
-<pre class="grammar">
+<pre class="ebnf">
SelectStmt = "select" "{" { CommClause } "}" .
CommClause = CommCase ":" StatementList .
CommCase = "case" ( SendExpr | RecvExpr) | "default" .
and optionally provides a result value or values to the caller.
</p>
-<pre class="grammar">
+<pre class="ebnf">
ReturnStmt = "return" [ ExpressionList ] .
</pre>
"for", "switch" or "select" statement.
</p>
-<pre class="grammar">
+<pre class="ebnf">
BreakStmt = "break" [ Label ].
</pre>
innermost "for" loop at the post statement (§For statements).
</p>
-<pre class="grammar">
+<pre class="ebnf">
ContinueStmt = "continue" [ Label ].
</pre>
A "goto" statement transfers control to the statement with the corresponding label.
</p>
-<pre class="grammar">
+<pre class="ebnf">
GotoStmt = "goto" Label .
</pre>
expression "switch" statement.
</p>
-<pre class="grammar">
+<pre class="ebnf">
FallthroughStmt = "fallthrough" .
</pre>
the surrounding function returns.
</p>
-<pre class="grammar">
+<pre class="ebnf">
DeferStmt = "defer" Expression .
</pre>
rules).
</p>
-<pre class="grammar">
+<pre class="ebnf">
SourceFile = PackageClause { ImportDecl [ ";" ] } { Declaration [ ";" ] } .
</pre>
to which the file belongs.
</p>
-<pre class="grammar">
+<pre class="ebnf">
PackageClause = "package" PackageName .
</pre>
installed packages.
</p>
-<pre class="grammar">
+<pre class="ebnf">
ImportDecl = "import" ( ImportSpec | "(" [ ImportSpecList ] ")" ) .
ImportSpecList = ImportSpec { ";" ImportSpec } [ ";" ] .
ImportSpec = [ "." | PackageName ] PackageFileName .