Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
bbced024
Commit
bbced024
authored
Mar 06, 2008
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- updated docs
SVN=111669
parent
28590a0a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
138 additions
and
74 deletions
+138
-74
doc/go_lang.txt
doc/go_lang.txt
+138
-74
No files found.
doc/go_lang.txt
View file @
bbced024
The Go Programming Language
The Go Programming Language
(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
language.
language.
...
@@ -490,21 +491,19 @@ TypeName = QualifiedIdent.
...
@@ -490,21 +491,19 @@ 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
regular
and dynamic arrays]
static, open
and dynamic arrays]
An array is a structured type consisting of a number of elements which
An array is a structured type consisting of a number of elements which
are all of the same type, called the element type. The number of
are all of the same type, called the element type. The number of
elements of an array is called its length. The elements of an array
elements of an array is called its length. The elements of an array
are designated by indices which are integers between 0 and the length
are designated by indices which are integers between 0 and the length - 1.
- 1.
An array type specifies arrays with a given element type and
An array type specifies arrays with a given element type and
an optional array length. The array length must be a (compile-time)
an optional array length. If the length is present, it is part of the type.
constant expression, if present. Arrays without length specification
Arrays without a length specification are called open arrays.
are called dynamic arrays. A dynamic array must not contain other dynamic
Any array may be assigned to an open array variable with the
arrays, and dynamic arrays can only be used as parameter types or in a
same element type. Typically, open arrays are used as
pointer type (for instance, a struct may not contain a dynamic array
formal parameters for functions.
field, but only a pointer to an open array).
ArrayType = { '[' ArrayLength ']' } ElementType.
ArrayType = { '[' ArrayLength ']' } ElementType.
ArrayLength = Expression.
ArrayLength = Expression.
...
@@ -515,6 +514,11 @@ ElementType = Type.
...
@@ -515,6 +514,11 @@ ElementType = Type.
[64] struct { x, y: int32; }
[64] struct { x, y: int32; }
[1000][1000] float64
[1000][1000] float64
The length of an array can be discovered at run time using the
built-in special function len():
len(a)
Array literals
Array literals
...
@@ -920,61 +924,38 @@ export directive.
...
@@ -920,61 +924,38 @@ export directive.
ExportDecl = 'export' ExportIdentifier { ',' ExportIdentifier } .
ExportDecl = 'export' ExportIdentifier { ',' ExportIdentifier } .
ExportIdentifier = QualifiedIdent .
ExportIdentifier = QualifiedIdent .
export sin, cos
export sin, cos
export Math.abs
export Math.abs
[ TODO complete this section ]
[ TODO complete this section ]
Expressions
Expressions
Expression syntax is based on that of C.
Expression syntax is based on that of C but with fewer precedence levels.
Operand = Literal | Designator | UnaryExpr | '(' Expression ')' | Call.
UnaryExpr = unary_op Expression
unary_op = '!' | '-' | '^' | '&' | '<' .
Designator = QualifiedIdent { Selector }.
Selector = '.' identifier | '[' Expression [ ':' Expression ] ']'.
Call = Operand '(' ExpressionList ')'.
2
Expression = BinaryExpr | UnaryExpr | PrimaryExpr .
a[i]
BinaryExpr = Expression binary_op Expression .
"hello"
UnaryExpr = unary_op Expression .
f("abc")
p.q.r
a.m(zot, bar)
<chan_ptr
~v
m["key"]
(x+y)
For selectors and function invocations, one level of pointer dereferencing
is provided automatically. Thus, the expressions
(*a)[i]
(*m)["key"]
(*s).field
(*f)()
can be simplified to
a[i]
PrimaryExpr =
m["key"]
identifier | Literal | '(' Expression ')' | 'iota' |
s.field
Call | Conversion |
f()
Expression '[' Expression [ ':' Expression ] ']' | Expression '.' identifier .
Call = Expression '(' [ ExpressionList ] ')' .
Conversion = TypeName '(' [ ExpressionList ] ')' .
Expression = Conjunction { '||' Conjunction }.
binary_op = log_op | rel_op | add_op | mul_op .
Conjunction = Comparison { '&&' Comparison }.
log_op = '||' | '&&' .
Comparison = SimpleExpr [ relation SimpleExpr ].
rel_op = '==' | '!=' | '<' | '<=' | '>' | '>='.
SimpleExpr = Term { add_op Term }.
Term = Operand { mul_op Operand }.
relation = '==' | '!=' | '<' | '<=' | '>' | '>='.
add_op = '+' | '-' | '|' | '^'.
add_op = '+' | '-' | '|' | '^'.
mul_op = '*' | '/' | '%' | '<<' | '>>' | '&'.
mul_op = '*' | '/' | '%' | '<<' | '>>' | '&'.
The corresponding precedence hierarchy is as follows:
unary_op = '+' | '-' | '!' | '^' | '<' | '>' | '*' | '&' .
Field selection ('.') binds tightest, followed by indexing ('[]') and then calls and conversions.
The remaining precedence levels are as follows (in increasing precedence order):
Precedence Operator
Precedence Operator
1 ||
1 ||
...
@@ -982,13 +963,8 @@ Precedence Operator
...
@@ -982,13 +963,8 @@ Precedence Operator
3 == != < <= > >=
3 == != < <= > >=
4 + - | ^
4 + - | ^
5 * / % << >> &
5 * / % << >> &
6 + - ! ^ < > * & (unary)
23 + 3*x[i]
x <= f()
a >> ~b
f() || g()
x == y || <chan_ptr > 0
For integer values, / and % satisfy the following relationship:
For integer values, / and % satisfy the following relationship:
(a / b) * b + a % b == a
(a / b) * b + a % b == a
...
@@ -997,15 +973,67 @@ and
...
@@ -997,15 +973,67 @@ and
(a / b) is "truncated towards zero".
(a / b) is "truncated towards zero".
There are no implicit type conversions except for
constants and literals. In particular, unsigned and signed integers
cannot be mixed in an expression without explicit conversion.
The shift operators implement arithmetic shifts for signed integers,
The shift operators implement arithmetic shifts for signed integers,
and logical shifts for unsigned integers. The property of negative
and logical shifts for unsigned integers. The property of negative
shift counts are undefined.
shift counts are undefined. Unary '^' corresponds to C '~' (bitwise
complement).
There are no implicit type conversions except for
There is no '->' operator. Given a pointer p to a struct, one writes
constants and literals. In particular, unsigned and signed integers
p.f to access field f of the struct. Similarly. given an array or map pointer, one
cannot be mixed in an expression w/o explicit casting.
writes p[i], given a function pointer, one writes p() to call the function.
Other operators behave as in C.
The 'iota' keyword is discussed in the next section.
Primary expressions
x
2
(s + ".txt")
f(3.1415, true)
Point(1, 2)
m["foo"]
s[i : j + 1]
obj.color
Math.sin
f.p[i].x()
General expressions
+x
23 + 3*x[i]
x <= f()
^a >> b
f() || g()
x == y + 1 && <chan_ptr > 0
The constant generator 'iota'
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'
or 'var' introduces a new declaration. For instance, 'iota' can be used to construct
a set of related constants:
const (
enum0 = iota; // sets enum0 to 0, etc.
enum1 = iota;
enum2 = iota
)
Unary '^' corresponds to C '~' (bitwise complement).
const (
a = 1 << iota; // sets a to 1 (iota has been reset)
b = 1 << iota; // sets b to 2
c = 1 << iota; // sets c to 4
)
const x = iota; // sets x to 0
const y = iota; // sets y to 0
Statements
Statements
...
@@ -1014,14 +1042,16 @@ Statements control execution.
...
@@ -1014,14 +1042,16 @@ Statements control execution.
Statement =
Statement =
Declaration |
Declaration |
ExpressionStat | IncDecStat | CompoundStat |
SimpleStat | CompoundStat |
Assignment |
GoStat |
GoStat |
ReturnStat |
ReturnStat |
IfStat | SwitchStat |
IfStat | SwitchStat |
ForStat | RangeStat |
ForStat | RangeStat |
BreakStat | ContinueStat | GotoStat | LabelStat .
BreakStat | ContinueStat | GotoStat | LabelStat .
SimpleStat =
ExpressionStat | IncDecStat | Assignment | SimpleVarDecl .
Expression statements
Expression statements
...
@@ -1055,17 +1085,22 @@ from the declaration to the end of the compound statement.
...
@@ -1055,17 +1085,22 @@ from the declaration to the end of the compound statement.
Assignments
Assignments
Assignment = SingleAssignment | TupleAssignment | Send .
Assignment = SingleAssignment | TupleAssignment | Send .
SimpleAssignment = Designator
'='
Expression .
SimpleAssignment = Designator
assign_op
Expression .
TupleAssignment = DesignatorList
'='
ExpressionList .
TupleAssignment = DesignatorList
assign_op
ExpressionList .
Send = '>' Expression = Expression .
Send = '>' Expression = Expression .
assign_op = [ add_op | mul_op ] '=' .
The designator must be an l-value such as a variable, pointer indirection,
The designator must be an l-value such as a variable, pointer indirection,
or an array indexing.
or an array indexing.
x = 1
x = 1
*p = f()
*p = f()
a[i] = 23
a[i] = 23
As in C, arithmetic binary operators can be combined with assignments:
j <<= 2
A tuple assignment assigns the individual elements of a multi-valued operation,
A tuple assignment assigns the individual elements of a multi-valued operation,
such function evaluation or some channel and map operations, into individual
such function evaluation or some channel and map operations, into individual
...
@@ -1243,7 +1278,7 @@ InitStat = SimpleStat .
...
@@ -1243,7 +1278,7 @@ InitStat = SimpleStat .
Condition = Expression .
Condition = Expression .
PostStat = SimpleStat .
PostStat = SimpleStat .
A SimpleStat is a simple statement such as an assign
em
nt, a SimpleVarDecl,
A SimpleStat is a simple statement such as an assign
me
nt, a SimpleVarDecl,
or an increment or decrement statement. Therefore one may declare a loop
or an increment or decrement statement. Therefore one may declare a loop
variable in the init statement.
variable in the init statement.
...
@@ -1350,14 +1385,45 @@ PackageClause = 'package' PackageName .
...
@@ -1350,14 +1385,45 @@ PackageClause = 'package' PackageName .
Import declarations
Import declarations
A program can
access exported items from another package using
A program can
gain access to exported items from another package
an import declaration:
through
an import declaration:
ImportDecl = 'import' [ PackageName ] PackageFileName .
ImportDecl = 'import' [
'.' |
PackageName ] PackageFileName .
PackageFileName = string_lit .
PackageFileName = string_lit .
An import statement makes the exported contents of the named
package file accessible in this package.
[ TODO complete this section ]
In the following discussion, assume we have a package in the
file "/lib/math", called package Math, which exports functions sin
and cos.
In the general form, with an explicit package name, the import
statement declares that package name as an identifier whose
contents are the exported elements of the imported package.
For instance, after
import M "/lib/math"
the contents of the package /lib/math can be accessed by
M.cos, M.sin, etc.
In its simplest form, with no package name, the import statement
implicitly uses the imported package name itself as the local
package name. After
import "/lib/math"
the contents are accessible by Math.sin, Math.cos.
Finally, if instead of a package name the import statement uses
an explicit period, the contents of the imported package are added
to the current package. After
import . "/lib/math"
the contents are accessible by sin and cos. In this instance, it is
an error if the import introduces name conflicts.
Program
Program
...
@@ -1372,5 +1438,3 @@ Program = PackageClause { ImportDecl } { Declaration } .
...
@@ -1372,5 +1438,3 @@ Program = PackageClause { ImportDecl } { Declaration } .
TODO: type switch?
TODO: type switch?
TODO: select
TODO: select
TODO: words about slices
TODO: words about slices
TODO: words about channel ops, tuple returns
TODO: words about map ops, tuple returns
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment