Skip to content

Instantly share code, notes, and snippets.

@radxene
Created October 10, 2017 09:13
Show Gist options
  • Save radxene/f336fe86febce451363386163d6f0f02 to your computer and use it in GitHub Desktop.
Save radxene/f336fe86febce451363386163d6f0f02 to your computer and use it in GitHub Desktop.
Golang - Read Lines From File
package main
import (
"bufio"
"fmt"
"os"
)
func ReadFileLines(filePath string) []string {
f, err := os.Open(filePath)
if err != nil {
panic(err)
}
defer f.Close()
var lines []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, err)
}
return lines
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment