I wrote a way to easily read each line from a file. The Readln function (* bufio.Reader) returns a string (sans \ n) from the base structure of bufio.Reader.
// Readln returns a single line (without the ending \n) // from the input buffered reader. // An error is returned iff there is an error with the // buffered reader. func Readln(r *bufio.Reader) (string, error) { var (isPrefix bool = true err error = nil line, ln []byte ) for isPrefix && err == nil { line, isPrefix, err = r.ReadLine() ln = append(ln, line...) } return string(ln),err }
You can use Readln to read each line from a file. The following code reads each line in the file and prints each line to standard output.
f, err := os.Open(fi) if err != nil { fmt.Println("error opening file= ",err) os.Exit(1) } r := bufio.NewReader(f) s, e := Readln(r) for e == nil { fmt.Println(s) s,e = Readln(r) }
Hurrah!
Malcolm Aug 30 '12 at 22:17 2012-08-30 22:17
source share