diff options
Diffstat (limited to 'doc/progs')
-rw-r--r-- | doc/progs/cat.go | 47 | ||||
-rw-r--r-- | doc/progs/cat_rot13.go | 90 | ||||
-rw-r--r-- | doc/progs/echo.go | 32 | ||||
-rw-r--r-- | doc/progs/file.go | 89 | ||||
-rw-r--r-- | doc/progs/file_windows.go | 89 | ||||
-rw-r--r-- | doc/progs/helloworld.go | 11 | ||||
-rw-r--r-- | doc/progs/helloworld3.go | 21 | ||||
-rw-r--r-- | doc/progs/print.go | 23 | ||||
-rw-r--r-- | doc/progs/print_string.go | 21 | ||||
-rwxr-xr-x | doc/progs/run | 85 | ||||
-rw-r--r-- | doc/progs/server.go | 51 | ||||
-rw-r--r-- | doc/progs/server1.go | 56 | ||||
-rw-r--r-- | doc/progs/sieve.go | 38 | ||||
-rw-r--r-- | doc/progs/sieve1.go | 51 | ||||
-rw-r--r-- | doc/progs/sort.go | 59 | ||||
-rw-r--r-- | doc/progs/sortmain.go | 69 | ||||
-rw-r--r-- | doc/progs/strings.go | 17 | ||||
-rw-r--r-- | doc/progs/sum.go | 20 |
18 files changed, 869 insertions, 0 deletions
diff --git a/doc/progs/cat.go b/doc/progs/cat.go new file mode 100644 index 000000000..9f0b8d4a3 --- /dev/null +++ b/doc/progs/cat.go @@ -0,0 +1,47 @@ +// 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 ( + "./file" + "flag" + "fmt" + "os" +) + +func cat(f *file.File) { + 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) + case nr == 0: // EOF + 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()) + os.Exit(1) + } + } + } +} + +func main() { + flag.Parse() // Scans the arg list and sets up flags + if flag.NArg() == 0 { + cat(file.Stdin) + } + for i := 0; i < flag.NArg(); i++ { + f, err := file.Open(flag.Arg(i)) + if f == nil { + fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", flag.Arg(i), err) + os.Exit(1) + } + cat(f) + f.Close() + } +} diff --git a/doc/progs/cat_rot13.go b/doc/progs/cat_rot13.go new file mode 100644 index 000000000..0eefe7cfc --- /dev/null +++ b/doc/progs/cat_rot13.go @@ -0,0 +1,90 @@ +// 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 ( + "./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 + } + if 'A' <= b && b <= 'Z' { + b = 'A' + ((b-'A')+13)%26 + } + return b +} + +type reader interface { + Read(b []byte) (ret int, err os.Error) + String() string +} + +type rotate13 struct { + source reader +} + +func newRotate13(source reader) *rotate13 { + return &rotate13{source} +} + +func (r13 *rotate13) Read(b []byte) (ret int, err os.Error) { + r, e := r13.source.Read(b) + for i := 0; i < r; i++ { + b[i] = rot13(b[i]) + } + return r, e +} + +func (r13 *rotate13) String() string { + return r13.source.String() +} +// end of rotate13 implementation + +func cat(r reader) { + const NBUF = 512 + var buf [NBUF]byte + + if *rot13Flag { + r = newRotate13(r) + } + 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) + case nr == 0: // EOF + return + case nr > 0: + 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()) + os.Exit(1) + } + } + } +} + +func main() { + flag.Parse() // Scans the arg list and sets up flags + if flag.NArg() == 0 { + cat(file.Stdin) + } + for i := 0; i < flag.NArg(); i++ { + f, err := file.Open(flag.Arg(i)) + if f == nil { + fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", flag.Arg(i), err) + os.Exit(1) + } + cat(f) + f.Close() + } +} diff --git a/doc/progs/echo.go b/doc/progs/echo.go new file mode 100644 index 000000000..3260edd74 --- /dev/null +++ b/doc/progs/echo.go @@ -0,0 +1,32 @@ +// 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" + "flag" // command line option parser +) + +var omitNewline = flag.Bool("n", false, "don't print final newline") + +const ( + Space = " " + Newline = "\n" +) + +func main() { + 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) + } + if !*omitNewline { + s += Newline + } + os.Stdout.WriteString(s) +} diff --git a/doc/progs/file.go b/doc/progs/file.go new file mode 100644 index 000000000..2875ce73a --- /dev/null +++ b/doc/progs/file.go @@ -0,0 +1,89 @@ +// 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 file + +import ( + "os" + "syscall" +) + +type File struct { + fd int // file descriptor number + name string // file name at Open time +} + +func newFile(fd int, name string) *File { + if fd < 0 { + return nil + } + return &File{fd, name} +} + +var ( + Stdin = newFile(syscall.Stdin, "/dev/stdin") + Stdout = newFile(syscall.Stdout, "/dev/stdout") + Stderr = newFile(syscall.Stderr, "/dev/stderr") +) + +func OpenFile(name string, mode int, perm uint32) (file *File, err os.Error) { + r, e := syscall.Open(name, mode, perm) + if e != 0 { + err = os.Errno(e) + } + return newFile(r, name), err +} + +const ( + O_RDONLY = syscall.O_RDONLY + O_RDWR = syscall.O_RDWR + O_CREATE = syscall.O_CREAT + O_TRUNC = syscall.O_TRUNC +) + +func Open(name string) (file *File, err os.Error) { + return OpenFile(name, O_RDONLY, 0) +} + +func Create(name string) (file *File, err os.Error) { + return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666) +} + +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 + if e != 0 { + return os.Errno(e) + } + return nil +} + +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) + if e != 0 { + err = os.Errno(e) + } + return int(r), err +} + +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) + if e != 0 { + err = os.Errno(e) + } + return int(r), err +} + +func (file *File) String() string { + return file.name +} diff --git a/doc/progs/file_windows.go b/doc/progs/file_windows.go new file mode 100644 index 000000000..03003a3f7 --- /dev/null +++ b/doc/progs/file_windows.go @@ -0,0 +1,89 @@ +// 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 file + +import ( + "os" + "syscall" +) + +type File struct { + fd syscall.Handle // file descriptor number + name string // file name at Open time +} + +func newFile(fd syscall.Handle, name string) *File { + if fd < 0 { + return nil + } + return &File{fd, name} +} + +var ( + Stdin = newFile(syscall.Stdin, "/dev/stdin") + Stdout = newFile(syscall.Stdout, "/dev/stdout") + Stderr = newFile(syscall.Stderr, "/dev/stderr") +) + +func OpenFile(name string, mode int, perm uint32) (file *File, err os.Error) { + r, e := syscall.Open(name, mode, perm) + if e != 0 { + err = os.Errno(e) + } + return newFile(r, name), err +} + +const ( + O_RDONLY = syscall.O_RDONLY + O_RDWR = syscall.O_RDWR + O_CREATE = syscall.O_CREAT + O_TRUNC = syscall.O_TRUNC +) + +func Open(name string) (file *File, err os.Error) { + return OpenFile(name, O_RDONLY, 0) +} + +func Create(name string) (file *File, err os.Error) { + return OpenFile(name, O_RDWR|O_CREATE|O_TRUNC, 0666) +} + +func (file *File) Close() os.Error { + if file == nil { + return os.EINVAL + } + e := syscall.Close(file.fd) + file.fd = syscall.InvalidHandle // so it can't be closed again + if e != 0 { + return os.Errno(e) + } + return nil +} + +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) + if e != 0 { + err = os.Errno(e) + } + return int(r), err +} + +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) + if e != 0 { + err = os.Errno(e) + } + return int(r), err +} + +func (file *File) String() string { + return file.name +} diff --git a/doc/progs/helloworld.go b/doc/progs/helloworld.go new file mode 100644 index 000000000..8185038d9 --- /dev/null +++ b/doc/progs/helloworld.go @@ -0,0 +1,11 @@ +// 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 "fmt" // Package implementing formatted I/O. + +func main() { + fmt.Printf("Hello, world; or Καλημέρα κόσμε; or こんにちは 世界\n") +} diff --git a/doc/progs/helloworld3.go b/doc/progs/helloworld3.go new file mode 100644 index 000000000..2011513b7 --- /dev/null +++ b/doc/progs/helloworld3.go @@ -0,0 +1,21 @@ +// 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 ( + "./file" + "fmt" + "os" +) + +func main() { + hello := []byte("hello, world\n") + file.Stdout.Write(hello) + f, err := file.Open("/does/not/exist") + if f == nil { + fmt.Printf("can't open file; err=%s\n", err.String()) + os.Exit(1) + } +} diff --git a/doc/progs/print.go b/doc/progs/print.go new file mode 100644 index 000000000..8f44ba8c6 --- /dev/null +++ b/doc/progs/print.go @@ -0,0 +1,23 @@ +// 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() { + 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) +} diff --git a/doc/progs/print_string.go b/doc/progs/print_string.go new file mode 100644 index 000000000..46ab1d91a --- /dev/null +++ b/doc/progs/print_string.go @@ -0,0 +1,21 @@ +// 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" + +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"} + fmt.Println(t) +} diff --git a/doc/progs/run b/doc/progs/run new file mode 100755 index 000000000..81781c9d2 --- /dev/null +++ b/doc/progs/run @@ -0,0 +1,85 @@ +#!/usr/bin/env 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. + +set -e + +eval $(gomake --no-print-directory -f ../../src/Make.inc go-env) + +if [ -z "$O" ]; then + echo 'missing $O - maybe no Make.$GOARCH?' 1>&2 + exit 1 +fi + +rm -f *.$O + +if [ "$GOOS" = "windows" ];then + $GC -o file.8 file_windows.go +else + $GC file.go +fi + +for i in \ + helloworld.go \ + helloworld3.go \ + echo.go \ + cat.go \ + cat_rot13.go \ + sum.go \ + sort.go \ + sortmain.go \ + print.go \ + print_string.go \ + sieve.go \ + sieve1.go \ + server1.go \ + strings.go \ +; do + $GC $i +done + +function testit { + $LD $1.$O + x=$(echo $(./$O.out $2 2>&1)) # extra echo canonicalizes + if [ "$x" != "$3" ] + then + echo $1 failed: '"'$x'"' is not '"'$3'"' + fi +} + +function testitpipe { + $LD $1.$O + x=$(echo $(./$O.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 helloworld3 "" "hello, world can't open file; err=no such file or directory" +testit echo "hello, world" "hello, world" +testit sum "" "6" +testit strings "" "" + +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 Wednesday Thursday Friday Saturday" + +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 print_string "" "77 Sunset Strip" + +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, just compile it +$GC server.go +testit server1 "" "" + +rm -f $O.out *.$O diff --git a/doc/progs/server.go b/doc/progs/server.go new file mode 100644 index 000000000..b498b53a6 --- /dev/null +++ b/doc/progs/server.go @@ -0,0 +1,51 @@ +// 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" + +type request struct { + 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 +} + +func server(op binOp, service chan *request) { + for { + 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 +} + +func main() { + 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 + } + 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("done") +} diff --git a/doc/progs/server1.go b/doc/progs/server1.go new file mode 100644 index 000000000..a4093924b --- /dev/null +++ b/doc/progs/server1.go @@ -0,0 +1,56 @@ +// 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" + +type request struct { + 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 +} + +func server(op binOp, service chan *request, quit chan bool) { + for { + select { + case req := <-service: + go run(op, req) // don't wait for it + case <-quit: + 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 +} + +func main() { + 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 + } + for i := N - 1; i >= 0; i-- { // doesn't matter what order + if <-reqs[i].replyc != N+2*i { + fmt.Println("fail at", i) + } + } + quit <- true +} diff --git a/doc/progs/sieve.go b/doc/progs/sieve.go new file mode 100644 index 000000000..b31530981 --- /dev/null +++ b/doc/progs/sieve.go @@ -0,0 +1,38 @@ +// 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" + +// Send the sequence 2, 3, 4, ... 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, out chan int, prime int) { + for { + i := <-in // Receive value of new variable 'i' from 'in'. + if i%prime != 0 { + out <- i // Send 'i' to channel 'out'. + } + } +} + +// 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. + for i := 0; i < 100; i++ { // Print the first hundred primes. + prime := <-ch + 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 new file mode 100644 index 000000000..e1411a334 --- /dev/null +++ b/doc/progs/sieve1.go @@ -0,0 +1,51 @@ +// 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" + +// Send the sequence 2, 3, 4, ... to returned channel +func generate() chan int { + ch := make(chan int) + go func() { + for i := 2; ; i++ { + ch <- i + } + }() + 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) + go func() { + for { + if i := <-in; i%prime != 0 { + out <- i + } + } + }() + return out +} + +func sieve() chan int { + out := make(chan int) + go func() { + ch := generate() + for { + prime := <-ch + out <- prime + ch = filter(ch, prime) + } + }() + return out +} + +func main() { + primes := sieve() + for i := 0; i < 100; i++ { // Print the first hundred primes. + fmt.Println(<-primes) + } +} diff --git a/doc/progs/sort.go b/doc/progs/sort.go new file mode 100644 index 000000000..894693f0d --- /dev/null +++ b/doc/progs/sort.go @@ -0,0 +1,59 @@ +// 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 sort + +type Interface interface { + 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) + } + } +} + +func IsSorted(data Interface) bool { + n := data.Len() + for i := n - 1; i > 0; i-- { + if data.Less(i, i-1) { + return false + } + } + return true +} + +// Convenience types for common cases + +type IntSlice []int + +func (p IntSlice) Len() int { return len(p) } +func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] } +func (p IntSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } + +type Float64Slice []float64 + +func (p Float64Slice) Len() int { return len(p) } +func (p Float64Slice) Less(i, j int) bool { return p[i] < p[j] } +func (p Float64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } + +type StringSlice []string + +func (p StringSlice) Len() int { return len(p) } +func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] } +func (p StringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } + +// Convenience wrappers for common cases + +func SortInts(a []int) { Sort(IntSlice(a)) } +func SortFloat64s(a []float64) { Sort(Float64Slice(a)) } +func SortStrings(a []string) { Sort(StringSlice(a)) } + +func IntsAreSorted(a []int) bool { return IsSorted(IntSlice(a)) } +func Float64sAreSorted(a []float64) bool { return IsSorted(Float64Slice(a)) } +func StringsAreSorted(a []string) bool { return IsSorted(StringSlice(a)) } diff --git a/doc/progs/sortmain.go b/doc/progs/sortmain.go new file mode 100644 index 000000000..c1babb01f --- /dev/null +++ b/doc/progs/sortmain.go @@ -0,0 +1,69 @@ +// 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" + "./sort" +) + +func ints() { + data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586} + a := sort.IntSlice(data) + sort.Sort(a) + if !sort.IsSorted(a) { + panic("fail") + } +} + +func strings() { + data := []string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"} + a := sort.StringSlice(data) + sort.Sort(a) + if !sort.IsSorted(a) { + panic("fail") + } +} + +type day struct { + num int + shortName string + longName string +} + +type dayArray struct { + 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 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) + if !sort.IsSorted(&a) { + panic("fail") + } + for _, d := range data { + fmt.Printf("%s ", d.longName) + } + fmt.Printf("\n") +} + + +func main() { + ints() + strings() + days() +} diff --git a/doc/progs/strings.go b/doc/progs/strings.go new file mode 100644 index 000000000..e6739b385 --- /dev/null +++ b/doc/progs/strings.go @@ -0,0 +1,17 @@ +// 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" + +func main() { + s := "hello" + if s[1] != 'e' { + os.Exit(1) + } + s = "good bye" + var p *string = &s + *p = "ciao" +} diff --git a/doc/progs/sum.go b/doc/progs/sum.go new file mode 100644 index 000000000..e022195ed --- /dev/null +++ b/doc/progs/sum.go @@ -0,0 +1,20 @@ +// 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 sum(a []int) int { // returns an int + s := 0 + for i := 0; i < len(a); i++ { + s += a[i] + } + return s +} + +func main() { + s := sum([3]int{1, 2, 3}[:]) // a slice of the array is passed to sum + fmt.Print(s, "\n") +} |