Created
November 14, 2017 19:49
-
-
Save porcupie/033037796510e8a3b573849af8d94ddd to your computer and use it in GitHub Desktop.
testing golang bufio.Scanner
This file contains 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" | |
"flag" | |
"fmt" | |
"log" | |
"os" | |
) | |
var () | |
func main() { | |
flag.Parse() | |
// first arg is filename | |
filename := flag.Arg(0) | |
log.Printf("Preparing to open file: %s", filename) | |
f, err := os.Open(filename) // This will block on a pipe with no data waiting | |
defer f.Close() | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Printf("Preparing to Scan file") | |
scanner := bufio.NewScanner(f) | |
for scanner.Scan() { | |
log.Println("Scan()") | |
fmt.Println(scanner.Text()) | |
} | |
if err = scanner.Err(); err != nil { | |
log.Println("Got scanner.Err()") | |
log.Fatal(err) | |
} | |
log.Println("Finished scanning file") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment