line filter docco example
This commit is contained in:
parent
86854a5915
commit
46856df495
@ -1,49 +1,56 @@
|
|||||||
package main // A line-filter program reads input on stdin,
|
// ## Line Filter
|
||||||
// processes it, and prints results to stdout.
|
|
||||||
// Here is an example line filter that writes a
|
|
||||||
// a captialized version of all text it reads.
|
|
||||||
|
|
||||||
import "bufio" // Package `bufio` will help us read line-by-line.
|
// A line filter program reads input on stdin,
|
||||||
|
// processes it, and prints results to stdout.
|
||||||
|
// Here's an example line filter that writes
|
||||||
|
// a capitalized version of all text it reads.
|
||||||
|
package main
|
||||||
|
|
||||||
|
// Package `bufio` will help us read line-by-line.
|
||||||
|
import "bufio"
|
||||||
import "bytes"
|
import "bytes"
|
||||||
import "os"
|
import "os"
|
||||||
import "io"
|
import "io"
|
||||||
|
|
||||||
func main() {
|
// We'll need to add our own newlines between
|
||||||
newline := []byte("\n") // We'll need to add our own newlines
|
// processed lines.
|
||||||
// between processed lines.
|
func main() {
|
||||||
|
newline := []byte("\n")
|
||||||
|
|
||||||
in := bufio.NewReader(os.Stdin) // The buffered reader gives us `ReadLine`.
|
// The buffered reader gives us `ReadLine`.
|
||||||
|
in := bufio.NewReader(os.Stdin)
|
||||||
out := os.Stdout
|
out := os.Stdout
|
||||||
|
|
||||||
for { // If succesful, each `ReadLine` returns bytes and a
|
// If successful, each `ReadLine` returns bytes and a
|
||||||
inBytes, pfx, err := in.ReadLine() // boolean indicating if don't have the whole line yet.
|
// boolean indicating if don't have the whole line yet.
|
||||||
|
for {
|
||||||
|
inBytes, pfx, err := in.ReadLine()
|
||||||
|
|
||||||
if err == io.EOF { // The `EOF` error is expected when we reach the end
|
// The `EOF` error is expected when we reach the end
|
||||||
return // of the input, so exit gracefully in that case.
|
// of the input, so exit gracefully in that case.
|
||||||
} // Otherwise there is a problem.
|
// Otherwise there is a problem.
|
||||||
|
if err == io.EOF {
|
||||||
|
return
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic (err)
|
panic (err)
|
||||||
}
|
}
|
||||||
|
|
||||||
outBytes := bytes.ToUpper(inBytes) // `bytes.ToUpper` works directly on bytes, so we don't
|
// `bytes.ToUpper` works directly on bytes, so we don't
|
||||||
// need to convert to a string for `strings.ToUpper`.
|
// need to convert to a string for `strings.ToUpper`.
|
||||||
|
outBytes := bytes.ToUpper(inBytes)
|
||||||
_, err = out.Write(outBytes) // Write out the upercased bytes, checking for an error
|
|
||||||
if err != nil { // here as well.
|
// Write out the upercased bytes, checking for an error
|
||||||
|
// here as well.
|
||||||
|
_, err = out.Write(outBytes)
|
||||||
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !pfx { // Unless this read was for a prefix (not the full
|
// Unless this read was for a prefix (not the full
|
||||||
out.Write(newline) // line), we need to add our own newline.
|
// line), we need to add our own newline.
|
||||||
|
if !pfx {
|
||||||
|
out.Write(newline)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
$ cat > lines // Make a file with a few lowercase lines.
|
|
||||||
hello
|
|
||||||
filter
|
|
||||||
$ cat lines | go run line-filter.go // Use the line filter to get upercase lines.
|
|
||||||
HELLO
|
|
||||||
FILTER
|
|
||||||
*/
|
|
||||||
|
9
096-line-filter.sh
Normal file
9
096-line-filter.sh
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# Make a file with a few lowercase lines.
|
||||||
|
$ cat > lines
|
||||||
|
hello
|
||||||
|
filter
|
||||||
|
|
||||||
|
# Use the line filter to get uppercase lines.
|
||||||
|
$ cat lines | go run line-filter.go
|
||||||
|
HELLO
|
||||||
|
FILTER
|
Loading…
x
Reference in New Issue
Block a user