title: Golang: How do I determine the number of lines in a file efficiently?
body: <p>In Golang, I am looking for an efficient way to determine the number of lines a file has.</p>
  
  <p>Of course, I can always loop through the entire file, but does not seem very efficient.</p>
  
  <pre><code>file, _ := os.Open("/path/to/filename")
  fileScanner := bufio.NewScanner(file)
  lineCount := 0
  for fileScanner.Scan() {
      lineCount++
  }
  fmt.Println("number of lines:", lineCount)
  </code></pre>
  
  <p>Is there a better (quicker, less expensive) way to find out how many lines a file has?</p>