Commit 34356e9a authored by Rob Pike's avatar Rob Pike

update tutorial.

R=rsc
CC=golang-dev
https://golang.org/cl/179063
parent 1c729599
This diff is collapsed.
......@@ -47,6 +47,33 @@ The comment convention is the same as in C++:
Later we'll have much more to say about printing.
Semicolons
----
You might have noticed that our program has no semicolons. In Go
code, the only place you typically see semicolons is separating the
clauses of "for" loops and the like; they are not necessary after
every statement.
In fact, what happens is that the formal language uses semicolons,
much as in C or Java, but they are inserted automatically
at the end of every line that looks like the end of a statement. You
don't need to type them yourself.
For details about how this is done you can see the language
specification, but in practice all you need to know is that you
never need to put a semicolon at the end of a line. (You can put
them in if you want to write multiple statements per line.) As an
extra help, you can also leave out a semicolon immediately before
a closing brace.
This approach makes for clean-looking, semicolon-free code. The
one surprise is that it's important to put the opening
brace of a construct such as an "if" statement on the same line as
the "if"; if you don't, there are situations that may not compile
or may give the wrong result. The language forces the brace style
to some extent.
Compiling
----
......@@ -84,27 +111,12 @@ This program is small but it's doing a number of new things. In the last exampl
we saw "func" introduce a function. The keywords "var", "const", and "type"
(not used yet) also introduce declarations, as does "import".
Notice that we can group declarations of the same sort into
parenthesized, semicolon-separated lists if we want, as on lines 7-10 and 14-17.
parenthesized lists, one item per line, as on lines 7-10 and 14-17.
But it's not necessary to do so; we could have said
const Space = " "
const Newline = "\n"
Semicolons aren't needed here; in fact, semicolons are unnecessary after any
top-level declaration, although they are needed as separators <i>within</i>
a parenthesized list of declarations.
You can use semicolons just the way you would in C, C++, or Java, but if you
prefer you can also leave them out in many cases. They <i>separate</i> statements
rather than terminate them, so they aren't needed (but are still OK) at the end of the last
statement in a block.
They're also optional after braces, as in C.
Have a look at the source to "echo".
The only necessary semicolons in that program are on lines 8, 15, and 21
and of course between the elements of the "for" loop on line 22.
The ones on line 9, 16, 26, and 31 are optional but are there because a semicolon
on the end of a list of statements makes it easier to edit the list later.
This program imports the "&quot;os&quot;" package to access its "Stdout" variable, of type
"*os.File". The "import" statement is actually a declaration: in its general form,
as used in our ``hello world'' program,
......@@ -634,7 +646,7 @@ prints
In fact, if you're lazy the format "%v" will print, in a simple
appropriate style, any value, even an array or structure. The output of
--PROG progs/print.go 'NR==14' 'NR==17'
--PROG progs/print.go 'NR==14' 'NR==20'
is
......@@ -647,7 +659,7 @@ of "%v" while "Println" inserts spaces between arguments
and adds a newline. The output of each of these two lines is identical
to that of the "Printf" call above.
--PROG progs/print.go 'NR==18' 'NR==19'
--PROG progs/print.go 'NR==21' 'NR==22'
If you have your own type you'd like "Printf" or "Print" to format,
just give it a "String()" method that returns a string. The print
......
......@@ -5,42 +5,42 @@
package main
import (
"./file";
"flag";
"fmt";
"os";
"./file"
"flag"
"fmt"
"os"
)
func cat(f *file.File) {
const NBUF = 512;
var buf [NBUF]byte;
const NBUF = 512
var buf [NBUF]byte
for {
switch nr, er := f.Read(&buf); true {
case nr < 0:
fmt.Fprintf(os.Stderr, "cat: error reading from %s: %s\n", f.String(), er.String());
os.Exit(1);
fmt.Fprintf(os.Stderr, "cat: error reading from %s: %s\n", f.String(), er.String())
os.Exit(1)
case nr == 0: // EOF
return;
return
case nr > 0:
if nw, ew := file.Stdout.Write(buf[0:nr]); nw != nr {
fmt.Fprintf(os.Stderr, "cat: error writing from %s: %s\n", f.String(), ew.String());
fmt.Fprintf(os.Stderr, "cat: error writing from %s: %s\n", f.String(), ew.String())
}
}
}
}
func main() {
flag.Parse(); // Scans the arg list and sets up flags
flag.Parse() // Scans the arg list and sets up flags
if flag.NArg() == 0 {
cat(file.Stdin);
cat(file.Stdin)
}
for i := 0; i < flag.NArg(); i++ {
f, err := file.Open(flag.Arg(i), 0, 0);
f, err := file.Open(flag.Arg(i), 0, 0)
if f == nil {
fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", flag.Arg(i), err);
os.Exit(1);
fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", flag.Arg(i), err)
os.Exit(1)
}
cat(f);
f.Close();
cat(f)
f.Close()
}
}
......@@ -5,17 +5,17 @@
package main
import (
"./file";
"flag";
"fmt";
"os";
"./file"
"flag"
"fmt"
"os"
)
var rot13Flag = flag.Bool("rot13", false, "rot13 the input")
func rot13(b byte) byte {
if 'a' <= b && b <= 'z' {
b = 'a' + ((b - 'a') + 13) % 26;
b = 'a' + ((b - 'a') + 13) % 26
}
if 'A' <= b && b <= 'Z' {
b = 'A' + ((b - 'A') + 13) % 26
......@@ -24,12 +24,12 @@ func rot13(b byte) byte {
}
type reader interface {
Read(b []byte) (ret int, err os.Error);
String() string;
Read(b []byte) (ret int, err os.Error)
String() string
}
type rotate13 struct {
source reader;
source reader
}
func newRotate13(source reader) *rotate13 {
......@@ -37,7 +37,7 @@ func newRotate13(source reader) *rotate13 {
}
func (r13 *rotate13) Read(b []byte) (ret int, err os.Error) {
r, e := r13.source.Read(b);
r, e := r13.source.Read(b)
for i := 0; i < r; i++ {
b[i] = rot13(b[i])
}
......@@ -50,8 +50,8 @@ func (r13 *rotate13) String() string {
// end of rotate13 implementation
func cat(r reader) {
const NBUF = 512;
var buf [NBUF]byte;
const NBUF = 512
var buf [NBUF]byte
if *rot13Flag {
r = newRotate13(r)
......@@ -59,31 +59,31 @@ func cat(r reader) {
for {
switch nr, er := r.Read(&buf); {
case nr < 0:
fmt.Fprintf(os.Stderr, "cat: error reading from %s: %s\n", r.String(), er.String());
os.Exit(1);
fmt.Fprintf(os.Stderr, "cat: error reading from %s: %s\n", r.String(), er.String())
os.Exit(1)
case nr == 0: // EOF
return;
return
case nr > 0:
nw, ew := file.Stdout.Write(buf[0:nr]);
nw, ew := file.Stdout.Write(buf[0:nr])
if nw != nr {
fmt.Fprintf(os.Stderr, "cat: error writing from %s: %s\n", r.String(), ew.String());
fmt.Fprintf(os.Stderr, "cat: error writing from %s: %s\n", r.String(), ew.String())
}
}
}
}
func main() {
flag.Parse(); // Scans the arg list and sets up flags
flag.Parse() // Scans the arg list and sets up flags
if flag.NArg() == 0 {
cat(file.Stdin);
cat(file.Stdin)
}
for i := 0; i < flag.NArg(); i++ {
f, err := file.Open(flag.Arg(i), 0, 0);
f, err := file.Open(flag.Arg(i), 0, 0)
if f == nil {
fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", flag.Arg(i), err);
os.Exit(1);
fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", flag.Arg(i), err)
os.Exit(1)
}
cat(f);
f.Close();
cat(f)
f.Close()
}
}
......@@ -5,28 +5,28 @@
package main
import (
"os";
"flag"; // command line option parser
"os"
"flag" // command line option parser
)
var omitNewline = flag.Bool("n", false, "don't print final newline")
const (
Space = " ";
Newline = "\n";
Space = " "
Newline = "\n"
)
func main() {
flag.Parse(); // Scans the arg list and sets up flags
var s string = "";
flag.Parse() // Scans the arg list and sets up flags
var s string = ""
for i := 0; i < flag.NArg(); i++ {
if i > 0 {
s += Space
}
s += flag.Arg(i);
s += flag.Arg(i)
}
if !*omitNewline {
s += Newline
}
os.Stdout.WriteString(s);
os.Stdout.WriteString(s)
}
......@@ -5,13 +5,13 @@
package file
import (
"os";
"syscall";
"os"
"syscall"
)
type File struct {
fd int; // file descriptor number
name string; // file name at Open time
fd int // file descriptor number
name string // file name at Open time
}
func newFile(fd int, name string) *File {
......@@ -22,15 +22,15 @@ func newFile(fd int, name string) *File {
}
var (
Stdin = newFile(0, "/dev/stdin");
Stdout = newFile(1, "/dev/stdout");
Stderr = newFile(2, "/dev/stderr");
Stdin = newFile(0, "/dev/stdin")
Stdout = newFile(1, "/dev/stdout")
Stderr = newFile(2, "/dev/stderr")
)
func Open(name string, mode int, perm int) (file *File, err os.Error) {
r, e := syscall.Open(name, mode, perm);
r, e := syscall.Open(name, mode, perm)
if e != 0 {
err = os.Errno(e);
err = os.Errno(e)
}
return newFile(r, name), err
}
......@@ -39,10 +39,10 @@ func (file *File) Close() os.Error {
if file == nil {
return os.EINVAL
}
e := syscall.Close(file.fd);
file.fd = -1; // so it can't be closed again
e := syscall.Close(file.fd)
file.fd = -1 // so it can't be closed again
if e != 0 {
return os.Errno(e);
return os.Errno(e)
}
return nil
}
......@@ -51,9 +51,9 @@ func (file *File) Read(b []byte) (ret int, err os.Error) {
if file == nil {
return -1, os.EINVAL
}
r, e := syscall.Read(file.fd, b);
r, e := syscall.Read(file.fd, b)
if e != 0 {
err = os.Errno(e);
err = os.Errno(e)
}
return int(r), err
}
......@@ -62,9 +62,9 @@ func (file *File) Write(b []byte) (ret int, err os.Error) {
if file == nil {
return -1, os.EINVAL
}
r, e := syscall.Write(file.fd, b);
r, e := syscall.Write(file.fd, b)
if e != 0 {
err = os.Errno(e);
err = os.Errno(e)
}
return int(r), err
}
......
......@@ -7,5 +7,5 @@ package main
import fmt "fmt" // Package implementing formatted I/O.
func main() {
fmt.Printf("Hello, world; or Καλημέρα κόσμε; or こんにちは 世界\n");
fmt.Printf("Hello, world; or Καλημέρα κόσμε; or こんにちは 世界\n")
}
......@@ -5,17 +5,17 @@
package main
import (
"./file";
"fmt";
"os";
"./file"
"fmt"
"os"
)
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);
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 {
fmt.Printf("can't open file; err=%s\n", err.String());
os.Exit(1);
fmt.Printf("can't open file; err=%s\n", err.String())
os.Exit(1)
}
}
......@@ -7,14 +7,17 @@ package main
import "fmt"
func main() {
var u64 uint64 = 1<<64-1;
fmt.Printf("%d %d\n", u64, int64(u64));
var u64 uint64 = 1<<64-1
fmt.Printf("%d %d\n", u64, int64(u64))
// harder stuff
type T struct { a int; b string };
t := T{77, "Sunset Strip"};
a := []int{1, 2, 3, 4};
fmt.Printf("%v %v %v\n", u64, t, a);
fmt.Print(u64, " ", t, " ", a, "\n");
fmt.Println(u64, t, a);
type T struct {
a int
b string
}
t := T{77, "Sunset Strip"}
a := []int{1, 2, 3, 4}
fmt.Printf("%v %v %v\n", u64, t, a)
fmt.Print(u64, " ", t, " ", a, "\n")
fmt.Println(u64, t, a)
}
......@@ -6,13 +6,16 @@ package main
import "fmt"
type testType struct { a int; b string }
type testType struct {
a int
b string
}
func (t *testType) String() string {
return fmt.Sprint(t.a) + " " + t.b
}
func main() {
t := &testType{77, "Sunset Strip"};
t := &testType{77, "Sunset Strip"}
fmt.Println(t)
}
......@@ -7,45 +7,45 @@ package main
import "fmt"
type request struct {
a, b int;
replyc chan int;
a, b int
replyc chan int
}
type binOp func(a, b int) int
func run(op binOp, req *request) {
reply := op(req.a, req.b);
req.replyc <- reply;
reply := op(req.a, req.b)
req.replyc <- reply
}
func server(op binOp, service chan *request) {
for {
req := <-service;
go run(op, req); // don't wait for it
req := <-service
go run(op, req) // don't wait for it
}
}
func startServer(op binOp) chan *request {
req := make(chan *request);
go server(op, req);
return req;
req := make(chan *request)
go server(op, req)
return req
}
func main() {
adder := startServer(func(a, b int) int { return a + b });
const N = 100;
var reqs [N]request;
adder := startServer(func(a, b int) int { return a + b })
const N = 100
var reqs [N]request
for i := 0; i < N; i++ {
req := &reqs[i];
req.a = i;
req.b = i + N;
req.replyc = make(chan int);
adder <- req;
req := &reqs[i]
req.a = i
req.b = i + N
req.replyc = make(chan int)
adder <- req
}
for i := N-1; i >= 0; i-- { // doesn't matter what order
if <-reqs[i].replyc != N + 2*i {
fmt.Println("fail at", i);
fmt.Println("fail at", i)
}
}
fmt.Println("done");
fmt.Println("done")
}
......@@ -7,50 +7,50 @@ package main
import "fmt"
type request struct {
a, b int;
replyc chan int;
a, b int
replyc chan int
}
type binOp func(a, b int) int
func run(op binOp, req *request) {
reply := op(req.a, req.b);
req.replyc <- reply;
reply := op(req.a, req.b)
req.replyc <- reply
}
func server(op binOp, service chan *request, quit chan bool) {
for {
select {
case req := <-service:
go run(op, req); // don't wait for it
go run(op, req) // don't wait for it
case <-quit:
return;
return
}
}
}
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;
service = make(chan *request)
quit = make(chan bool)
go server(op, service, quit)
return service, quit
}
func main() {
adder, quit := startServer(func(a, b int) int { return a + b });
const N = 100;
var reqs [N]request;
adder, quit := startServer(func(a, b int) int { return a + b })
const N = 100
var reqs [N]request
for i := 0; i < N; i++ {
req := &reqs[i];
req.a = i;
req.b = i + N;
req.replyc = make(chan int);
adder <- req;
req := &reqs[i]
req.a = i
req.b = i + N
req.replyc = make(chan int)
adder <- req
}
for i := N-1; i >= 0; i-- { // doesn't matter what order
if <-reqs[i].replyc != N + 2*i {
fmt.Println("fail at", i);
fmt.Println("fail at", i)
}
}
quit <- true;
quit <- true
}
......@@ -17,7 +17,7 @@ func generate(ch chan int) {
// removing those divisible by 'prime'.
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'.
}
......@@ -26,13 +26,13 @@ func filter(in, out chan int, prime int) {
// The prime sieve: Daisy-chain filter processes together.
func main() {
ch := make(chan int); // Create a new channel.
go generate(ch); // Start generate() as a goroutine.
ch := make(chan int) // Create a new channel.
go generate(ch) // Start generate() as a goroutine.
for {
prime := <-ch;
fmt.Println(prime);
ch1 := make(chan int);
go filter(ch, ch1, prime);
prime := <-ch
fmt.Println(prime)
ch1 := make(chan int)
go filter(ch, ch1, prime)
ch = ch1
}
}
......@@ -8,44 +8,44 @@ import "fmt"
// Send the sequence 2, 3, 4, ... to returned channel
func generate() chan int {
ch := make(chan int);
ch := make(chan int)
go func(){
for i := 2; ; i++ {
ch <- i
}
}();
return ch;
}()
return ch
}
// Filter out input values divisible by 'prime', send rest to returned channel
func filter(in chan int, prime int) chan int {
out := make(chan int);
out := make(chan int)
go func() {
for {
if i := <-in; i % prime != 0 {
out <- i
}
}
}();
return out;
}()
return out
}
func sieve() chan int {
out := make(chan int);
out := make(chan int)
go func() {
ch := generate();
ch := generate()
for {
prime := <-ch;
out <- prime;
ch = filter(ch, prime);
prime := <-ch
out <- prime
ch = filter(ch, prime)
}
}();
return out;
}()
return out
}
func main() {
primes := sieve();
primes := sieve()
for {
fmt.Println(<-primes);
fmt.Println(<-primes)
}
}
......@@ -5,59 +5,59 @@
package sort
type Interface interface {
Len() int;
Less(i, j int) bool;
Swap(i, j int);
Len() int
Less(i, j int) bool
Swap(i, j int)
}
func Sort(data Interface) {
for i := 1; i < data.Len(); i++ {
for j := i; j > 0 && data.Less(j, j-1); j-- {
data.Swap(j, j-1);
data.Swap(j, j-1)
}
}
}
func IsSorted(data Interface) bool {
n := data.Len();
n := data.Len()
for i := n - 1; i > 0; i-- {
if data.Less(i, i - 1) {
return false;
return false
}
}
return true;
return true
}
// Convenience types for common cases
type IntArray []int
func (p IntArray) Len() int { return len(p); }
func (p IntArray) Less(i, j int) bool { return p[i] < p[j]; }
func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i]; }
func (p IntArray) Len() int { return len(p) }
func (p IntArray) Less(i, j int) bool { return p[i] < p[j] }
func (p IntArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type FloatArray []float
func (p FloatArray) Len() int { return len(p); }
func (p FloatArray) Less(i, j int) bool { return p[i] < p[j]; }
func (p FloatArray) Swap(i, j int) { p[i], p[j] = p[j], p[i]; }
func (p FloatArray) Len() int { return len(p) }
func (p FloatArray) Less(i, j int) bool { return p[i] < p[j] }
func (p FloatArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type StringArray []string
func (p StringArray) Len() int { return len(p); }
func (p StringArray) Less(i, j int) bool { return p[i] < p[j]; }
func (p StringArray) Swap(i, j int) { p[i], p[j] = p[j], p[i]; }
func (p StringArray) Len() int { return len(p) }
func (p StringArray) Less(i, j int) bool { return p[i] < p[j] }
func (p StringArray) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// Convenience wrappers for common cases
func SortInts(a []int) { Sort(IntArray(a)); }
func SortFloats(a []float) { Sort(FloatArray(a)); }
func SortStrings(a []string) { Sort(StringArray(a)); }
func SortInts(a []int) { Sort(IntArray(a)) }
func SortFloats(a []float) { Sort(FloatArray(a)) }
func SortStrings(a []string) { Sort(StringArray(a)) }
func IntsAreSorted(a []int) bool { return IsSorted(IntArray(a)); }
func FloatsAreSorted(a []float) bool { return IsSorted(FloatArray(a)); }
func StringsAreSorted(a []string) bool { return IsSorted(StringArray(a)); }
func IntsAreSorted(a []int) bool { return IsSorted(IntArray(a)) }
func FloatsAreSorted(a []float) bool { return IsSorted(FloatArray(a)) }
func StringsAreSorted(a []string) bool { return IsSorted(StringArray(a)) }
......@@ -5,53 +5,53 @@
package main
import (
"fmt";
"sort";
"fmt"
"sort"
)
func ints() {
data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586};
a := sort.IntArray(data);
sort.Sort(a);
data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
a := sort.IntArray(data)
sort.Sort(a)
if !sort.IsSorted(a) {
panic()
}
}
func strings() {
data := []string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"};
a := sort.StringArray(data);
sort.Sort(a);
data := []string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"}
a := sort.StringArray(data)
sort.Sort(a)
if !sort.IsSorted(a) {
panic()
}
}
type day struct {
num int;
shortName string;
longName string;
num int
shortName string
longName string
}
type dayArray struct {
data []*day;
data []*day
}
func (p *dayArray) Len() int { return len(p.data); }
func (p *dayArray) Less(i, j int) bool { return p.data[i].num < p.data[j].num; }
func (p *dayArray) Swap(i, j int) { p.data[i], p.data[j] = p.data[j], p.data[i]; }
func (p *dayArray) Len() int { return len(p.data) }
func (p *dayArray) Less(i, j int) bool { return p.data[i].num < p.data[j].num }
func (p *dayArray) Swap(i, j int) { p.data[i], p.data[j] = p.data[j], p.data[i] }
func days() {
Sunday := day{ 0, "SUN", "Sunday" };
Monday := day{ 1, "MON", "Monday" };
Tuesday := day{ 2, "TUE", "Tuesday" };
Wednesday := day{ 3, "WED", "Wednesday" };
Thursday := day{ 4, "THU", "Thursday" };
Friday := day{ 5, "FRI", "Friday" };
Saturday := day{ 6, "SAT", "Saturday" };
data := []*day{&Tuesday, &Thursday, &Wednesday, &Sunday, &Monday, &Friday, &Saturday};
a := dayArray{data};
sort.Sort(&a);
Sunday := day{ 0, "SUN", "Sunday" }
Monday := day{ 1, "MON", "Monday" }
Tuesday := day{ 2, "TUE", "Tuesday" }
Wednesday := day{ 3, "WED", "Wednesday" }
Thursday := day{ 4, "THU", "Thursday" }
Friday := day{ 5, "FRI", "Friday" }
Saturday := day{ 6, "SAT", "Saturday" }
data := []*day{&Tuesday, &Thursday, &Wednesday, &Sunday, &Monday, &Friday, &Saturday}
a := dayArray{data}
sort.Sort(&a)
if !sort.IsSorted(&a) {
panic()
}
......@@ -63,7 +63,7 @@ func days() {
func main() {
ints();
strings();
days();
ints()
strings()
days()
}
......@@ -8,9 +8,9 @@ import "fmt"
import "os"
func main() {
s := "hello";
s := "hello"
if s[1] != 'e' { os.Exit(1) }
s = "good bye";
var p *string = &s;
*p = "ciao";
s = "good bye"
var p *string = &s
*p = "ciao"
}
......@@ -7,7 +7,7 @@ package main
import "fmt"
func sum(a []int) int { // returns an int
s := 0;
s := 0
for i := 0; i < len(a); i++ {
s += a[i]
}
......@@ -16,6 +16,6 @@ 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
fmt.Print(s, "\n");
s := sum(&[3]int{1,2,3}) // a slice of the array is passed to sum
fmt.Print(s, "\n")
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment