Commit 293c8f8c authored by Rob Pike's avatar Rob Pike

casify tutorial examples

will bring document in line in a later CL, which may include revisiting some of the names

R=rsc
DELTA=58  (0 added, 0 deleted, 58 changed)
OCL=22906
CL=22908
parent c1e7e270
...@@ -22,20 +22,20 @@ func rot13(b byte) byte { ...@@ -22,20 +22,20 @@ func rot13(b byte) byte {
return b return b
} }
type Reader interface { type reader interface {
Read(b []byte) (ret int, err *os.Error); Read(b []byte) (ret int, err *os.Error);
String() string; String() string;
} }
type Rot13 struct { type rotate13 struct {
source Reader; source reader;
} }
func NewRot13(source Reader) *Rot13 { func newRotate13(source reader) *rotate13 {
return &Rot13{source} return &rotate13{source}
} }
func (r13 *Rot13) Read(b []byte) (ret int, err *os.Error) { 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++ { for i := 0; i < r; i++ {
b[i] = rot13(b[i]) b[i] = rot13(b[i])
...@@ -43,17 +43,17 @@ func (r13 *Rot13) Read(b []byte) (ret int, err *os.Error) { ...@@ -43,17 +43,17 @@ func (r13 *Rot13) Read(b []byte) (ret int, err *os.Error) {
return r, e return r, e
} }
func (r13 *Rot13) String() string { func (r13 *rotate13) String() string {
return r13.source.String() return r13.source.String()
} }
// end of Rot13 implementation // end of Rotate13 implementation
func cat(r Reader) { func cat(r reader) {
const NBUF = 512; const NBUF = 512;
var buf [NBUF]byte; var buf [NBUF]byte;
if *rot13_flag { if *rot13_flag {
r = NewRot13(r) r = newRotate13(r)
} }
for { for {
switch nr, er := r.Read(buf); { switch nr, er := r.Read(buf); {
......
...@@ -12,8 +12,8 @@ import ( ...@@ -12,8 +12,8 @@ import (
var n_flag = flag.Bool("n", false, "don't print final newline") var n_flag = flag.Bool("n", false, "don't print final newline")
const ( const (
Space = " "; kSpace = " ";
Newline = "\n"; kNewline = "\n";
) )
func main() { func main() {
...@@ -21,12 +21,12 @@ func main() { ...@@ -21,12 +21,12 @@ func main() {
var s string = ""; var s string = "";
for i := 0; i < flag.NArg(); i++ { for i := 0; i < flag.NArg(); i++ {
if i > 0 { if i > 0 {
s += Space s += kSpace
} }
s += flag.Arg(i) s += flag.Arg(i)
} }
if !*n_flag { if !*n_flag {
s += Newline s += kNewline
} }
os.Stdout.WriteString(s); os.Stdout.WriteString(s);
} }
...@@ -14,7 +14,7 @@ export type FD struct { ...@@ -14,7 +14,7 @@ export type FD struct {
name string; // file name at Open time name string; // file name at Open time
} }
func NewFD(fd int64, name string) *FD { func newFD(fd int64, name string) *FD {
if fd < 0 { if fd < 0 {
return nil return nil
} }
...@@ -22,14 +22,14 @@ func NewFD(fd int64, name string) *FD { ...@@ -22,14 +22,14 @@ func NewFD(fd int64, name string) *FD {
} }
export var ( export var (
Stdin = NewFD(0, "/dev/stdin"); Stdin = newFD(0, "/dev/stdin");
Stdout = NewFD(1, "/dev/stdout"); Stdout = newFD(1, "/dev/stdout");
Stderr = NewFD(2, "/dev/stderr"); Stderr = newFD(2, "/dev/stderr");
) )
export func Open(name string, mode int64, perm int64) (fd *FD, err *os.Error) { export func Open(name string, mode int64, perm int64) (fd *FD, err *os.Error) {
r, e := syscall.open(name, mode, perm); r, e := syscall.open(name, mode, perm);
return NewFD(r, name), os.ErrnoToError(e) return newFD(r, name), os.ErrnoToError(e)
} }
func (fd *FD) Close() *os.Error { func (fd *FD) Close() *os.Error {
......
...@@ -6,13 +6,13 @@ package main ...@@ -6,13 +6,13 @@ package main
import "fmt" import "fmt"
type T struct { a int; b string } type testType struct { a int; b string }
func (t *T) String() string { func (t *testType) String() string {
return fmt.Sprint(t.a) + " " + t.b return fmt.Sprint(t.a) + " " + t.b
} }
func main() { func main() {
t := &T{77, "Sunset Strip"}; t := &testType{77, "Sunset Strip"};
fmt.Println(t) fmt.Println(t)
} }
...@@ -4,40 +4,40 @@ ...@@ -4,40 +4,40 @@
package main package main
type Request struct { type request struct {
a, b int; a, b int;
replyc chan int; replyc chan int;
} }
type BinOp (a, b int) int; type binOp (a, b int) int;
func Run(op *BinOp, request *Request) { func run(op *binOp, request *request) {
result := op(request.a, request.b); result := op(request.a, request.b);
request.replyc <- result; request.replyc <- result;
} }
func Server(op *BinOp, service chan *Request, quit chan bool) { func server(op *binOp, service chan *request, quit chan bool) {
for { for {
select { select {
case request := <-service: case req := <-service:
go Run(op, request); // don't wait for it go run(op, req); // don't wait for it
case <-quit: case <-quit:
return; return;
} }
} }
} }
func StartServer(op *BinOp) (service chan *Request, quit chan bool) { func startServer(op *binOp) (service chan *request, quit chan bool) {
service = make(chan *Request); service = make(chan *request);
quit = make(chan bool); quit = make(chan bool);
go Server(op, service, quit); go server(op, service, quit);
return service, quit; return service, quit;
} }
func main() { func main() {
adder, quit := StartServer(func(a, b int) int { return a + b }); adder, quit := startServer(func(a, b int) int { return a + b });
const N = 100; const N = 100;
var reqs [N]Request; var reqs [N]request;
for i := 0; i < N; i++ { for i := 0; i < N; i++ {
req := &reqs[i]; req := &reqs[i];
req.a = i; req.a = i;
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
package main package main
// Send the sequence 2, 3, 4, ... to channel 'ch'. // Send the sequence 2, 3, 4, ... to channel 'ch'.
func Generate(ch chan int) { func generate(ch chan int) {
for i := 2; ; i++ { for i := 2; ; i++ {
ch <- i // Send 'i' to channel 'ch'. ch <- i // Send 'i' to channel 'ch'.
} }
...@@ -13,7 +13,7 @@ func Generate(ch chan int) { ...@@ -13,7 +13,7 @@ func Generate(ch chan int) {
// Copy the values from channel 'in' to channel 'out', // Copy the values from channel 'in' to channel 'out',
// removing those divisible by 'prime'. // removing those divisible by 'prime'.
func Filter(in, out chan int, prime int) { func filter(in, out chan int, prime int) {
for { 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 { if i % prime != 0 {
...@@ -25,12 +25,12 @@ func Filter(in, out chan int, prime int) { ...@@ -25,12 +25,12 @@ func Filter(in, out chan int, prime int) {
// The prime sieve: Daisy-chain Filter processes together. // The prime sieve: Daisy-chain Filter processes together.
func main() { func main() {
ch := make(chan int); // Create a new channel. ch := make(chan int); // Create a new channel.
go Generate(ch); // Start Generate() as a goroutine. go generate(ch); // Start Generate() as a goroutine.
for { for {
prime := <-ch; prime := <-ch;
print(prime, "\n"); print(prime, "\n");
ch1 := make(chan int); ch1 := make(chan int);
go Filter(ch, ch1, prime); go filter(ch, ch1, prime);
ch = ch1 ch = ch1
} }
} }
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
package main package main
// Send the sequence 2, 3, 4, ... to returned channel // Send the sequence 2, 3, 4, ... to returned channel
func Generate() chan int { func generate() chan int {
ch := make(chan int); ch := make(chan int);
go func(ch chan int){ go func(ch chan int){
for i := 2; ; i++ { for i := 2; ; i++ {
...@@ -16,7 +16,7 @@ func Generate() chan int { ...@@ -16,7 +16,7 @@ func Generate() chan int {
} }
// Filter out input values divisible by 'prime', send rest to returned channel // Filter out input values divisible by 'prime', send rest to returned channel
func Filter(in chan int, prime int) chan int { func filter(in chan int, prime int) chan int {
out := make(chan int); out := make(chan int);
go func(in chan int, out chan int, prime int) { go func(in chan int, out chan int, prime int) {
for { for {
...@@ -28,21 +28,21 @@ func Filter(in chan int, prime int) chan int { ...@@ -28,21 +28,21 @@ func Filter(in chan int, prime int) chan int {
return out; return out;
} }
func Sieve() chan int { func sieve() chan int {
out := make(chan int); out := make(chan int);
go func(out chan int) { go func(out chan int) {
ch := Generate(); ch := generate();
for { for {
prime := <-ch; prime := <-ch;
out <- prime; out <- prime;
ch = Filter(ch, prime); ch = filter(ch, prime);
} }
}(out); }(out);
return out; return out;
} }
func main() { func main() {
primes := Sieve(); primes := sieve();
for { for {
print(<-primes, "\n"); print(<-primes, "\n");
} }
......
...@@ -24,30 +24,30 @@ func strings() { ...@@ -24,30 +24,30 @@ func strings() {
} }
} }
type Day struct { type day struct {
num int; num int;
short_name string; short_name string;
long_name string; long_name string;
} }
type DayArray struct { type dayArray struct {
data []*Day; data []*day;
} }
func (p *DayArray) Len() int { return len(p.data); } 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) 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) Swap(i, j int) { p.data[i], p.data[j] = p.data[j], p.data[i]; }
func days() { func days() {
Sunday := Day{ 0, "SUN", "Sunday" }; Sunday := day{ 0, "SUN", "Sunday" };
Monday := Day{ 1, "MON", "Monday" }; Monday := day{ 1, "MON", "Monday" };
Tuesday := Day{ 2, "TUE", "Tuesday" }; Tuesday := day{ 2, "TUE", "Tuesday" };
Wednesday := Day{ 3, "WED", "Wednesday" }; Wednesday := day{ 3, "WED", "Wednesday" };
Thursday := Day{ 4, "THU", "Thursday" }; Thursday := day{ 4, "THU", "Thursday" };
Friday := Day{ 5, "FRI", "Friday" }; Friday := day{ 5, "FRI", "Friday" };
Saturday := Day{ 6, "SAT", "Saturday" }; Saturday := day{ 6, "SAT", "Saturday" };
data := []*Day{&Tuesday, &Thursday, &Sunday, &Monday, &Friday}; data := []*day{&Tuesday, &Thursday, &Sunday, &Monday, &Friday};
a := DayArray{data}; a := dayArray{data};
sort.Sort(&a); sort.Sort(&a);
if !sort.IsSorted(&a) { if !sort.IsSorted(&a) {
panic() panic()
......
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