← Back to Gists

read file line by line

📝 Go
mcollins
mcollins · Level 3 ·

Reads a file line by line using a buffered scanner.

Go
package main import ( "bufio" "log" "os"
) func main() { file, err := os.Open("file.txt") if err != nil { log.Fatal(err) } defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { line := scanner.Text() println(line) } if err := scanner.Err(); err != nil { log.Fatal(err) }
}

Comments

No comments yet. Start the discussion.