Skip to content

Instantly share code, notes, and snippets.

@fengxsong
Created September 19, 2016 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fengxsong/95a71c2d1a672fb50e04960c0b78ca99 to your computer and use it in GitHub Desktop.
Save fengxsong/95a71c2d1a672fb50e04960c0b78ca99 to your computer and use it in GitHub Desktop.
count the number of rows in a file
package main
import (
"bufio"
"fmt"
//"bytes"
"flag"
"os"
//"io"
//"io/ioutil"
"time"
)
func main() {
filepath := flag.String("f", "", "filepath name")
flag.Parse()
start := time.Now()
//f, err := ioutil.ReadFile(*filepath)
f, err := os.Open(*filepath)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
lineNum := 0
/*
//rd := bufio.NewReader(bytes.NewBuffer(f))
rd := bufio.NewReader(f)
for {
_, err = rd.ReadString('\n')
if err == io.EOF {
break
} else if err != nil {
fmt.Println(err)
return
}
lineNum++
}
*/
scanner := bufio.NewScanner(f)
//scanner.Split(bufio.ScanLines)
for scanner.Scan() {
lineNum++
}
fmt.Println(lineNum)
fmt.Println(time.Now().Sub(start))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment