Created
July 15, 2015 16:21
-
-
Save arschles/6523b48f1c393ae9aa0e to your computer and use it in GitHub Desktop.
golang read a file line by line
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
import ( | |
"bufio" | |
"os" | |
) | |
func main() { | |
f := os.Open("somefile") | |
defer f.Close | |
rdr := bufio.NewReader(f) | |
for { | |
// I'm assuming it's ok to assume lines are newline-terminated | |
line, err := rdr.ReadString("\n") | |
if err != nil { | |
break | |
} | |
//do something with line | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment