Created
February 25, 2014 06:09
-
-
Save nickserv/9203620 to your computer and use it in GitHub Desktop.
A simple grep implementation in Go
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
package main | |
import( | |
"bufio" | |
"fmt" | |
"log" | |
"os" | |
"regexp" | |
) | |
func handleLine(line string) { | |
matched, err := regexp.MatchString(os.Args[1], line) | |
if err != nil { | |
log.Fatal(err) | |
} else if matched { | |
fmt.Println(line) | |
} | |
} | |
func main() { | |
scanner := bufio.NewScanner(os.Stdin) | |
for scanner.Scan() { | |
handleLine(scanner.Text()) | |
} | |
if err := scanner.Err(); err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
oof, this is pretty painful, you match and check for err on every Scan(), what you should do is
and then every Scan()
no need for handleLine() function at all