import suggestion from ward
This commit is contained in:
parent
853f862880
commit
6b2aa07fa5
@ -8,43 +8,43 @@
|
|||||||
// pattern to write your own Go line filters.
|
// pattern to write your own Go line filters.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
// Package `bufio` will help us read line-by-line.
|
import (
|
||||||
import "bufio"
|
"bufio"
|
||||||
import "strings"
|
"io"
|
||||||
import "os"
|
"log"
|
||||||
import "io"
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
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
|
// reader gives us a convenient `ReadString` method
|
||||||
// that we'll use to read input line-by-line.
|
// that we'll use to read input line-by-line.
|
||||||
in := bufio.NewReader(os.Stdin)
|
rdr := bufio.NewReader(os.Stdin)
|
||||||
out := os.Stdout
|
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 {
|
for {
|
||||||
inLine, err := in.ReadString('\n')
|
// `ReadString` returns the next string from the
|
||||||
|
// input up to the given separator byte. We give the
|
||||||
// The `EOF` error is expected when we reach the
|
// newline byte `'\n'` as our separator so we'll get
|
||||||
// end of input, so exit gracefully in that case.
|
// successive input lines.
|
||||||
// Otherwise there's a problem.
|
switch line, err := rdr.ReadString('\n'); err {
|
||||||
if err == io.EOF {
|
case nil:
|
||||||
return
|
// Write out the uppercased line, checking for an
|
||||||
}
|
// error on the write as we did on the read.
|
||||||
if err != nil {
|
ucl := strings.ToUpper(line)
|
||||||
panic(err)
|
if _, err = out.WriteString(ucl); err != nil {
|
||||||
}
|
log.Println(err)
|
||||||
|
os.Exit(-1)
|
||||||
// Write out the uppercased line, checking for an
|
}
|
||||||
// error on the write as we did on the read.
|
case io.EOF:
|
||||||
outLine := strings.ToUpper(inLine)
|
// The `EOF` error is expected when we reach the
|
||||||
_, err = out.WriteString(outLine)
|
// end of input, so exit gracefully in that case.
|
||||||
if err != nil {
|
os.Exit(0)
|
||||||
panic(err)
|
default:
|
||||||
|
// Otherwise there's a problem
|
||||||
|
log.Println(err)
|
||||||
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user