2

I've started studying golang a bit and I'm absolutely unable to understand how I'm supposed to read line by line in the old-fashioned way:

while filehandler != EOF {
line_buffer = readline(filehandler)
}

I'm aware that I have to use bufio scanlines. This isn't what I am using as code, I'm merely trying to explain the idea.

user4654137
  • 49
  • 1
  • 7
  • See http://stackoverflow.com/questions/8757389/reading-file-line-by-line-in-go – Robert Jacobs Jun 12 '15 at 15:15
  • 2
    If you know what to use (`bufio.Scanner`), then check examples on golang.org, there is one for reading lines! http://golang.org/pkg/bufio/#Scanner – tomasz Jun 12 '15 at 15:37

1 Answers1

9

use this:

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    file, _ := os.Open("path/to_file")
    fscanner := bufio.NewScanner(file)
    for fscanner.Scan() {
        fmt.Println(fscanner.Text())
    }
}