How to read a file line by line in Go

If you need to read a file line by line in Go, then you can use the bufio package as follows:

package main import ( "bufio" "fmt" "log" "os" ) func main() { file, err := os.Open("/path/to/file.txt") if err != nil { log.Fatal(err) } defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { fmt.Println(scanner.Text()) } if err := scanner.Err(); err != nil { log.Fatal(err) } }
Code language: Go (go)

Just note that the Scanner will throw an error if any line has a character count longer than 65536. This relates back to a 64K size limit. In this case, you can use the Buffer method to increase the capacity.

As illustrated with the maxCapacity variable below on line 17:

package main import ( "bufio" "fmt" "log" "os" ) func main() { file, err := os.Open("/path/to/file.txt") if err != nil { log.Fatal(err) } defer file.Close() scanner := bufio.NewScanner(file) // -- swap this variable out const maxCapacity = longLineLen // your required line length buf := make([]byte, maxCapacity) // -- for scanner.Scan() { fmt.Println(scanner.Text()) } if err := scanner.Err(); err != nil { log.Fatal(err) } }
Code language: Go (go)
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments