diff options
author | Rob Pike <r@golang.org> | 2009-01-06 15:49:27 -0800 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2009-01-06 15:49:27 -0800 |
commit | 5df73e8b14d435bcb5f110ca9e2ae6ab7917e8ee (patch) | |
tree | baa8a65017491726042d244d68061a3599ab7741 /doc/progs | |
parent | 92743b1fbff6b14864c4ffd200042e5a65a2b1c1 (diff) | |
download | golang-5df73e8b14d435bcb5f110ca9e2ae6ab7917e8ee.tar.gz |
make the tutorial programs run again.
(the text still needs fixing)
add the tutorial programs to the test run.
R=rsc
DELTA=41 (6 added, 0 deleted, 35 changed)
OCL=22174
CL=22174
Diffstat (limited to 'doc/progs')
-rw-r--r-- | doc/progs/cat.go | 4 | ||||
-rw-r--r-- | doc/progs/cat_rot13.go | 8 | ||||
-rw-r--r-- | doc/progs/fd.go | 4 | ||||
-rw-r--r-- | doc/progs/helloworld3.go | 2 | ||||
-rwxr-xr-x | doc/progs/run | 6 | ||||
-rw-r--r-- | doc/progs/server.go | 6 | ||||
-rw-r--r-- | doc/progs/server1.go | 12 | ||||
-rw-r--r-- | doc/progs/sieve.go | 10 | ||||
-rw-r--r-- | doc/progs/sieve1.go | 18 |
9 files changed, 36 insertions, 34 deletions
diff --git a/doc/progs/cat.go b/doc/progs/cat.go index 993f9d59c..f74faf4f1 100644 --- a/doc/progs/cat.go +++ b/doc/progs/cat.go @@ -13,14 +13,14 @@ func cat(fd *FD.FD) { const NBUF = 512; var buf [NBUF]byte; for { - switch nr, er := fd.Read(&buf); true { + switch nr, er := fd.Read(buf); true { case nr < 0: print("error reading from ", fd.Name(), ": ", er, "\n"); sys.exit(1); case nr == 0: // EOF return; case nr > 0: - if nw, ew := FD.Stdout.Write((&buf)[0:nr]); nw != nr { + if nw, ew := FD.Stdout.Write(buf[0:nr]); nw != nr { print("error writing from ", fd.Name(), ": ", ew, "\n"); } } diff --git a/doc/progs/cat_rot13.go b/doc/progs/cat_rot13.go index a8c570add..1ef0f6443 100644 --- a/doc/progs/cat_rot13.go +++ b/doc/progs/cat_rot13.go @@ -22,7 +22,7 @@ func rot13(b byte) byte { } type Reader interface { - Read(b *[]byte) (ret int64, errno int64); + Read(b []byte) (ret int64, errno int64); Name() string; } @@ -36,7 +36,7 @@ func NewRot13(source Reader) *Rot13 { return r13 } -func (r13 *Rot13) Read(b *[]byte) (ret int64, errno int64) { +func (r13 *Rot13) Read(b []byte) (ret int64, errno int64) { // TODO: use standard Read sig? r, e := r13.source.Read(b); for i := int64(0); i < r; i++ { b[i] = rot13(b[i]) @@ -57,14 +57,14 @@ func cat(r Reader) { r = NewRot13(r) } for { - switch nr, er := r.Read(&buf); { + switch nr, er := r.Read(buf); { case nr < 0: print("error reading from ", r.Name(), ": ", er, "\n"); sys.exit(1); case nr == 0: // EOF return; case nr > 0: - nw, ew := FD.Stdout.Write((&buf)[0:nr]); + nw, ew := FD.Stdout.Write(buf[0:nr]); if nw != nr { print("error writing from ", r.Name(), ": ", ew, "\n"); } diff --git a/doc/progs/fd.go b/doc/progs/fd.go index 9ec7d5493..76b784f83 100644 --- a/doc/progs/fd.go +++ b/doc/progs/fd.go @@ -41,7 +41,7 @@ func (fd *FD) Close() int64 { return 0 } -func (fd *FD) Read(b *[]byte) (ret int64, errno int64) { +func (fd *FD) Read(b []byte) (ret int64, errno int64) { if fd == nil { return -1, Syscall.EINVAL } @@ -49,7 +49,7 @@ func (fd *FD) Read(b *[]byte) (ret int64, errno int64) { return r, e } -func (fd *FD) Write(b *[]byte) (ret int64, errno int64) { +func (fd *FD) Write(b []byte) (ret int64, errno int64) { if fd == nil { return -1, Syscall.EINVAL } diff --git a/doc/progs/helloworld3.go b/doc/progs/helloworld3.go index 2347a1a19..f282bcefb 100644 --- a/doc/progs/helloworld3.go +++ b/doc/progs/helloworld3.go @@ -8,7 +8,7 @@ import FD "fd" func main() { 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); if fd == nil { print("can't open file; errno=", errno, "\n"); diff --git a/doc/progs/run b/doc/progs/run index 489cab5bd..e04c1031e 100755 --- a/doc/progs/run +++ b/doc/progs/run @@ -27,7 +27,7 @@ done function testit { 6l $1.6 - x=$(echo $(6.out $2 2>&1)) # extra echo canonicalizes + x=$(echo $(./6.out $2 2>&1)) # extra echo canonicalizes if [ "$x" != "$3" ] then echo $1 failed: '"'$x'"' is not '"'$3'"' @@ -36,7 +36,7 @@ function testit { function testitpipe { 6l $1.6 - x=$(echo $(6.out | $2 2>&1)) # extra echo canonicalizes + x=$(echo $(./6.out | $2 2>&1)) # extra echo canonicalizes if [ "$x" != "$3" ] then echo $1 failed: '"'$x'"' is not '"'$3'"' @@ -63,3 +63,5 @@ testitpipe sieve "sed 10q" "2 3 5 7 11 13 17 19 23 29" # server hangs; don't run it testit server1 "" "" + +rm -f 6.out *.6 diff --git a/doc/progs/server.go b/doc/progs/server.go index ea089785d..7a21e4396 100644 --- a/doc/progs/server.go +++ b/doc/progs/server.go @@ -6,7 +6,7 @@ package main type Request struct { a, b int; - replyc *chan int; + replyc chan int; } type BinOp (a, b int) int; @@ -16,14 +16,14 @@ func Run(op *BinOp, request *Request) { request.replyc <- result; } -func Server(op *BinOp, service *chan *Request) { +func Server(op *BinOp, service chan *Request) { for { request := <-service; go Run(op, request); // don't wait for it } } -func StartServer(op *BinOp) *chan *Request { +func StartServer(op *BinOp) chan *Request { req := new(chan *Request); go Server(op, req); return req; diff --git a/doc/progs/server1.go b/doc/progs/server1.go index d70ddfd9d..b7e489d46 100644 --- a/doc/progs/server1.go +++ b/doc/progs/server1.go @@ -6,7 +6,7 @@ package main type Request struct { a, b int; - replyc *chan int; + replyc chan int; } type BinOp (a, b int) int; @@ -16,7 +16,7 @@ func Run(op *BinOp, request *Request) { request.replyc <- result; } -func Server(op *BinOp, service *chan *Request, quit *chan bool) { +func Server(op *BinOp, service chan *Request, quit chan bool) { for { select { case request := <-service: @@ -27,9 +27,9 @@ func Server(op *BinOp, service *chan *Request, quit *chan bool) { } } -func StartServer(op *BinOp) (service *chan *Request, quit *chan bool) { - service = new(chan *Request); - quit = new(chan bool); +func StartServer(op *BinOp) (service chan *Request, quit chan bool) { + service = make(chan *Request); + quit = make(chan bool); go Server(op, service, quit); return service, quit; } @@ -42,7 +42,7 @@ func main() { req := &reqs[i]; req.a = i; req.b = i + N; - req.replyc = new(chan int); + req.replyc = make(chan int); adder <- req; } for i := N-1; i >= 0; i-- { // doesn't matter what order diff --git a/doc/progs/sieve.go b/doc/progs/sieve.go index 1ee60bddf..22e14535e 100644 --- a/doc/progs/sieve.go +++ b/doc/progs/sieve.go @@ -5,7 +5,7 @@ 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'. } @@ -13,9 +13,9 @@ func Generate(ch *chan int) { // Copy the values from channel 'in' to channel 'out', // removing those divisible by 'prime'. -func Filter(in, out *chan int, prime int) { +func Filter(in, 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'. } @@ -24,12 +24,12 @@ func Filter(in, out *chan int, prime int) { // The prime sieve: Daisy-chain Filter processes together. func main() { - ch := new(chan int); // Create a new channel. + ch := make(chan int); // Create a new channel. go Generate(ch); // Start Generate() as a goroutine. for { prime := <-ch; print(prime, "\n"); - ch1 := new(chan int); + ch1 := make(chan int); go Filter(ch, ch1, prime); ch = ch1 } diff --git a/doc/progs/sieve1.go b/doc/progs/sieve1.go index d1c3c7277..3f2cb9eac 100644 --- a/doc/progs/sieve1.go +++ b/doc/progs/sieve1.go @@ -5,9 +5,9 @@ package main // Send the sequence 2, 3, 4, ... to returned channel -func Generate() *chan int { - ch := new(chan int); - go func(ch *chan int){ +func Generate() chan int { + ch := make(chan int); + go func(ch chan int){ for i := 2; ; i++ { ch <- i } @@ -16,9 +16,9 @@ func Generate() *chan int { } // Filter out input values divisible by 'prime', send rest to returned channel -func Filter(in *chan int, prime int) *chan int { - out := new(chan int); - go func(in *chan int, out *chan int, prime int) { +func Filter(in chan int, prime int) chan int { + out := make(chan int); + go func(in chan int, out chan int, prime int) { for { if i := <-in; i % prime != 0 { out <- i @@ -28,9 +28,9 @@ func Filter(in *chan int, prime int) *chan int { return out; } -func Sieve() *chan int { - out := new(chan int); - go func(out *chan int) { +func Sieve() chan int { + out := make(chan int); + go func(out chan int) { ch := Generate(); for { prime := <-ch; |