The function ScanLines splits lines from a io.Reader, it returns a channel where the results are written. Handy when you want to read things on the fly line by line:
//
// Read input line by line and send it to the returned channel. Once there's
// nothing left to read closes the channel.
//
func ScanLines(input io.Reader) <-chan string {
var output = make(chan string)
go func() {
var scanner = bufio.NewScanner(input)
for scanner.Scan() {
output <- scanner.Text()
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading input:", err)
}
close(output)
}()
return output
}
func main() {
var input = ScanLines(os.Stdin)
for x := range input {
fmt.Printf("%#v\n", x)
}
}