Scanner (Lines) in Go

Posted by GoDoc
Public (Editable by Users)

The simplest use of a Scanner, to read standard input as a set of lines.

Go
Edit
package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		fmt.Println(scanner.Text()) // Println will add back the final '\n'
	}
	if err := scanner.Err(); err != nil {
		fmt.Fprintln(os.Stderr, "reading standard input:", err)
	}
}