Avatar ·

How to read a file line by line in Go: a step-by-step guide

Hello everyone! I'm a beginner Go developer, and I'm having trouble reading a text file line by line. I need to process a large log file (about 500 MB) where each line is a separate record. The problem is that I don't want to load the entire file into memory at once because it's very large. I tried using `ioutil.ReadFile()`, but it loads all the content into memory, and the program starts to slow down. Moreover, if the file turns out to be even larger, it could lead to memory exhaustion. Here's what I tried: ```go package main import ( "fmt" "io/ioutil" "strings" ) func main() { data, err := ioutil.ReadFile("large_log.txt") if err != nil { fmt.Println("Error:", err) return } lines := strings.Split(string(data), "\n") ```