add HTML formatting; use

/home/sanjay/bin/makehtml --mode=document go_lang.txt
to generate the html output.

SVN=111681
This commit is contained in:
Rob Pike 2008-03-06 19:40:52 -08:00
parent bbced02490
commit 250767174b

View File

@ -1,4 +1,5 @@
The Go Programming Language The Go Programming Language
----
(March 7, 2008) (March 7, 2008)
This document is an informal specification/proposal for a new systems programming This document is an informal specification/proposal for a new systems programming
@ -6,6 +7,7 @@ language.
Guiding principles Guiding principles
----
Go is a new systems programming language intended as an alternative to C++ at Go is a new systems programming language intended as an alternative to C++ at
Google. Its main purpose is to provide a productive and efficient programming Google. Its main purpose is to provide a productive and efficient programming
@ -28,11 +30,12 @@ written in itself.
Modularity, identifiers and scopes Modularity, identifiers and scopes
----
A Go program consists of one or more `packages' compiled separately, though A Go program consists of one or more `packages' compiled separately, though
not independently. A single package may make not independently. A single package may make
individual identifiers visible to other files by marking them as individual identifiers visible to other files by marking them as
exported; there is no "header file". exported; there is no ``header file''.
A package collects types, constants, functions, and so on into a named A package collects types, constants, functions, and so on into a named
entity that may be exported to enable its constituents be used in entity that may be exported to enable its constituents be used in
@ -45,6 +48,7 @@ Scoping is essentially the same as in C.
Program structure Program structure
----
A compilation unit (usually a single source file) A compilation unit (usually a single source file)
consists of a package specifier followed by import consists of a package specifier followed by import
@ -63,6 +67,7 @@ still under development.
Typing, polymorphism, and object-orientation Typing, polymorphism, and object-orientation
----
Go programs are strongly typed. Certain expressions, in particular map Go programs are strongly typed. Certain expressions, in particular map
and channel accesses, can also be polymorphic. The language provides and channel accesses, can also be polymorphic. The language provides
@ -80,7 +85,7 @@ An interface is implemented by associating methods with
structures. If a structure implements all methods of an interface, it structures. If a structure implements all methods of an interface, it
implements that interface and thus can be used where that interface is implements that interface and thus can be used where that interface is
required. Unless used through a variable of interface type, methods required. Unless used through a variable of interface type, methods
can always be statically bound (they are not "virtual"), and incur no can always be statically bound (they are not ``virtual''), and incur no
runtime overhead compared to an ordinary function. runtime overhead compared to an ordinary function.
Go has no explicit notion of classes, sub-classes, or inheritance. Go has no explicit notion of classes, sub-classes, or inheritance.
@ -93,6 +98,7 @@ use of abstract data types operating on interface types.
Pointers and garbage collection Pointers and garbage collection
----
Variables may be allocated automatically (when entering the scope of Variables may be allocated automatically (when entering the scope of
the variable) or explicitly on the heap. Pointers are used to refer the variable) or explicitly on the heap. Pointers are used to refer
@ -103,6 +109,7 @@ they are no longer accessible. There is no pointer arithmetic in Go.
Functions Functions
----
Functions contain declarations and statements. They may be Functions contain declarations and statements. They may be
recursive. Functions may be anonymous and appear as recursive. Functions may be anonymous and appear as
@ -110,6 +117,7 @@ literals in expressions.
Multithreading and channels Multithreading and channels
----
Go supports multithreaded programming directly. A function may Go supports multithreaded programming directly. A function may
be invoked as a parallel thread of execution. Communication and be invoked as a parallel thread of execution. Communication and
@ -118,6 +126,7 @@ language support.
Values and references Values and references
----
All objects have value semantics, but its contents may be accessed All objects have value semantics, but its contents may be accessed
through different pointers referring to the same object. through different pointers referring to the same object.
@ -131,6 +140,7 @@ byte strings.
Syntax Syntax
----
The syntax of statements and expressions in Go borrows from the C tradition; The syntax of statements and expressions in Go borrows from the C tradition;
declarations are loosely derived from the Pascal tradition to allow more declarations are loosely derived from the Pascal tradition to allow more
@ -138,7 +148,7 @@ comprehensible composability of types.
Here is a complete example Go program that implements a concurrent prime sieve: Here is a complete example Go program that implements a concurrent prime sieve:
============================
package Main package Main
// Send the sequence 2, 3, 4, ... to channel 'ch'. // Send the sequence 2, 3, 4, ... to channel 'ch'.
@ -175,19 +185,19 @@ func Sieve() {
func Main() { func Main() {
Sieve(); Sieve();
} }
============================
Notation Notation
----
The syntax is specified using Extended The syntax is specified using Extended
Backus-Naur Form (EBNF). In particular: Backus-Naur Form (EBNF). In particular:
'' encloses lexical symbols - '' encloses lexical symbols
| separates alternatives - | separates alternatives
() used for grouping - () used for grouping
[] specifies option (0 or 1 times) - [] specifies option (0 or 1 times)
{} specifies repetition (0 to n times) - {} specifies repetition (0 to n times)
A production may be referenced from various places in this document A production may be referenced from various places in this document
but is usually defined close to its first use. Code examples are indented. but is usually defined close to its first use. Code examples are indented.
@ -198,6 +208,7 @@ productions are in CamelCase.
Common productions Common productions
----
IdentifierList = identifier { ',' identifier }. IdentifierList = identifier { ',' identifier }.
ExpressionList = Expression { ',' Expression }. ExpressionList = Expression { ',' Expression }.
@ -207,6 +218,7 @@ PackageName = identifier.
Source code representation Source code representation
----
Source code is Unicode text encoded in UTF-8. Source code is Unicode text encoded in UTF-8.
@ -222,6 +234,7 @@ implementation, Go treats these as distinct characters.
Characters Characters
----
In the grammar we use the notation In the grammar we use the notation
@ -231,6 +244,7 @@ to refer to an arbitrary Unicode code point encoded in UTF-8.
Digits and Letters Digits and Letters
----
octal_digit = { '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' } . octal_digit = { '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' } .
decimal_digit = { '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' } . decimal_digit = { '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' } .
@ -243,6 +257,7 @@ Unicode identifiers.
Identifiers Identifiers
----
An identifier is a name for a program entity such as a variable, a An identifier is a name for a program entity such as a variable, a
type, a function, etc. An identifier must not be a reserved word. type, a function, etc. An identifier must not be a reserved word.
@ -255,6 +270,7 @@ identifier = letter { letter | decimal_digit } .
Types Types
----
A type specifies the set of values which variables of that type may A type specifies the set of values which variables of that type may
assume, and the operators that are applicable. assume, and the operators that are applicable.
@ -263,6 +279,7 @@ There are basic types and compound types constructed from them.
Basic types Basic types
----
Go defines a number of basic types which are referred to by their Go defines a number of basic types which are referred to by their
predeclared type names. There are signed and unsigned integer predeclared type names. There are signed and unsigned integer
@ -288,17 +305,18 @@ and floating point types:
Additionally, Go declares 4 basic types, uint, int, float, and double, Additionally, Go declares 4 basic types, uint, int, float, and double,
which are platform-specific. The bit width of these types corresponds to which are platform-specific. The bit width of these types corresponds to
the "natural bit width" for the respective types for the given the ``natural bit width'' for the respective types for the given
platform. For instance, int is usally the same as int32 on a 32-bit platform. For instance, int is usally the same as int32 on a 32-bit
architecture, or int64 on a 64-bit architecture. These types are by architecture, or int64 on a 64-bit architecture. These types are by
definition platform-specific and should be used with the appropriate definition platform-specific and should be used with the appropriate
caution. caution.
Two reserved words, 'true' and 'false', represent the Two reserved words, "true" and "false", represent the
corresponding boolean constant values. corresponding boolean constant values.
Numeric literals Numeric literals
----
Integer literals take the usual C form, except for the absence of the Integer literals take the usual C form, except for the absence of the
'U', 'L' etc. suffixes, and represent integer constants. (Character 'U', 'L' etc. suffixes, and represent integer constants. (Character
@ -334,6 +352,7 @@ unsigned_float_lit = "the usual decimal-only floating point representation".
+3.24e-7 +3.24e-7
The string type The string type
----
The string type represents the set of string values (strings). The string type represents the set of string values (strings).
A string behaves like an array of bytes, with the following properties: A string behaves like an array of bytes, with the following properties:
@ -356,8 +375,8 @@ A string behaves like an array of bytes, with the following properties:
Character and string literals Character and string literals
----
[ R: FIX ALL UNICODE INSIDE ]
Character and string literals are almost the same as in C, but with Character and string literals are almost the same as in C, but with
UTF-8 required. This section is precise but can be skipped on first UTF-8 required. This section is precise but can be skipped on first
reading. reading.
@ -368,6 +387,8 @@ Character and string literals are similar to C except:
- Strings are UTF-8 and represent Unicode - Strings are UTF-8 and represent Unicode
- `` strings exist; they do not interpret backslashes - `` strings exist; they do not interpret backslashes
The rules are:
char_lit = '\'' ( unicode_value | byte_value ) '\'' . char_lit = '\'' ( unicode_value | byte_value ) '\'' .
unicode_value = utf8_char | little_u_value | big_u_value | escaped_char . unicode_value = utf8_char | little_u_value | big_u_value | escaped_char .
byte_value = octal_byte_value | hex_byte_value . byte_value = octal_byte_value | hex_byte_value .
@ -380,14 +401,14 @@ escaped_char = '\' ( 'a' | 'b' | 'f' | 'n' | 'r' | 't' | 'v' ) .
A UnicodeValue takes one of four forms: A UnicodeValue takes one of four forms:
1. The UTF-8 encoding of a Unicode code point. Since Go source * The UTF-8 encoding of a Unicode code point. Since Go source
text is in UTF-8, this is the obvious translation from input text is in UTF-8, this is the obvious translation from input
text into Unicode characters. text into Unicode characters.
2. The usual list of C backslash escapes: \n \t etc. 3. A * The usual list of C backslash escapes: \n \t etc.
`little u' value, such as \u12AB. This represents the Unicode * A `little u' value, such as \u12AB. This represents the Unicode
code point with the corresponding hexadecimal value. It always code point with the corresponding hexadecimal value. It always
has exactly 4 hexadecimal digits. has exactly 4 hexadecimal digits.
4. A `big U' value, such as '\U00101234'. This represents the * A `big U' value, such as '\U00101234'. This represents the
Unicode code point with the corresponding hexadecimal value. Unicode code point with the corresponding hexadecimal value.
It always has exactly 8 hexadecimal digits. It always has exactly 8 hexadecimal digits.
@ -404,11 +425,11 @@ It is erroneous for an OctalByteValue to represent a value larger than 255.
A character literal is a form of unsigned integer constant. Its value A character literal is a form of unsigned integer constant. Its value
is that of the Unicode code point represented by the text between the is that of the Unicode code point represented by the text between the
quotes. quotes. [Note: the Unicode doesn't look right in the browser.]
'a' 'a'
'ä' // FIX 'ä'
'本' // FIX '本'
'\t' '\t'
'\0' '\0'
'\07' '\07'
@ -430,7 +451,11 @@ A string literal has type 'string'. Its value is constructed by
taking the byte values formed by the successive elements of the taking the byte values formed by the successive elements of the
literal. For ByteValues, these are the literal bytes; for literal. For ByteValues, these are the literal bytes; for
UnicodeValues, these are the bytes of the UTF-8 encoding of the UnicodeValues, these are the bytes of the UTF-8 encoding of the
corresponding Unicode code points. Note that "\u00FF" and "\xFF" are corresponding Unicode code points. Note that
"\u00FF"
and
"\xFF"
are
different strings: the first contains the two-byte UTF-8 expansion of different strings: the first contains the two-byte UTF-8 expansion of
the value 255, while the second contains a single byte of value 255. the value 255, while the second contains a single byte of value 255.
The same rules apply to raw string literals, except the contents are The same rules apply to raw string literals, except the contents are
@ -465,6 +490,7 @@ literal.
More about types More about types
----
The static type of a variable is the type defined by the variable's The static type of a variable is the type defined by the variable's
declaration. At run-time, some variables, in particular those of declaration. At run-time, some variables, in particular those of
@ -489,6 +515,7 @@ TypeName = QualifiedIdent.
Array types Array types
----
[TODO: this section needs work regarding the precise difference between [TODO: this section needs work regarding the precise difference between
static, open and dynamic arrays] static, open and dynamic arrays]
@ -521,6 +548,7 @@ built-in special function len():
Array literals Array literals
----
Array literals represent array constants. All the contained expressions must Array literals represent array constants. All the contained expressions must
be of the same type, which is the element type of the resulting array. be of the same type, which is the element type of the resulting array.
@ -532,6 +560,7 @@ ArrayLit = '[' ExpressionList ']' .
Map types Map types
----
A map is a structured type consisting of a variable number of entries A map is a structured type consisting of a variable number of entries
called (key, value) pairs. For a given map, called (key, value) pairs. For a given map,
@ -550,6 +579,7 @@ ValueType = Type | 'any' .
Map Literals Map Literals
----
Map literals represent map constants. They comprise a list of (key, value) Map literals represent map constants. They comprise a list of (key, value)
pairs. All keys must have the same type; all values must have the same type. pairs. All keys must have the same type; all values must have the same type.
@ -564,6 +594,7 @@ KeyValue = Expression ':' Expression .
Struct types Struct types
----
Struct types are similar to C structs. Struct types are similar to C structs.
@ -586,6 +617,7 @@ FieldDecl = IdentifierList Type ';' .
Struct literals Struct literals
----
Struct literals represent struct constants. They comprise a list of Struct literals represent struct constants. They comprise a list of
expressions that represent the individual fields of a struct. The expressions that represent the individual fields of a struct. The
@ -601,6 +633,7 @@ The type name must be that of a defined struct type.
Pointer types Pointer types
----
Pointer types are similar to those in C. Pointer types are similar to those in C.
@ -615,6 +648,7 @@ There are no pointer literals.
Channel types Channel types
----
A channel provides a mechanism for two concurrently executing functions A channel provides a mechanism for two concurrently executing functions
to exchange values and synchronize execution. A channel type can be to exchange values and synchronize execution. A channel type can be
@ -639,6 +673,7 @@ There are no channel literals.
Function types Function types
----
A function type denotes the set of all functions with the same signature. A function type denotes the set of all functions with the same signature.
@ -673,6 +708,7 @@ pointer.
Function Literals Function Literals
----
Function literals represent anonymous functions. Function literals represent anonymous functions.
@ -692,6 +728,7 @@ variables, and variables declared within the function literal.
Methods Methods
----
A method is a function bound to a particular struct type T. When defined, A method is a function bound to a particular struct type T. When defined,
a method indicates the type of the struct by declaring a receiver of type a method indicates the type of the struct by declaring a receiver of type
@ -721,12 +758,14 @@ For instance, given a Point variable pt, one may call
Interface of a struct Interface of a struct
----
The interface of a struct is defined to be the unordered set of methods The interface of a struct is defined to be the unordered set of methods
associated with that struct. associated with that struct.
Interface types Interface types
----
An interface type denotes a set of methods. An interface type denotes a set of methods.
@ -774,6 +813,7 @@ There are no interface literals.
Literals Literals
----
Literal = BasicLit | CompoundLit . Literal = BasicLit | CompoundLit .
BasicLit = CharLit | StringLit | IntLit | FloatLit . BasicLit = CharLit | StringLit | IntLit | FloatLit .
@ -781,6 +821,7 @@ CompoundLit = ArrayLit | MapLit | StructLit | FunctionLit .
Declarations Declarations
----
A declaration associates a name with a language entity such as a type, A declaration associates a name with a language entity such as a type,
constant, variable, or function. constant, variable, or function.
@ -789,6 +830,7 @@ Declaration = ConstDecl | TypeDecl | VarDecl | FunctionDecl | ExportDecl .
Const declarations Const declarations
----
A constant declaration gives a name to the value of a constant expression. A constant declaration gives a name to the value of a constant expression.
@ -805,6 +847,7 @@ ConstSpecList = ConstSpec { ';' ConstSpec }.
Type declarations Type declarations
----
A type declaration introduces a name as a shorthand for a type. A type declaration introduces a name as a shorthand for a type.
In certain situations, such as conversions, it may be necessary to In certain situations, such as conversions, it may be necessary to
@ -823,6 +866,7 @@ TypeSpecList = TypeSpec { ';' TypeSpec }.
Variable declarations Variable declarations
----
A variable declaration creates a variable and gives it a type and a name. A variable declaration creates a variable and gives it a type and a name.
It may optionally give the variable an initial value; in some forms of It may optionally give the variable an initial value; in some forms of
@ -848,7 +892,7 @@ The syntax
SimpleVarDecl = identifier ':=' Expression . SimpleVarDecl = identifier ':=' Expression .
is syntactic shorthand for is shorthand for
var identifer = Expression. var identifer = Expression.
@ -861,6 +905,7 @@ declare local temporary variables.
Function and method declarations Function and method declarations
----
Functions and methods have a special declaration syntax, slightly Functions and methods have a special declaration syntax, slightly
different from the type syntax because an identifier must be present different from the type syntax because an identifier must be present
@ -904,6 +949,7 @@ Functions and methods can be forward declared by omitting the body:
Export declarations Export declarations
----
Global identifiers may be exported, thus making the Global identifiers may be exported, thus making the
exported identifer visible outside the package. Another package may exported identifer visible outside the package. Another package may
@ -931,6 +977,7 @@ ExportIdentifier = QualifiedIdent .
Expressions Expressions
----
Expression syntax is based on that of C but with fewer precedence levels. Expression syntax is based on that of C but with fewer precedence levels.
@ -1014,6 +1061,7 @@ General expressions
The constant generator 'iota' The constant generator 'iota'
----
Within a declaration, each appearance of the keyword 'iota' represents a successive Within a declaration, each appearance of the keyword 'iota' represents a successive
element of an integer sequence. It is reset to zero whenever the keyword 'const', 'type' element of an integer sequence. It is reset to zero whenever the keyword 'const', 'type'
@ -1037,6 +1085,7 @@ a set of related constants:
Statements Statements
----
Statements control execution. Statements control execution.
@ -1054,6 +1103,7 @@ SimpleStat =
Expression statements Expression statements
----
ExpressionStat = Expression . ExpressionStat = Expression .
@ -1061,6 +1111,7 @@ ExpressionStat = Expression .
IncDec statements IncDec statements
----
IncDecStat = Expression ( '++' | '--' ) . IncDecStat = Expression ( '++' | '--' ) .
@ -1070,6 +1121,7 @@ Note that ++ and -- are not operators for expressions.
Compound statements Compound statements
----
CompoundStat = '{' { Statement } '}' . CompoundStat = '{' { Statement } '}' .
@ -1083,6 +1135,7 @@ from the declaration to the end of the compound statement.
Assignments Assignments
----
Assignment = SingleAssignment | TupleAssignment | Send . Assignment = SingleAssignment | TupleAssignment | Send .
SimpleAssignment = Designator assign_op Expression . SimpleAssignment = Designator assign_op Expression .
@ -1141,6 +1194,7 @@ In assignments, the type of the expression must match the type of the designator
Go statements Go statements
----
A go statement starts the execution of a function as an independent A go statement starts the execution of a function as an independent
concurrent thread of control within the same address space. Unlike concurrent thread of control within the same address space. Unlike
@ -1149,17 +1203,20 @@ function to complete.
GoStat = 'go' Call . GoStat = 'go' Call .
go Server() go Server()
go func(ch chan> bool) { for ;; { sleep(10); >ch = true; }} (c) go func(ch chan> bool) { for ;; { sleep(10); >ch = true; }} (c)
Return statements Return statements
----
A return statement terminates execution of the containing function A return statement terminates execution of the containing function
and optionally provides a result value or values to the caller. and optionally provides a result value or values to the caller.
ReturnStat = 'return' [ ExpressionList ] . ReturnStat = 'return' [ ExpressionList ] .
There are two ways to return values from a function. The first is to There are two ways to return values from a function. The first is to
explicitly list the return value or values in the return statement: explicitly list the return value or values in the return statement:
@ -1190,6 +1247,7 @@ first form of return statement is used:
If statements If statements
----
If statements have the traditional form except that the If statements have the traditional form except that the
condition need not be parenthesized and the "then" statement condition need not be parenthesized and the "then" statement
@ -1215,6 +1273,7 @@ the variable is initialized once before the statement is entered.
Switch statements Switch statements
----
Switches provide multi-way execution. Switches provide multi-way execution.
@ -1268,6 +1327,7 @@ If the expression is omitted, it is equivalent to 'true'.
For statements For statements
----
For statements are a combination of the 'for' and 'while' loops of C. For statements are a combination of the 'for' and 'while' loops of C.
@ -1301,6 +1361,7 @@ If the condition is absent, it is equivalent to 'true'.
Range statements Range statements
----
Range statements are a special control structure for iterating over Range statements are a special control structure for iterating over
the contents of arrays and maps. the contents of arrays and maps.
@ -1327,6 +1388,7 @@ array elements (the values).
Break statements Break statements
----
Within a 'for' or 'switch' statement, a 'break' statement terminates execution of Within a 'for' or 'switch' statement, a 'break' statement terminates execution of
the innermost 'for' or 'switch' statement. the innermost 'for' or 'switch' statement.
@ -1344,6 +1406,7 @@ statement, and that is the one whose execution terminates.
Continue statements Continue statements
----
Within a 'for' loop a continue statement begins the next iteration of the Within a 'for' loop a continue statement begins the next iteration of the
loop at the post statement. loop at the post statement.
@ -1354,6 +1417,7 @@ The optional identifier is analogous to that of a 'break' statement.
Goto statements Goto statements
----
A goto statement transfers control to the corresponding label statement. A goto statement transfers control to the corresponding label statement.
@ -1363,6 +1427,7 @@ GotoStat = 'goto' identifier .
Label statement Label statement
----
A label statement serves as the target of a 'goto', 'break' or 'continue' statement. A label statement serves as the target of a 'goto', 'break' or 'continue' statement.
@ -1374,6 +1439,7 @@ There are various restrictions [TBD] as to where a label statement can be used.
Packages Packages
----
Every source file identifies the package to which it belongs. Every source file identifies the package to which it belongs.
The file must begin with a package clause. The file must begin with a package clause.
@ -1384,6 +1450,7 @@ PackageClause = 'package' PackageName .
Import declarations Import declarations
----
A program can gain access to exported items from another package A program can gain access to exported items from another package
through an import declaration: through an import declaration:
@ -1427,14 +1494,16 @@ an error if the import introduces name conflicts.
Program Program
----
A program is package clause, optionally followed by import declarations, A program is package clause, optionally followed by import declarations,
followed by a series of declarations. followed by a series of declarations.
Program = PackageClause { ImportDecl } { Declaration } . Program = PackageClause { ImportDecl } { Declaration } .
------------------------------------------------------------------------- TODO
----
TODO: type switch? - TODO: type switch?
TODO: select - TODO: select
TODO: words about slices - TODO: words about slices