Created
October 9, 2019 13:10
-
-
Save jvmvik/2ff3f722a60ef9a69852b6202d7ceca0 to your computer and use it in GitHub Desktop.
Draft to create a parallel grep using channel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Go grep parallel | |
| // https://golang.hotexamples.com/examples/codesearch.regexp/Grep/File/golang-grep-file-method-examples.html | |
| func main() { | |
| var g regexp.Grep | |
| g.AddFlags() | |
| g.Stdout = os.Stdout | |
| g.Stderr = os.Stderr | |
| flag.Usage = usage | |
| flag.Parse() | |
| args := flag.Args() | |
| if len(args) == 0 { | |
| flag.Usage() | |
| } | |
| if *cpuProfile != "" { | |
| f, err := os.Create(*cpuProfile) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer f.Close() | |
| pprof.StartCPUProfile(f) | |
| defer pprof.StopCPUProfile() | |
| } | |
| pat := "(?m)" + args[0] | |
| if *iflag { | |
| pat = "(?i)" + pat | |
| } | |
| re, err := regexp.Compile(pat) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| g.Regexp = re | |
| if len(args) == 1 { | |
| g.Reader(os.Stdin, "<standard input>") | |
| } else { | |
| for _, arg := range args[1:] { | |
| g.File(arg) | |
| } | |
| } | |
| if !g.Match { | |
| os.Exit(1) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment