Documente Academic
Documente Profesional
Documente Cultură
grep
regular expressions, goroutines, pipelines
04/14/10 grep – 1
04/14/10 grep – 2
grep/grep.go
package grep; import ( "bufio"; "io"; "os"; "regexp"; "strings" )
04/14/10 grep – 4
http://golang.org/pkg/io/#Reader
http://golang.org/pkg/io/#Writer
bufio
import "bufio"
slightly byte-aware.
04/14/10 grep – 5
http://golang.org/pkg/bufio/
regexp
import "regexp"
04/14/10 grep – 6
http://golang.org/pkg/regexp/
grep/main.go
package main
04/14/10 grep – 7
grep/mgrep.go
init
package main; import ( "flag"; "fmt"; "grep"; "io"; "os"; "runtime" )
func main() {
fflag := flag.Bool("f", false, "use file system pipe")
flag.Parse()
if flag.NArg() != 1 { // arguments?
fmt.Fprintln(os.Stderr, "usage: mgrep [-f] regular-expression")
os.Exit(1)
}
pipe := func() (io.Reader, io.WriteCloser, os.Error) { // -f?
if *fflag { return os.Pipe() }
i, o := io.Pipe(); return i, o, nil
}
04/14/10 grep – 8
grep/mgrep.go
one step
runtime.GOMAXPROCS(2) // arrange to schedule 2 CPUs
04/14/10 grep – 9
grep/mgrep.go
pipeline
var in io.Reader = os.Stdin // first input from stdin
04/14/10 grep – 10
go
go function-call-expression
go func() { ... } ()
04/14/10 grep – 11
http://golang.org/doc/go_spec.html#Go_statements
runtime
04/14/10 grep – 12
http://golang.org/pkg/runtime/