Skip to content

Instantly share code, notes, and snippets.

@CodyKochmann
Created July 15, 2021 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodyKochmann/c54a02f8ff7dd12ef99735df6d71ae9e to your computer and use it in GitHub Desktop.
Save CodyKochmann/c54a02f8ff7dd12ef99735df6d71ae9e to your computer and use it in GitHub Desktop.
iterate stdin lines in golang
// by: Cody Kochmann
// this iterates stdin lines to a golang channel
package main
import (
"os"
"bufio"
)
func IterStdin() <-chan string {
out := make(chan string)
go func() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
out <- scanner.Text() // Println will add back the final '\n'
}
}()
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment