diff --git a/src/054-bytes/bytes.go b/src/054-bytes/bytes.go index 95cfe23..c69baef 100644 --- a/src/054-bytes/bytes.go +++ b/src/054-bytes/bytes.go @@ -10,3 +10,5 @@ func main() { fmt.Println(arr) fmt.Println(str) } + +// todo: bytes package? diff --git a/src/066-line-filters/line-filters b/src/066-line-filters/line-filters new file mode 100755 index 0000000..f398a7e Binary files /dev/null and b/src/066-line-filters/line-filters differ diff --git a/src/066-line-filters/line-filters.go b/src/066-line-filters/line-filters.go index 67ebc29..8dde90d 100644 --- a/src/066-line-filters/line-filters.go +++ b/src/066-line-filters/line-filters.go @@ -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 } diff --git a/src/066-line-filters/line-filters.sh b/src/066-line-filters/line-filters.sh index 9b8dd24..dfd718e 100644 --- a/src/066-line-filters/line-filters.sh +++ b/src/066-line-filters/line-filters.sh @@ -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