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
8c1408dd
Commit
8c1408dd
authored
Mar 27, 2008
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add select statement
SVN=114147
parent
e118551c
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
2 deletions
+60
-2
doc/go_lang.txt
doc/go_lang.txt
+60
-2
No files found.
doc/go_lang.txt
View file @
8c1408dd
...
...
@@ -1163,7 +1163,7 @@ Statements control execution.
[ LabelDecl ] ( StructuredStat | UnstructuredStat ) .
StructuredStat =
Block | IfStat | SwitchStat | ForStat | RangeStat .
Block | IfStat | SwitchStat |
SelectStat |
ForStat | RangeStat .
UnstructuredStat =
Declaration | SimpleVarDecl |
...
...
@@ -1399,6 +1399,65 @@ If the expression is omitted, it is equivalent to "true".
}
Select statements
----
A select statement chooses which of a set of possible communications
will proceed. It looks similar to a switch statement but with the
cases all referring to communication operations.
SelectStat = "select" "{" { CommClause } "}" .
CommClause = CommCase { Statement } .
CommCase = ( "default" | ( "case" ( SendCase | RecvCase) ) ) ":" .
SendCase = Send .
RecvCase = [ identifier '=' ] RecvExpression .
RecvExpression = '<' Expression .
The select statement evaluates all the channel (pointers) involved.
If any of the channels can proceed, the corresponding communication
and statements are evaluated. Otherwise, if there is a default case,
that executes; if not, the statement blocks until one of the
communications can complete. A channel pointer may be nil, which is
equivalent to that case not being present in the select statement.
If the channel sends or receives "any" or an interface type, its
communication can proceed only if the type of the communication
clause matches that of the dynamic value to be exchanged.
If multiple cases can proceed, a uniform fair choice is made regarding
which single communication will execute.
var c, c1, c2 *chan int;
select {
case i1 = <c1:
printf("received %d from c1\n", i1);
case >c2 = i2:
printf("sent %d to c2\n", i2);
default:
printf("no communication\n");
}
for { // send random sequence of bits to c
select {
case >c = 0: // note: no statement, no fallthrough, no folding of cases
case >c = 1:
}
}
var ca *chan any;
var i int;
var f float;
select {
case i = <ca:
printf("received int %d from ca\n", i);
case f = <ca:
printf("received float %f from ca\n", f);
}
TODO: do we allow case i := <c: ?
TODO: need to precise about all the details but this is not the right doc for that
For statements
----
...
...
@@ -1591,7 +1650,6 @@ TODO
----
- TODO: type switch?
- TODO: select
- TODO: words about slices
- TODO: what is nil? do we type-test by a nil conversion or something else?
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