Created
December 30, 2014 16:57
-
-
Save bmarini/a9cecd876eb0423a61c5 to your computer and use it in GitHub Desktop.
Read lines from stdin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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