line filter work

This commit is contained in:
Mark McGranaghan 2012-10-03 16:13:37 -04:00
parent f7bea6ec03
commit c0e4eac2fa
4 changed files with 26 additions and 18 deletions

@ -10,3 +10,5 @@ func main() {
fmt.Println(arr)
fmt.Println(str)
}
// todo: bytes package?

BIN
src/066-line-filters/line-filters Executable file

Binary file not shown.

@ -1,35 +1,41 @@
// ## Line Filters
// 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.
// A _line filter_ is a very common type of program that
// reads input on stdin, processes it, and then prints
// some derived results to stdout. `grep` and `sed` for
// example are common line filters.
// Here's an example line filter in Go that writes a
// capitalized version of all text it reads. You can use
// this pattern to write your own Go line filters.
package main
// Package `bufio` will help us read line-by-line.
// Package `bufio` will help us read line-by-line, and
// `bytes` provides the byte-level capitaliazation
// function.
import "bufio"
import "bytes"
import "os"
import "io"
// We'll need to add our own newlines between
// processed lines.
func main() {
newline := []byte("\n")
var newline = []byte("\n")
// The buffered reader gives us `ReadLine`.
func main() {
// Wrapping the unbuffered `os.Stdin` with a buffered
//reader gives us the convenient `ReadLine` method.
in := bufio.NewReader(os.Stdin)
out := os.Stdout
// If successful, each `ReadLine` returns bytes and a
// boolean indicating if don't have the whole line
// yet.
// Each `ReadLine` call returns bytes of read data and
// a boolean indicating if we don't have the whole
// line yet, or an error.
for {
inBytes, pfx, err := in.ReadLine()
// The `EOF` error is expected when we reach the
// end of the input, so exit gracefully in that
// case. Otherwise there is a problem.
// end of input, so exit gracefully in that case.
// Otherwise there is a problem.
if err == io.EOF {
return
}

@ -1,8 +1,8 @@
# Make a file with a few lowercase lines.
$ echo 'hello' > lines
$ echo 'filter' >> lines
$ echo 'hello' > /tmp/lines
$ echo 'filter' >> /tmp/lines
# Use the line filter to get uppercase lines.
$ cat lines | go run line-filters.go
$ cat /tmp/lines | go run line-filters.go
HELLO
FILTER