Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
592d2e3d
Commit
592d2e3d
authored
Sep 17, 2008
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update to new communications syntax
R=gri OCL=15417 CL=15417
parent
27c0eb84
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
14 additions
and
18 deletions
+14
-18
go_tutorial.txt
doc/go_tutorial.txt
+3
-6
server.go
doc/progs/server.go
+2
-2
server1.go
doc/progs/server1.go
+4
-5
sieve.go
doc/progs/sieve.go
+2
-2
sieve1.go
doc/progs/sieve1.go
+3
-3
No files found.
doc/go_tutorial.txt
View file @
592d2e3d
...
@@ -456,18 +456,17 @@ Here is the first function in "progs/sieve.go":
...
@@ -456,18 +456,17 @@ Here is the first function in "progs/sieve.go":
--PROG progs/sieve.go /Send/ /^}/
--PROG progs/sieve.go /Send/ /^}/
The function "Generate" sends the sequence 2, 3, 4, 5, ... to its
The function "Generate" sends the sequence 2, 3, 4, 5, ... to its
argument channel, "ch", using the binary
send operator "-<
".
argument channel, "ch", using the binary
communications operator "<-
".
Channels block, so if there's no recipient for the the value on "ch",
Channels block, so if there's no recipient for the the value on "ch",
the send operation will wait until one becomes available.
the send operation will wait until one becomes available.
The "Filter" function has three arguments: an input channel, an output
The "Filter" function has three arguments: an input channel, an output
channel, and a prime number. It copies values from the input to the
channel, and a prime number. It copies values from the input to the
output, discarding anything divisible by the prime. The unary
prefix
output, discarding anything divisible by the prime. The unary
communications
operator "<-" (receive) retrieves the next value on the channel.
operator "<-" (receive) retrieves the next value on the channel.
--PROG progs/sieve.go /Copy/ /^}/
--PROG progs/sieve.go /Copy/ /^}/
The generator and filters execute concurrently. Go has
The generator and filters execute concurrently. Go has
its own model of process/threads/light-weight processes/coroutines,
its own model of process/threads/light-weight processes/coroutines,
so to avoid notational confusion we'll call concurrently executing
so to avoid notational confusion we'll call concurrently executing
...
@@ -567,9 +566,7 @@ Inside "Server", a "select" statement chooses which of the multiple communicatio
...
@@ -567,9 +566,7 @@ Inside "Server", a "select" statement chooses which of the multiple communicatio
listed by its cases can proceed. If all are blocked, it waits until one can proceed; if
listed by its cases can proceed. If all are blocked, it waits until one can proceed; if
multiple can proceed, it chooses one at random. In this instance, the "select" allows
multiple can proceed, it chooses one at random. In this instance, the "select" allows
the server to honor requests until it receives a quit message, at which point it
the server to honor requests until it receives a quit message, at which point it
returns, terminating its execution. (The language doesn't yet allow the ":="
returns, terminating its execution.
syntax in "select" statements, although it might one day. Also, observe the use
of the binary, infix form of the receive operator.)
All that's left is to strobe the "quit" channel
All that's left is to strobe the "quit" channel
...
...
doc/progs/server.go
View file @
592d2e3d
...
@@ -13,7 +13,7 @@ type BinOp (a, b int) int;
...
@@ -13,7 +13,7 @@ type BinOp (a, b int) int;
func
Run
(
op
*
BinOp
,
request
*
Request
)
{
func
Run
(
op
*
BinOp
,
request
*
Request
)
{
result
:=
op
(
request
.
a
,
request
.
b
);
result
:=
op
(
request
.
a
,
request
.
b
);
request
.
replyc
-<
result
;
request
.
replyc
<-
result
;
}
}
func
Server
(
op
*
BinOp
,
service
*
chan
*
Request
)
{
func
Server
(
op
*
BinOp
,
service
*
chan
*
Request
)
{
...
@@ -38,7 +38,7 @@ func main() {
...
@@ -38,7 +38,7 @@ func main() {
req
.
a
=
i
;
req
.
a
=
i
;
req
.
b
=
i
+
N
;
req
.
b
=
i
+
N
;
req
.
replyc
=
new
(
chan
int
);
req
.
replyc
=
new
(
chan
int
);
adder
-<
req
;
adder
<-
req
;
}
}
for
i
:=
N
-
1
;
i
>=
0
;
i
--
{
// doesn't matter what order
for
i
:=
N
-
1
;
i
>=
0
;
i
--
{
// doesn't matter what order
if
<-
reqs
[
i
]
.
replyc
!=
N
+
2
*
i
{
if
<-
reqs
[
i
]
.
replyc
!=
N
+
2
*
i
{
...
...
doc/progs/server1.go
View file @
592d2e3d
...
@@ -13,14 +13,13 @@ type BinOp (a, b int) int;
...
@@ -13,14 +13,13 @@ type BinOp (a, b int) int;
func
Run
(
op
*
BinOp
,
request
*
Request
)
{
func
Run
(
op
*
BinOp
,
request
*
Request
)
{
result
:=
op
(
request
.
a
,
request
.
b
);
result
:=
op
(
request
.
a
,
request
.
b
);
request
.
replyc
-<
result
;
request
.
replyc
<-
result
;
}
}
func
Server
(
op
*
BinOp
,
service
*
chan
*
Request
,
quit
*
chan
bool
)
{
func
Server
(
op
*
BinOp
,
service
*
chan
*
Request
,
quit
*
chan
bool
)
{
for
{
for
{
var
request
*
Request
;
select
{
select
{
case
request
<-
service
:
case
request
:=
<-
service
:
go
Run
(
op
,
request
);
// don't wait for it
go
Run
(
op
,
request
);
// don't wait for it
case
<-
quit
:
case
<-
quit
:
return
;
return
;
...
@@ -44,12 +43,12 @@ func main() {
...
@@ -44,12 +43,12 @@ func main() {
req
.
a
=
i
;
req
.
a
=
i
;
req
.
b
=
i
+
N
;
req
.
b
=
i
+
N
;
req
.
replyc
=
new
(
chan
int
);
req
.
replyc
=
new
(
chan
int
);
adder
-<
req
;
adder
<-
req
;
}
}
for
i
:=
N
-
1
;
i
>=
0
;
i
--
{
// doesn't matter what order
for
i
:=
N
-
1
;
i
>=
0
;
i
--
{
// doesn't matter what order
if
<-
reqs
[
i
]
.
replyc
!=
N
+
2
*
i
{
if
<-
reqs
[
i
]
.
replyc
!=
N
+
2
*
i
{
print
(
"fail at "
,
i
,
"
\n
"
);
print
(
"fail at "
,
i
,
"
\n
"
);
}
}
}
}
quit
-<
true
;
quit
<-
true
;
}
}
doc/progs/sieve.go
View file @
592d2e3d
...
@@ -7,7 +7,7 @@ package main
...
@@ -7,7 +7,7 @@ package main
// Send the sequence 2, 3, 4, ... to channel 'ch'.
// Send the sequence 2, 3, 4, ... to channel 'ch'.
func
Generate
(
ch
*
chan
int
)
{
func
Generate
(
ch
*
chan
int
)
{
for
i
:=
2
;
;
i
++
{
for
i
:=
2
;
;
i
++
{
ch
-<
i
// Send 'i' to channel 'ch'.
ch
<-
i
// Send 'i' to channel 'ch'.
}
}
}
}
...
@@ -17,7 +17,7 @@ func Filter(in *chan int, out *chan int, prime int) {
...
@@ -17,7 +17,7 @@ func Filter(in *chan int, out *chan int, prime int) {
for
{
for
{
i
:=
<-
in
// Receive value of new variable 'i' from 'in'.
i
:=
<-
in
// Receive value of new variable 'i' from 'in'.
if
i
%
prime
!=
0
{
if
i
%
prime
!=
0
{
out
-<
i
// Send 'i' to channel 'out'.
out
<-
i
// Send 'i' to channel 'out'.
}
}
}
}
}
}
...
...
doc/progs/sieve1.go
View file @
592d2e3d
...
@@ -9,7 +9,7 @@ func Generate() *chan int {
...
@@ -9,7 +9,7 @@ func Generate() *chan int {
ch
:=
new
(
chan
int
);
ch
:=
new
(
chan
int
);
go
func
(
ch
*
chan
int
){
go
func
(
ch
*
chan
int
){
for
i
:=
2
;
;
i
++
{
for
i
:=
2
;
;
i
++
{
ch
-<
i
ch
<-
i
}
}
}(
ch
);
}(
ch
);
return
ch
;
return
ch
;
...
@@ -21,7 +21,7 @@ func Filter(in *chan int, prime int) *chan int {
...
@@ -21,7 +21,7 @@ func Filter(in *chan int, prime int) *chan int {
go
func
(
in
*
chan
int
,
out
*
chan
int
,
prime
int
)
{
go
func
(
in
*
chan
int
,
out
*
chan
int
,
prime
int
)
{
for
{
for
{
if
i
:=
<-
in
;
i
%
prime
!=
0
{
if
i
:=
<-
in
;
i
%
prime
!=
0
{
out
-<
i
out
<-
i
}
}
}
}
}(
in
,
out
,
prime
);
}(
in
,
out
,
prime
);
...
@@ -34,7 +34,7 @@ func Sieve() *chan int {
...
@@ -34,7 +34,7 @@ func Sieve() *chan int {
ch
:=
Generate
();
ch
:=
Generate
();
for
{
for
{
prime
:=
<-
ch
;
prime
:=
<-
ch
;
out
-<
prime
;
out
<-
prime
;
ch
=
Filter
(
ch
,
prime
);
ch
=
Filter
(
ch
,
prime
);
}
}
}(
out
);
}(
out
);
...
...
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