rendered paste bodypackage main
import (
"bufio";
"fmt";
"os";
)
type Tally struct {
bytes int;
words int;
lines int;
}
func wc(input *bufio.Reader) (tally Tally, err os.Error) {
tally.bytes = 0;
tally.words = 0;
tally.lines = 0;
err = nil;
for {
var c uint8;
c, err = input.ReadByte();
if err != nil {
break;
}
tally.bytes++;
switch {
case c == ' ' || c == '\t':
tally.words++;
case c == '\n':
tally.words++;
tally.lines++;
}
}
return;
}
func main() {
tally, err := wc(bufio.NewReader(os.Stdin));
if err == os.EOF {
fmt.Printf("%v\t%v\t%v\n", tally.bytes, tally.words,
tally.lines);
} else {
fmt.Fprintf(os.Stderr, "%v\n", err);
os.Exit(1);
}
}