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
33101926
Commit
33101926
authored
Jul 16, 2008
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
channel tests with new syntax
SVN=127436
parent
f550cd67
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
49 deletions
+49
-49
fifo.go
test/chan/fifo.go
+9
-9
sieve.go
test/chan/sieve.go
+34
-34
sieve.go
test/sieve.go
+6
-6
No files found.
test/chan/fifo.go
View file @
33101926
...
...
@@ -13,22 +13,22 @@ const N = 10
func
AsynchFifo
()
{
ch
:=
new
(
chan
int
,
N
);
for
i
:=
0
;
i
<
N
;
i
++
{
>
ch
=
i
ch
-<
i
}
for
i
:=
0
;
i
<
N
;
i
++
{
if
<
ch
!=
i
{
if
<
-
ch
!=
i
{
print
"bad receive
\n
"
;
sys
.
exit
(
1
);
}
}
}
func
Chain
(
ch
*
chan
<
int
,
val
int
,
in
*
chan
<
int
,
out
*
chan
>
int
)
{
BUG
:=
<
in
;
if
<
ch
!=
val
{
panic
val
;
func
Chain
(
ch
*
chan
<
-
int
,
val
int
,
in
*
chan
<-
int
,
out
*
chan
-<
int
)
{
<-
in
;
if
<
-
ch
!=
val
{
panic
val
}
>
out
=
1
;
out
-<
1
}
// thread together a daisy chain to read the elements in sequence
...
...
@@ -41,9 +41,9 @@ func SynchFifo() {
go
Chain
(
ch
,
i
,
in
,
out
);
in
=
out
;
}
>
start
=
0
;
start
-<
0
;
for
i
:=
0
;
i
<
N
;
i
++
{
>
ch
=
i
ch
-<
i
}
}
...
...
test/chan/sieve.go
View file @
33101926
...
...
@@ -10,30 +10,30 @@
package
main
// Send the sequence 2, 3, 4, ... to channel 'ch'.
func
Generate
(
ch
*
chan
>
int
)
{
for
i
:=
2
;
i
<
100
;
i
++
{
>
ch
=
i
// Send 'i' to channel 'ch'.
func
Generate
(
ch
*
chan
-<
int
)
{
for
i
:=
2
;
;
i
++
{
ch
-<
i
// Send 'i' to channel 'ch'.
}
}
// Copy the values from channel 'in' to channel 'out',
// removing those divisible by 'prime'.
func
Filter
(
in
*
chan
<
int
,
out
*
chan
>
int
,
prime
int
)
{
func
Filter
(
in
*
chan
<
-
int
,
out
*
chan
-<
int
,
prime
int
)
{
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
{
>
out
=
i
// Send 'i' to channel 'out'.
out
-<
i
// Send 'i' to channel 'out'.
}
}
}
// The prime sieve: Daisy-chain Filter processes together.
func
Sieve
(
primes
*
chan
>
int
)
{
func
Sieve
()
{
ch
:=
new
(
chan
int
);
// Create a new channel.
go
Generate
(
ch
);
// Start Generate() as a subprocess.
for
{
prime
:=
<
ch
;
>
primes
=
prime
;
prime
:=
<
-
ch
;
print
prime
,
"
\n
"
;
ch1
:=
new
(
chan
int
);
go
Filter
(
ch
,
ch1
,
prime
);
ch
=
ch1
...
...
@@ -43,30 +43,30 @@ func Sieve(primes *chan> int) {
func
main
()
{
primes
:=
new
(
chan
int
);
go
Sieve
(
primes
);
if
<
primes
!=
2
{
panic
2
}
if
<
primes
!=
3
{
panic
3
}
if
<
primes
!=
5
{
panic
5
}
if
<
primes
!=
7
{
panic
7
}
if
<
primes
!=
11
{
panic
11
}
if
<
primes
!=
13
{
panic
13
}
if
<
primes
!=
17
{
panic
17
}
if
<
primes
!=
19
{
panic
19
}
if
<
primes
!=
23
{
panic
23
}
if
<
primes
!=
29
{
panic
29
}
if
<
primes
!=
31
{
panic
31
}
if
<
primes
!=
37
{
panic
37
}
if
<
primes
!=
41
{
panic
41
}
if
<
primes
!=
43
{
panic
43
}
if
<
primes
!=
47
{
panic
47
}
if
<
primes
!=
53
{
panic
53
}
if
<
primes
!=
59
{
panic
59
}
if
<
primes
!=
61
{
panic
61
}
if
<
primes
!=
67
{
panic
67
}
if
<
primes
!=
71
{
panic
71
}
if
<
primes
!=
73
{
panic
73
}
if
<
primes
!=
79
{
panic
79
}
if
<
primes
!=
83
{
panic
83
}
if
<
primes
!=
89
{
panic
89
}
if
<
primes
!=
97
{
panic
97
}
if
<
-
primes
!=
2
{
panic
2
}
if
<
-
primes
!=
3
{
panic
3
}
if
<
-
primes
!=
5
{
panic
5
}
if
<
-
primes
!=
7
{
panic
7
}
if
<
-
primes
!=
11
{
panic
11
}
if
<
-
primes
!=
13
{
panic
13
}
if
<
-
primes
!=
17
{
panic
17
}
if
<
-
primes
!=
19
{
panic
19
}
if
<
-
primes
!=
23
{
panic
23
}
if
<
-
primes
!=
29
{
panic
29
}
if
<
-
primes
!=
31
{
panic
31
}
if
<
-
primes
!=
37
{
panic
37
}
if
<
-
primes
!=
41
{
panic
41
}
if
<
-
primes
!=
43
{
panic
43
}
if
<
-
primes
!=
47
{
panic
47
}
if
<
-
primes
!=
53
{
panic
53
}
if
<
-
primes
!=
59
{
panic
59
}
if
<
-
primes
!=
61
{
panic
61
}
if
<
-
primes
!=
67
{
panic
67
}
if
<
-
primes
!=
71
{
panic
71
}
if
<
-
primes
!=
73
{
panic
73
}
if
<
-
primes
!=
79
{
panic
79
}
if
<
-
primes
!=
83
{
panic
83
}
if
<
-
primes
!=
89
{
panic
89
}
if
<
-
primes
!=
97
{
panic
97
}
sys
.
exit
(
0
);
}
test/sieve.go
View file @
33101926
...
...
@@ -7,19 +7,19 @@
package
main
// Send the sequence 2, 3, 4, ... to channel 'ch'.
func
Generate
(
ch
*
chan
>
int
)
{
func
Generate
(
ch
*
chan
-<
int
)
{
for
i
:=
2
;
;
i
++
{
>
ch
=
i
// Send 'i' to channel 'ch'.
ch
-<
i
// Send 'i' to channel 'ch'.
}
}
// Copy the values from channel 'in' to channel 'out',
// removing those divisible by 'prime'.
func
Filter
(
in
*
chan
<
int
,
out
*
chan
>
int
,
prime
int
)
{
func
Filter
(
in
*
chan
<
-
int
,
out
*
chan
-<
int
,
prime
int
)
{
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
{
>
out
=
i
// Send 'i' to channel 'out'.
out
-<
i
// Send 'i' to channel 'out'.
}
}
}
...
...
@@ -29,7 +29,7 @@ func Sieve() {
ch
:=
new
(
chan
int
);
// Create a new channel.
go
Generate
(
ch
);
// Start Generate() as a subprocess.
for
{
prime
:=
<
ch
;
prime
:=
<-
ch
;
print
prime
,
"
\n
"
;
ch1
:=
new
(
chan
int
);
go
Filter
(
ch
,
ch1
,
prime
);
...
...
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