Created
October 10, 2017 09:13
-
-
Save radxene/f336fe86febce451363386163d6f0f02 to your computer and use it in GitHub Desktop.
Golang - Read Lines From 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" | |
"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