From 6b2aa07fa532dbc9b1ead52149c780c68f06e890 Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Tue, 16 Oct 2012 10:22:55 -0700 Subject: [PATCH] import suggestion from ward --- examples/line-filters/line-filters.go | 58 +++++++++++++-------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/examples/line-filters/line-filters.go b/examples/line-filters/line-filters.go index 7cd92da..3d2e12c 100644 --- a/examples/line-filters/line-filters.go +++ b/examples/line-filters/line-filters.go @@ -8,43 +8,43 @@ // pattern to write your own Go line filters. package main -// Package `bufio` will help us read line-by-line. -import "bufio" -import "strings" -import "os" -import "io" +import ( + "bufio" + "io" + "log" + "os" + "strings" +) func main() { - // Wrapping the unbuffered `os.Stdin` with a buffered // reader gives us a convenient `ReadString` method // that we'll use to read input line-by-line. - in := bufio.NewReader(os.Stdin) + rdr := bufio.NewReader(os.Stdin) out := os.Stdout - // `ReadString` returns the next string from the - // input up to the given separator byte. We give the - // newline byte `'\n'` as our separator so we'll get - // successive input lines. for { - inLine, err := in.ReadString('\n') - - // The `EOF` error is expected when we reach the - // end of input, so exit gracefully in that case. - // Otherwise there's a problem. - if err == io.EOF { - return - } - if err != nil { - panic(err) - } - - // Write out the uppercased line, checking for an - // error on the write as we did on the read. - outLine := strings.ToUpper(inLine) - _, err = out.WriteString(outLine) - if err != nil { - panic(err) + // `ReadString` returns the next string from the + // input up to the given separator byte. We give the + // newline byte `'\n'` as our separator so we'll get + // successive input lines. + switch line, err := rdr.ReadString('\n'); err { + case nil: + // Write out the uppercased line, checking for an + // error on the write as we did on the read. + ucl := strings.ToUpper(line) + if _, err = out.WriteString(ucl); err != nil { + log.Println(err) + os.Exit(-1) + } + case io.EOF: + // The `EOF` error is expected when we reach the + // end of input, so exit gracefully in that case. + os.Exit(0) + default: + // Otherwise there's a problem + log.Println(err) + os.Exit(-1) } } }