Post

GoLangGoLang

S
Surya kumar

Posted on 29th March 2024|36 views

0
votes

Golang Read File Line By Line

Can I read a file line by line in Go?

Answers
P
sreelu 123

Posted on 29th March 2024

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 } }

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos