1
0
mirror of https://github.com/golang/go synced 2024-11-24 22:00:09 -07:00

tutorial code:

tweak a program or two
delete unused programs
add shell script to run them all

R=gri
DELTA=213  (62 added, 147 deleted, 4 changed)
OCL=15435
CL=15437
This commit is contained in:
Rob Pike 2008-09-17 12:14:52 -07:00
parent 304440356d
commit f7a506bf42
6 changed files with 69 additions and 163 deletions

View File

@ -1,52 +0,0 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
type INT uint64
func Multiplier(f INT, in, out *chan INT) {
for {
out -< (f * <-in);
}
}
func min(a, b INT) INT {
if a < b { return a }
return b;
}
func main() {
c2i := new(chan INT, 100);
c2o := new(chan INT);
c3i := new(chan INT, 100);
c3o := new(chan INT);
c5i := new(chan INT, 100);
c5o := new(chan INT);
go Multiplier(2, c2i, c2o);
go Multiplier(3, c3i, c3o);
go Multiplier(5, c5i, c5o);
var x INT = 1;
x2 := x;
x3 := x;
x5 := x;
for i := 0; i < 100; i++ {
print(x, "\n");
c2i -< x;
c3i -< x;
c5i -< x;
if x2 == x { x2 = <- c2o }
if x3 == x { x3 = <- c3o }
if x5 == x { x5 = <- c5o }
x = min(min(x2, x3), x5);
}
sys.exit(0);
}

View File

@ -1,48 +0,0 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
type INT uint64
func Multiplier(f INT) (in, out *chan INT) {
inc := new(chan INT, 100);
outc := new(chan INT);
go func(f INT, in, out *chan INT) {
for {
out -< f * <-in;
}
}(f, inc, outc)
return inc, outc
}
func min(a, b INT) INT {
if a < b { return a }
return b;
}
func main() {
c2i, c2o := Multiplier(2);
c3i, c3o := Multiplier(3);
c5i, c5o := Multiplier(5);
var x INT = 1;
x2, x3, x5 := x, x, x;
for i := 0; i < 100; i++ {
print(x, "\n");
c2i -< x;
c3i -< x;
c5i -< x;
if x2 == x { x2 = <- c2o }
if x3 == x { x3 = <- c3o }
if x5 == x { x5 = <- c5o }
x = min(min(x2, x3), x5);
}
sys.exit(0);
}

View File

@ -1,59 +0,0 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
type INT uint64
func Multiplier(f INT) (in, out *chan INT) {
in = new(chan INT, 100);
out = new(chan INT, 100);
go func(in, out *chan INT, f INT) {
for {
out -< f * <- in;
}
}(in, out, f);
return in, out;
}
func min(xs *[]INT) INT {
m := xs[0];
for i := 1; i < len(xs); i++ {
if xs[i] < m {
m = xs[i];
}
}
return m;
}
func main() {
F := []INT{2, 3, 5};
const n = len(F);
x := INT(1);
ins := new([]*chan INT, n);
outs := new([]*chan INT, n);
xs := new([]INT, n);
for i := 0; i < n; i++ {
ins[i], outs[i] = Multiplier(F[i]);
xs[i] = x;
}
for i := 0; i < 100; i++ {
print(x, "\n");
t := min(xs);
for i := 0; i < n; i++ {
ins[i] -< x;
}
for i := 0; i < n; i++ {
if xs[i] == x { xs[i] = <- outs[i]; }
}
x = min(xs);
}
sys.exit(0);
}

View File

@ -7,7 +7,7 @@ package main
import FD "fd" import FD "fd"
func main() { func main() {
hello := []byte{'h', 'e', 'l', 'l', 'o', ', ', ' ', 'w', 'o', 'r', 'l', 'd', '\n'}; hello := []byte{'h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '\n'};
FD.Stdout.Write(&hello); FD.Stdout.Write(&hello);
fd, errno := FD.Open("/does/not/exist", 0, 0); fd, errno := FD.Open("/does/not/exist", 0, 0);
if fd == nil { if fd == nil {

65
doc/progs/run Executable file
View File

@ -0,0 +1,65 @@
#!/bin/bash
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
rm -f *.6
for i in \
fd.go \
helloworld.go \
helloworld2.go \
helloworld3.go \
echo.go \
cat.go \
cat_rot13.go \
sum.go \
sort.go \
sortmain.go \
sieve.go \
sieve1.go \
server1.go \
; do
BASE=$(basename $i .go)
6g $i
done
function testit {
6l $1.6
x=$(echo $(6.out $2 2>&1)) # extra echo canonicalizes
if [ "$x" != "$3" ]
then
echo $1 failed: '"'$x'"' is not '"'$3'"'
fi
}
function testitpipe {
6l $1.6
x=$(echo $(6.out | $2 2>&1)) # extra echo canonicalizes
if [ "$x" != "$3" ]
then
echo $1 failed: '"'$x'"' is not '"'$3'"'
fi
}
testit helloworld "" "Hello, world; or Καλημέρα κόσμε; or こんにちは 世界"
testit helloworld2 "" "Hello, world; or Καλημέρα κόσμε; or こんにちは 世界"
testit helloworld3 "" "hello, world can't open file; errno=2"
testit echo "hello, world" "hello, world"
testit sum "" "6"
alphabet=abcdefghijklmnopqrstuvwxyz
rot13=nopqrstuvwxyzabcdefghijklm
echo $alphabet | testit cat "" $alphabet
echo $alphabet | testit cat_rot13 "--rot13" $rot13
echo $rot13 | testit cat_rot13 "--rot13" $alphabet
testit sortmain "" "Sunday Monday Tuesday Thursday Friday"
testitpipe sieve "sed 10q" "2 3 5 7 11 13 17 19 23 29"
testitpipe sieve "sed 10q" "2 3 5 7 11 13 17 19 23 29"
# server hangs; don't run it
testit server1 "" ""

View File

@ -27,9 +27,9 @@ func Server(op *BinOp, service *chan *Request, quit *chan bool) {
} }
} }
func StartServer(op *BinOp) (servch *chan *Request, quitch *chan bool) { func StartServer(op *BinOp) (service *chan *Request, quit *chan bool) {
service := new(chan *Request); service = new(chan *Request);
quit := new(chan bool); quit = new(chan bool);
go Server(op, service, quit); go Server(op, service, quit);
return service, quit; return service, quit;
} }