Merge branch 'matt2909/patch-1'

This commit is contained in:
Mark McGranaghan 2013-12-19 11:28:21 -08:00
commit e5ff8baebb

View File

@ -11,7 +11,6 @@ package main
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"io"
"os" "os"
"strings" "strings"
) )
@ -19,38 +18,24 @@ import (
func main() { func main() {
// Wrapping the unbuffered `os.Stdin` with a buffered // Wrapping the unbuffered `os.Stdin` with a buffered
// reader gives us a convenient `ReadString` method // scanner gives us a convenient `Scan` method that
// that we'll use to read input line-by-line. // advances the scanner to the next token; which is
rdr := bufio.NewReader(os.Stdin) // the next line in the default scanner.
out := os.Stdout scanner := bufio.NewScanner(os.Stdin)
// `ReadString` returns the next string from the for scanner.Scan() {
// input up to the given separator byte. We give the // `Text` returns the current token, here the next line,
// newline byte `'\n'` as our separator so we'll get // from the input.
// successive input lines. ucl := strings.ToUpper(scanner.Text())
for {
switch line, err := rdr.ReadString('\n'); err {
// If the read succeeded (the read `err` is nil), // Write out the uppercased line.
// write out out the uppercased line. Check for an fmt.Println(ucl)
// error on this write as we do on the read.
case nil:
ucl := strings.ToUpper(line)
if _, err = out.WriteString(ucl); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
} }
// The `EOF` error is expected when we reach the // Check for errors during `Scan`. End of file is
// end of input, so exit gracefully in that case. // expected and not reported by `Scan` as an error.
case io.EOF: if err := scanner.Err(); err != nil {
os.Exit(0)
// Otherwise there's a problem; print the
// error and exit with non-zero status.
default:
fmt.Fprintln(os.Stderr, "error:", err) fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1) os.Exit(1)
} }
}
} }