Posted on 28th April 2025|38 views
Can I read a file line by line in Go?
Posted on 28th April 2025| views
One of the simple ways is to use readstring with a \n as a separator here is the example
f, err := os.Open(filename)
if err != nil {
fmt.Println("opening a error file ", err)
os.Exit(1) }
defer f.Close() r := bufio.NewReader(f)
for { path, err := r.ReadString(10)
if err == io.EOF {
// perform something and break }
else if err != nil {
return err } }