Skip to content

Instantly share code, notes, and snippets.

@bmarini
Created December 30, 2014 16:57
Show Gist options
  • Save bmarini/a9cecd876eb0423a61c5 to your computer and use it in GitHub Desktop.
Save bmarini/a9cecd876eb0423a61c5 to your computer and use it in GitHub Desktop.
Read lines from stdin
package main
import (
"bufio"
"fmt"
"io"
"os"
)
func main() {
for line := range scan(os.Stdin) {
fmt.Printf("%v\n", line)
}
}
func scan(input io.Reader) chan string {
lines := make(chan string)
go func() {
scanner := bufio.NewScanner(os.Stdin)
for ok := scanner.Scan(); ok; ok = scanner.Scan() {
lines <- scanner.Text()
}
close(lines)
}()
return lines
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment