From 6b2aa07fa532dbc9b1ead52149c780c68f06e890 Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Tue, 16 Oct 2012 10:22:55 -0700 Subject: [PATCH 1/6] 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) } } } From 06c7cbd52cc75585cf53d8f8af923499a6d5828a Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Tue, 16 Oct 2012 10:23:04 -0700 Subject: [PATCH 2/6] whitespace for rendering --- examples/line-filters/line-filters.go | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/line-filters/line-filters.go b/examples/line-filters/line-filters.go index 3d2e12c..f17b5d6 100644 --- a/examples/line-filters/line-filters.go +++ b/examples/line-filters/line-filters.go @@ -17,6 +17,7 @@ import ( ) 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. From ca74db848f5b0eb381d367b3d2fbbaf48b85f780 Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Tue, 16 Oct 2012 10:29:10 -0700 Subject: [PATCH 3/6] comment editing --- examples/line-filters/line-filters.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/examples/line-filters/line-filters.go b/examples/line-filters/line-filters.go index f17b5d6..2edec12 100644 --- a/examples/line-filters/line-filters.go +++ b/examples/line-filters/line-filters.go @@ -24,26 +24,31 @@ func main() { 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 { - // `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 { + + // If the read succeeded, write out out the + // uppercased line. Check for an error on the + // write as we do on the read. 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) } + + // The `EOF` error is expected when we reach the + // end of input, so exit gracefully in that case. case io.EOF: - // The `EOF` error is expected when we reach the - // end of input, so exit gracefully in that case. os.Exit(0) + + // Otherwise there's a problem; print the + // error and exit with non-zero status. default: - // Otherwise there's a problem log.Println(err) os.Exit(-1) } From 594a75670422d87238273d1b1e17ffadf961256c Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Tue, 16 Oct 2012 10:33:56 -0700 Subject: [PATCH 4/6] more familiar exit code --- examples/line-filters/line-filters.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/line-filters/line-filters.go b/examples/line-filters/line-filters.go index 2edec12..a941f38 100644 --- a/examples/line-filters/line-filters.go +++ b/examples/line-filters/line-filters.go @@ -38,7 +38,7 @@ func main() { ucl := strings.ToUpper(line) if _, err = out.WriteString(ucl); err != nil { log.Println(err) - os.Exit(-1) + os.Exit(1) } // The `EOF` error is expected when we reach the @@ -50,7 +50,7 @@ func main() { // error and exit with non-zero status. default: log.Println(err) - os.Exit(-1) + os.Exit(1) } } } From 342ffb491d17146a321b520ef6acc982eeb11ddd Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Tue, 16 Oct 2012 10:36:58 -0700 Subject: [PATCH 5/6] print errors to stderr, just use fmt for now --- examples/line-filters/line-filters.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/line-filters/line-filters.go b/examples/line-filters/line-filters.go index a941f38..8cd9af5 100644 --- a/examples/line-filters/line-filters.go +++ b/examples/line-filters/line-filters.go @@ -10,8 +10,8 @@ package main import ( "bufio" + "fmt" "io" - "log" "os" "strings" ) @@ -37,7 +37,7 @@ func main() { case nil: ucl := strings.ToUpper(line) if _, err = out.WriteString(ucl); err != nil { - log.Println(err) + fmt.Fprintln(os.Stderr, "error:", err) os.Exit(1) } @@ -49,7 +49,7 @@ func main() { // Otherwise there's a problem; print the // error and exit with non-zero status. default: - log.Println(err) + fmt.Fprintln(os.Stderr, "error:", err) os.Exit(1) } } From 3b081dae265eca6cfcdf31868041b053c4768d1a Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Tue, 16 Oct 2012 10:39:00 -0700 Subject: [PATCH 6/6] explain tricky err switch --- examples/line-filters/line-filters.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/line-filters/line-filters.go b/examples/line-filters/line-filters.go index 8cd9af5..38f60bc 100644 --- a/examples/line-filters/line-filters.go +++ b/examples/line-filters/line-filters.go @@ -31,9 +31,9 @@ func main() { for { switch line, err := rdr.ReadString('\n'); err { - // If the read succeeded, write out out the - // uppercased line. Check for an error on the - // write as we do on the read. + // If the read succeeded (the read `err` is nil), + // write out out the uppercased line. Check for an + // error on this write as we do on the read. case nil: ucl := strings.ToUpper(line) if _, err = out.WriteString(ucl); err != nil {