Created
September 19, 2016 09:07
-
-
Save fengxsong/95a71c2d1a672fb50e04960c0b78ca99 to your computer and use it in GitHub Desktop.
count the number of rows in a file
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" | |
"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