How to read a file line by line in Go: a step-by-step guide
Hello everyone! I just started learning Go and encountered a task that seems simple but for some reason is causing me difficulties. I need to read a text file line by line. The file can be quite large (several hundred megabytes), so I want to avoid loading the entire content into memory.\n\nHere's what I tried:\n\n```go\npackage main\n\nimport (\n "fmt"\n "os"\n)\n\nfunc main() {\n data, err := os.ReadFile("file.txt")\n if err != nil {\n panic(err)\n }\n lines := strings.Split(string(data), "")\n for _, line := range lines {\n fmt.Println(line)\n }\n}\n```\n\nBut this code loads the entire file into memory, which is inefficient for large files. Also, I heard that Go has a special scanner for line-by-line reading, but I can't figure out how to