Golang - Read a file line by line

import (
      "bufio"
      "os"
)

func FileToLines(filePath string) (lines []string, err error) {
      f, err := os.Open(filePath)
      if err != nil {
              return
      }
      defer f.Close()

      scanner := bufio.NewScanner(f)
      for scanner.Scan() {
              lines = append(lines, scanner.Text())
      }
      err = scanner.Err()
      return
}

Comments system

[blogger][disqus][facebook]
Powered by Blogger.