summaryrefslogtreecommitdiff
path: root/doc/progs
diff options
context:
space:
mode:
Diffstat (limited to 'doc/progs')
-rw-r--r--doc/progs/cat.go8
-rw-r--r--doc/progs/cat_rot13.go7
-rw-r--r--doc/progs/helloworld.go4
-rw-r--r--doc/progs/helloworld2.go11
-rw-r--r--doc/progs/helloworld3.go7
-rw-r--r--doc/progs/printf.go11
-rwxr-xr-xdoc/progs/run4
-rw-r--r--doc/progs/server.go6
-rw-r--r--doc/progs/server1.go4
-rw-r--r--doc/progs/sieve.go4
-rw-r--r--doc/progs/sieve1.go4
-rw-r--r--doc/progs/sortmain.go9
-rw-r--r--doc/progs/strings.go4
-rw-r--r--doc/progs/sum.go4
14 files changed, 42 insertions, 45 deletions
diff --git a/doc/progs/cat.go b/doc/progs/cat.go
index b46487fd2..c06a730ce 100644
--- a/doc/progs/cat.go
+++ b/doc/progs/cat.go
@@ -7,6 +7,8 @@ package main
import (
"file";
"flag";
+ "fmt";
+ "os";
)
func cat(f *file.File) {
@@ -15,13 +17,13 @@ func cat(f *file.File) {
for {
switch nr, er := f.Read(buf); true {
case nr < 0:
- print("error reading from ", f.String(), ": ", er.String(), "\n");
+ fmt.Fprintf(os.Stderr, "error reading from %s: %s\n", f.String(), er.String());
sys.Exit(1);
case nr == 0: // EOF
return;
case nr > 0:
if nw, ew := file.Stdout.Write(buf[0:nr]); nw != nr {
- print("error writing from ", f.String(), ": ", ew.String(), "\n");
+ fmt.Fprintf(os.Stderr, "error writing from %s: %s\n", f.String(), ew.String());
}
}
}
@@ -35,7 +37,7 @@ func main() {
for i := 0; i < flag.NArg(); i++ {
f, err := file.Open(flag.Arg(i), 0, 0);
if f == nil {
- print("can't open ", flag.Arg(i), ": error ", err, "\n");
+ fmt.Fprintf(os.Stderr, "can't open %s: error %s\n", flag.Arg(i), err);
sys.Exit(1);
}
cat(f);
diff --git a/doc/progs/cat_rot13.go b/doc/progs/cat_rot13.go
index 27d1e467f..618ae9111 100644
--- a/doc/progs/cat_rot13.go
+++ b/doc/progs/cat_rot13.go
@@ -7,6 +7,7 @@ package main
import (
"file";
"flag";
+ "fmt";
"os";
)
@@ -58,14 +59,14 @@ func cat(r reader) {
for {
switch nr, er := r.Read(buf); {
case nr < 0:
- print("error reading from ", r.String(), ": ", er.String(), "\n");
+ fmt.Fprintf(os.Stderr, "error reading from %s: %s\n", r.String(), er.String());
sys.Exit(1);
case nr == 0: // EOF
return;
case nr > 0:
nw, ew := file.Stdout.Write(buf[0:nr]);
if nw != nr {
- print("error writing from ", r.String(), ": ", ew.String(), "\n");
+ fmt.Fprintf(os.Stderr, "error writing from %s: %s\n", r.String(), ew.String());
}
}
}
@@ -79,7 +80,7 @@ func main() {
for i := 0; i < flag.NArg(); i++ {
f, err := file.Open(flag.Arg(i), 0, 0);
if f == nil {
- print("can't open ", flag.Arg(i), ": error ", err, "\n");
+ fmt.Fprintf(os.Stderr, "can't open %s: error %s\n", flag.Arg(i), err);
sys.Exit(1);
}
cat(f);
diff --git a/doc/progs/helloworld.go b/doc/progs/helloworld.go
index b77b72088..c4c3855ed 100644
--- a/doc/progs/helloworld.go
+++ b/doc/progs/helloworld.go
@@ -4,6 +4,8 @@
package main
+import fmt "fmt" // Package implementing formatted I/O.
+
func main() {
- print("Hello, world; or Καλημέρα κόσμε; or こんにちは 世界\n");
+ fmt.Printf("Hello, world; or Καλημέρα κόσμε; or こんにちは 世界\n");
}
diff --git a/doc/progs/helloworld2.go b/doc/progs/helloworld2.go
deleted file mode 100644
index 66b32ed54..000000000
--- a/doc/progs/helloworld2.go
+++ /dev/null
@@ -1,11 +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
-
-import os "os" // this package contains features for basic I/O
-
-func main() {
- os.Stdout.WriteString("Hello, world; or Καλημέρα κόσμε; or こんにちは 世界\n");
-}
diff --git a/doc/progs/helloworld3.go b/doc/progs/helloworld3.go
index 18fa594f0..630c49232 100644
--- a/doc/progs/helloworld3.go
+++ b/doc/progs/helloworld3.go
@@ -4,14 +4,17 @@
package main
-import file "file"
+import (
+ "file";
+ "fmt";
+)
func main() {
hello := []byte{'h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '\n'};
file.Stdout.Write(hello);
file, err := file.Open("/does/not/exist", 0, 0);
if file == nil {
- print("can't open file; err=", err.String(), "\n");
+ fmt.Printf("can't open file; err=%s\n", err.String());
sys.Exit(1);
}
}
diff --git a/doc/progs/printf.go b/doc/progs/printf.go
deleted file mode 100644
index 3bd70f264..000000000
--- a/doc/progs/printf.go
+++ /dev/null
@@ -1,11 +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
-
-import "fmt"
-
-func main() {
- fmt.Printf("hello, %s\n", "world");
-}
diff --git a/doc/progs/run b/doc/progs/run
index 6f047b155..c02a632d0 100755
--- a/doc/progs/run
+++ b/doc/progs/run
@@ -8,7 +8,6 @@ rm -f *.6
for i in \
file.go \
helloworld.go \
- helloworld2.go \
helloworld3.go \
echo.go \
cat.go \
@@ -17,7 +16,6 @@ for i in \
sort.go \
sortmain.go \
print.go \
- printf.go \
print_string.go \
sieve.go \
sieve1.go \
@@ -48,7 +46,6 @@ function testitpipe {
testit helloworld "" "Hello, world; or Καλημέρα κόσμε; or こんにちは 世界"
-testit helloworld2 "" "Hello, world; or Καλημέρα κόσμε; or こんにちは 世界"
testit helloworld3 "" "hello, world can't open file; err=No such file or directory"
testit echo "hello, world" "hello, world"
testit sum "" "6"
@@ -62,7 +59,6 @@ echo $rot13 | testit cat_rot13 "--rot13" $alphabet
testit sortmain "" "Sunday Monday Tuesday Thursday Friday"
testit print "" "18446744073709551615 -1 18446744073709551615 {77 Sunset Strip} [1 2 3 4] 18446744073709551615 {77 Sunset Strip} [1 2 3 4] 18446744073709551615 {77 Sunset Strip} [1 2 3 4]"
-testit printf "" "hello, world"
testit print_string "" "77 Sunset Strip"
testitpipe sieve "sed 10q" "2 3 5 7 11 13 17 19 23 29"
diff --git a/doc/progs/server.go b/doc/progs/server.go
index a5317f27f..8906e9635 100644
--- a/doc/progs/server.go
+++ b/doc/progs/server.go
@@ -4,6 +4,8 @@
package main
+import "fmt"
+
type request struct {
a, b int;
replyc chan int;
@@ -42,8 +44,8 @@ func main() {
}
for i := N-1; i >= 0; i-- { // doesn't matter what order
if <-reqs[i].replyc != N + 2*i {
- print("fail at ", i, "\n");
+ fmt.Println("fail at", i);
}
}
- print("done\n");
+ fmt.Println("done");
}
diff --git a/doc/progs/server1.go b/doc/progs/server1.go
index 46d7b4ccf..591e27606 100644
--- a/doc/progs/server1.go
+++ b/doc/progs/server1.go
@@ -4,6 +4,8 @@
package main
+import "fmt"
+
type request struct {
a, b int;
replyc chan int;
@@ -47,7 +49,7 @@ func main() {
}
for i := N-1; i >= 0; i-- { // doesn't matter what order
if <-reqs[i].replyc != N + 2*i {
- print("fail at ", i, "\n");
+ fmt.Println("fail at", i);
}
}
quit <- true;
diff --git a/doc/progs/sieve.go b/doc/progs/sieve.go
index 2da7df475..cd011d293 100644
--- a/doc/progs/sieve.go
+++ b/doc/progs/sieve.go
@@ -4,6 +4,8 @@
package main
+import "fmt"
+
// Send the sequence 2, 3, 4, ... to channel 'ch'.
func generate(ch chan int) {
for i := 2; ; i++ {
@@ -28,7 +30,7 @@ func main() {
go generate(ch); // Start generate() as a goroutine.
for {
prime := <-ch;
- print(prime, "\n");
+ fmt.Println(prime);
ch1 := make(chan int);
go filter(ch, ch1, prime);
ch = ch1
diff --git a/doc/progs/sieve1.go b/doc/progs/sieve1.go
index c9b27f061..567f5d9bb 100644
--- a/doc/progs/sieve1.go
+++ b/doc/progs/sieve1.go
@@ -4,6 +4,8 @@
package main
+import "fmt"
+
// Send the sequence 2, 3, 4, ... to returned channel
func generate() chan int {
ch := make(chan int);
@@ -44,6 +46,6 @@ func sieve() chan int {
func main() {
primes := sieve();
for {
- print(<-primes, "\n");
+ fmt.Println(<-primes);
}
}
diff --git a/doc/progs/sortmain.go b/doc/progs/sortmain.go
index 74d1d1840..035ca5442 100644
--- a/doc/progs/sortmain.go
+++ b/doc/progs/sortmain.go
@@ -4,7 +4,10 @@
package main
-import "sort"
+import (
+ "fmt";
+ "sort";
+)
func ints() {
data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586};
@@ -53,9 +56,9 @@ func days() {
panic()
}
for i, d := range data {
- print(d.long_name, " ")
+ fmt.Printf("%s ", d.long_name)
}
- print("\n")
+ fmt.Printf("\n")
}
diff --git a/doc/progs/strings.go b/doc/progs/strings.go
index 28553c26a..ee2596d8f 100644
--- a/doc/progs/strings.go
+++ b/doc/progs/strings.go
@@ -4,9 +4,11 @@
package main
+import "fmt"
+
func main() {
s := "hello";
- if s[1] == 'e' { print("success") }
+ if s[1] != 'e' { sys.Exit(1) }
s = "good bye";
var p *string = &s;
*p = "ciao";
diff --git a/doc/progs/sum.go b/doc/progs/sum.go
index 3ca1a5877..19600af06 100644
--- a/doc/progs/sum.go
+++ b/doc/progs/sum.go
@@ -4,6 +4,8 @@
package main
+import "fmt"
+
func sum(a []int) int { // returns an int
s := 0;
for i := 0; i < len(a); i++ {
@@ -15,5 +17,5 @@ func sum(a []int) int { // returns an int
func main() {
s := sum([3]int{1,2,3}); // a slice of the array is passed to sum
- print(s, "\n");
+ fmt.Print(s, "\n");
}