1
0
mirror of https://github.com/golang/go synced 2024-11-22 00:44:39 -07:00

Automated g4 rollback of changelist 35383.

*** Reason for rollback ***

roll back the changes to the tutorial programs (only) since they
break the automated processing used to create the tutorial.

*** Original change description ***

apply gofmt to the LGTM-marked files from 34501
that have not changed since I applied gofmt.

R=rsc
DELTA=139  (0 added, 44 deleted, 95 changed)
OCL=35670
CL=35670
This commit is contained in:
Rob Pike 2009-10-13 12:37:04 -07:00
parent 3139b2031c
commit 7839521335
14 changed files with 95 additions and 139 deletions

View File

@ -19,7 +19,7 @@ func cat(f *file.File) {
case nr < 0: case nr < 0:
fmt.Fprintf(os.Stderr, "error reading from %s: %s\n", f.String(), er.String()); fmt.Fprintf(os.Stderr, "error reading from %s: %s\n", f.String(), er.String());
os.Exit(1); os.Exit(1);
case nr == 0: // EOF case nr == 0: // EOF
return; return;
case nr > 0: case nr > 0:
if nw, ew := file.Stdout.Write(buf[0:nr]); nw != nr { if nw, ew := file.Stdout.Write(buf[0:nr]); nw != nr {
@ -30,7 +30,7 @@ func cat(f *file.File) {
} }
func main() { 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 { if flag.NArg() == 0 {
cat(file.Stdin); cat(file.Stdin);
} }

View File

@ -15,12 +15,12 @@ var rot13_flag = flag.Bool("rot13", false, "rot13 the input")
func rot13(b byte) byte { func rot13(b byte) byte {
if 'a' <= b && b <= 'z' { if 'a' <= b && b <= 'z' {
b = 'a' + ((b-'a')+13)%26; b = 'a' + ((b - 'a') + 13) % 26;
} }
if 'A' <= b && b <= 'Z' { if 'A' <= b && b <= 'Z' {
b = 'A' + ((b-'A')+13)%26; b = 'A' + ((b - 'A') + 13) % 26
} }
return b; return b
} }
type reader interface { type reader interface {
@ -29,23 +29,23 @@ type reader interface {
} }
type rotate13 struct { type rotate13 struct {
source reader; source reader;
} }
func newRotate13(source reader) *rotate13 { func newRotate13(source reader) *rotate13 {
return &rotate13{source}; return &rotate13{source}
} }
func (r13 *rotate13) 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])
} }
return r, e; return r, e
} }
func (r13 *rotate13) String() string { func (r13 *rotate13) String() string {
return r13.source.String(); return r13.source.String()
} }
// end of rotate13 implementation // end of rotate13 implementation
@ -54,14 +54,14 @@ func cat(r reader) {
var buf [NBUF]byte; var buf [NBUF]byte;
if *rot13_flag { if *rot13_flag {
r = newRotate13(r); r = newRotate13(r)
} }
for { for {
switch nr, er := r.Read(&buf); { switch nr, er := r.Read(&buf); {
case nr < 0: case nr < 0:
fmt.Fprintf(os.Stderr, "error reading from %s: %s\n", r.String(), er.String()); fmt.Fprintf(os.Stderr, "error reading from %s: %s\n", r.String(), er.String());
os.Exit(1); os.Exit(1);
case nr == 0: // EOF case nr == 0: // EOF
return; return;
case nr > 0: case nr > 0:
nw, ew := file.Stdout.Write(buf[0:nr]); nw, ew := file.Stdout.Write(buf[0:nr]);
@ -73,7 +73,7 @@ func cat(r reader) {
} }
func main() { 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 { if flag.NArg() == 0 {
cat(file.Stdin); cat(file.Stdin);
} }

View File

@ -12,21 +12,21 @@ 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 (
kSpace = " "; kSpace = " ";
kNewline = "\n"; kNewline = "\n";
) )
func main() { func main() {
flag.Parse(); // Scans the arg list and sets up flags flag.Parse(); // Scans the arg list and sets up flags
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 += kSpace; s += kSpace
} }
s += flag.Arg(i); s += flag.Arg(i)
} }
if !*n_flag { if !*n_flag {
s += kNewline; s += kNewline
} }
os.Stdout.WriteString(s); os.Stdout.WriteString(s);
} }

View File

@ -10,21 +10,21 @@ import (
) )
type File struct { type File struct {
fd int; // file descriptor number fd int; // file descriptor number
name string; // file name at Open time name string; // file name at Open time
} }
func newFile(fd int, name string) *File { func newFile(fd int, name string) *File {
if fd < 0 { if fd < 0 {
return nil; return nil
} }
return &File{fd, name}; return &File{fd, name}
} }
var ( var (
Stdin = newFile(0, "/dev/stdin"); Stdin = newFile(0, "/dev/stdin");
Stdout = newFile(1, "/dev/stdout"); Stdout = newFile(1, "/dev/stdout");
Stderr = newFile(2, "/dev/stderr"); Stderr = newFile(2, "/dev/stderr");
) )
func Open(name string, mode int, perm int) (file *File, err os.Error) { func Open(name string, mode int, perm int) (file *File, err os.Error) {
@ -32,43 +32,43 @@ func Open(name string, mode int, perm int) (file *File, err os.Error) {
if e != 0 { if e != 0 {
err = os.Errno(e); err = os.Errno(e);
} }
return newFile(r, name), err; return newFile(r, name), err
} }
func (file *File) Close() os.Error { func (file *File) Close() os.Error {
if file == nil { if file == nil {
return os.EINVAL; return os.EINVAL
} }
e := syscall.Close(file.fd); e := syscall.Close(file.fd);
file.fd = -1; // so it can't be closed again file.fd = -1; // so it can't be closed again
if e != 0 { if e != 0 {
return os.Errno(e); return os.Errno(e);
} }
return nil; return nil
} }
func (file *File) Read(b []byte) (ret int, err os.Error) { func (file *File) Read(b []byte) (ret int, err os.Error) {
if file == nil { if file == nil {
return -1, os.EINVAL; return -1, os.EINVAL
} }
r, e := syscall.Read(file.fd, b); r, e := syscall.Read(file.fd, b);
if e != 0 { if e != 0 {
err = os.Errno(e); err = os.Errno(e);
} }
return int(r), err; return int(r), err
} }
func (file *File) Write(b []byte) (ret int, err os.Error) { func (file *File) Write(b []byte) (ret int, err os.Error) {
if file == nil { if file == nil {
return -1, os.EINVAL; return -1, os.EINVAL
} }
r, e := syscall.Write(file.fd, b); r, e := syscall.Write(file.fd, b);
if e != 0 { if e != 0 {
err = os.Errno(e); err = os.Errno(e);
} }
return int(r), err; return int(r), err
} }
func (file *File) String() string { func (file *File) String() string {
return file.name; return file.name
} }

View File

@ -4,7 +4,7 @@
package main package main
import fmt "fmt" // Package implementing formatted I/O. import fmt "fmt" // Package implementing formatted I/O.
func main() { func main() {
fmt.Printf("Hello, world; or Καλημέρα κόσμε; or こんにちは 世界\n"); fmt.Printf("Hello, world; or Καλημέρα κόσμε; or こんにちは 世界\n");

View File

@ -13,9 +13,9 @@ import (
func main() { func main() {
hello := []byte{'h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '\n'}; hello := []byte{'h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '\n'};
file.Stdout.Write(hello); file.Stdout.Write(hello);
file, err := file.Open("/does/not/exist", 0, 0); file, err := file.Open("/does/not/exist", 0, 0);
if file == nil { if file == nil {
fmt.Printf("can't open file; err=%s\n", err.String()); fmt.Printf("can't open file; err=%s\n", err.String());
os.Exit(1); os.Exit(1);
} }
} }

View File

@ -7,14 +7,11 @@ package main
import "fmt" import "fmt"
func main() { func main() {
var u64 uint64 = 1<<64 - 1; var u64 uint64 = 1<<64-1;
fmt.Printf("%d %d\n", u64, int64(u64)); fmt.Printf("%d %d\n", u64, int64(u64));
// harder stuff // harder stuff
type T struct { type T struct { a int; b string };
a int;
b string;
}
t := T{77, "Sunset Strip"}; t := T{77, "Sunset Strip"};
a := []int{1, 2, 3, 4}; a := []int{1, 2, 3, 4};
fmt.Printf("%v %v %v\n", u64, t, a); fmt.Printf("%v %v %v\n", u64, t, a);

View File

@ -6,16 +6,13 @@ package main
import "fmt" import "fmt"
type testType struct { type testType struct { a int; b string }
a int;
b string;
}
func (t *testType) 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 := &testType{77, "Sunset Strip"}; t := &testType{77, "Sunset Strip"};
fmt.Println(t); fmt.Println(t)
} }

View File

@ -9,7 +9,7 @@ import "fmt"
// 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'.
} }
} }
@ -17,22 +17,22 @@ func generate(ch chan int) {
// 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 {
out <- i; // Send 'i' to channel 'out'. out <- i // Send 'i' to channel 'out'.
} }
} }
} }
// 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;
fmt.Println(prime); fmt.Println(prime);
ch1 := make(chan int); ch1 := make(chan int);
go filter(ch, ch1, prime); go filter(ch, ch1, prime);
ch = ch1; ch = ch1
} }
} }

View File

@ -6,12 +6,12 @@ package main
import "fmt" import "fmt"
// 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() { go func(){
for i := 2; ; i++ { for i := 2; ; i++ {
ch <- i; ch <- i
} }
}(); }();
return ch; return ch;
@ -22,8 +22,8 @@ func filter(in chan int, prime int) chan int {
out := make(chan int); out := make(chan int);
go func() { go func() {
for { for {
if i := <-in; i%prime != 0 { if i := <-in; i % prime != 0 {
out <- i; out <- i
} }
} }
}(); }();

View File

@ -20,8 +20,8 @@ func Sort(data SortInterface) {
func IsSorted(data SortInterface) bool { func IsSorted(data SortInterface) bool {
n := data.Len(); n := data.Len();
for i := n-1; i > 0; i-- { for i := n - 1; i > 0; i-- {
if data.Less(i, i-1) { if data.Less(i, i - 1) {
return false; return false;
} }
} }
@ -32,62 +32,32 @@ func IsSorted(data SortInterface) bool {
type IntArray []int type IntArray []int
func (p IntArray) Len() int { func (p IntArray) Len() int { return len(p); }
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) 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 type FloatArray []float
func (p FloatArray) Len() int { func (p FloatArray) Len() int { return len(p); }
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) 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 type StringArray []string
func (p StringArray) Len() int { func (p StringArray) Len() int { return len(p); }
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) 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 // Convenience wrappers for common cases
func SortInts(a []int) { func SortInts(a []int) { Sort(IntArray(a)); }
Sort(IntArray(a)); func SortFloats(a []float) { Sort(FloatArray(a)); }
} func SortStrings(a []string) { Sort(StringArray(a)); }
func SortFloats(a []float) {
Sort(FloatArray(a));
}
func SortStrings(a []string) {
Sort(StringArray(a));
}
func IntsAreSorted(a []int) bool { func IntsAreSorted(a []int) bool { return IsSorted(IntArray(a)); }
return IsSorted(IntArray(a)); func FloatsAreSorted(a []float) bool { return IsSorted(FloatArray(a)); }
} func StringsAreSorted(a []string) bool { return IsSorted(StringArray(a)); }
func FloatsAreSorted(a []float) bool {
return IsSorted(FloatArray(a));
}
func StringsAreSorted(a []string) bool {
return IsSorted(StringArray(a));
}

View File

@ -14,7 +14,7 @@ func ints() {
a := sort.IntArray(data); a := sort.IntArray(data);
sort.Sort(a); sort.Sort(a);
if !sort.IsSorted(a) { if !sort.IsSorted(a) {
panic(); panic()
} }
} }
@ -23,48 +23,42 @@ func strings() {
a := sort.StringArray(data); a := sort.StringArray(data);
sort.Sort(a); sort.Sort(a);
if !sort.IsSorted(a) { if !sort.IsSorted(a) {
panic(); panic()
} }
} }
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 { func (p *dayArray) Len() int { return len(p.data); }
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) 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() { 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, &Wednesday, &Sunday, &Monday, &Friday, &Saturday}; data := []*day{&Tuesday, &Thursday, &Wednesday, &Sunday, &Monday, &Friday, &Saturday};
a := dayArray{data}; a := dayArray{data};
sort.Sort(&a); sort.Sort(&a);
if !sort.IsSorted(&a) { if !sort.IsSorted(&a) {
panic(); panic()
} }
for _, d := range data { for _, d := range data {
fmt.Printf("%s ", d.long_name); fmt.Printf("%s ", d.long_name)
} }
fmt.Printf("\n"); fmt.Printf("\n")
} }

View File

@ -9,9 +9,7 @@ import "os"
func main() { func main() {
s := "hello"; s := "hello";
if s[1] != 'e' { if s[1] != 'e' { os.Exit(1) }
os.Exit(1);
}
s = "good bye"; s = "good bye";
var p *string = &s; var p *string = &s;
*p = "ciao"; *p = "ciao";

View File

@ -6,16 +6,16 @@ package main
import "fmt" import "fmt"
func sum(a []int) int { // returns an int func sum(a []int) int { // returns an int
s := 0; s := 0;
for i := 0; i < len(a); i++ { for i := 0; i < len(a); i++ {
s += a[i]; s += a[i]
} }
return s; return s
} }
func main() { func main() {
s := sum(&[3]int{1, 2, 3}); // a slice of the array is passed to sum s := sum(&[3]int{1,2,3}); // a slice of the array is passed to sum
fmt.Print(s, "\n"); fmt.Print(s, "\n");
} }