Skip to content

Instantly share code, notes, and snippets.

@nm3mon
Last active December 23, 2015 20:39
Show Gist options
  • Save nm3mon/6690725 to your computer and use it in GitHub Desktop.
Save nm3mon/6690725 to your computer and use it in GitHub Desktop.
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