- Open
- NewReader
- ReadString
- readline
Read file line by line with Reader
- Leaves the newlines at the end of the line
examples/read-file-line-by-line/read_file_line_by_line.go
package main import ( "bufio" "fmt" "io" "os" ) func main() { filename := "random1.txt" fh, err := os.Open(filename) if err != nil { fmt.Printf("Could not open file '%v': %v", filename, err) os.Exit(1) } reader := bufio.NewReader(fh) for { line, err := reader.ReadString('\n') if err != nil { if err != io.EOF { fmt.Println(err) } break } fmt.Printf("Line: %v", line) } }
examples/read-file-line-by-line/random1.txt
This is just some random text with more than one lines. This is already the 3rd line.