Last active
December 23, 2015 20:39
-
-
Save nm3mon/6690725 to your computer and use it in GitHub Desktop.
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 ( | |
"fmt" | |
"bufio" | |
"os" | |
"log" | |
"io" | |
) | |
func main() { | |
var name string | |
fmt.Println(len(os.Args)) | |
if len(os.Args) > 1 { | |
// fmt.Println(ioutil.ReadFile("/Users/nm3mon/Projects/algs4j/src/test/resources/tale.txt")) | |
name = os.Args[1] | |
} else { | |
name = "/Users/nm3mon/Projects/algs4j/src/test/resources/leipzig1M.txt" | |
} | |
readFile(name) | |
} | |
func readFile(name string) { | |
count := 0 | |
file, err := os.Open(name) | |
if err != nil { | |
log.Fatal("can't read file") | |
} | |
defer file.Close() | |
bufReader:= bufio.NewReader(file) | |
const newline = '\n' | |
for { | |
line, err := bufReader.ReadString(newline) | |
if err != nil { | |
if err == io.EOF { | |
break | |
} | |
log.Fatal("caught error reading new line") | |
} | |
count++ | |
fmt.Print(line) | |
} | |
fmt.Printf("\n\ntotal lines read: %d\n", count) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment