Skip to content

Instantly share code, notes, and snippets.

@mclarkson
Created March 7, 2017 13:44
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 mclarkson/88ad2aaad216e2134408d0c099e1e5a3 to your computer and use it in GitHub Desktop.
Save mclarkson/88ad2aaad216e2134408d0c099e1e5a3 to your computer and use it in GitHub Desktop.
Output text received on input in 1 second chunks
package main
import (
"bufio"
"fmt"
"os"
"time"
)
func main() {
input := make(chan string, 1)
getInput := func(input chan string) {
in := bufio.NewReader(os.Stdin)
for {
result, err := in.ReadString('\n')
input <- result
if err != nil {
close(input)
break
}
}
}
go getInput(input)
lines := ""
t := time.NewTicker(time.Duration(1000 * time.Millisecond))
OuterLoop:
for {
select {
case i, ok := <-input:
lines += i
if ok == false {
fmt.Printf(lines)
break OuterLoop
}
case <-t.C:
if len(lines) > 0 {
fmt.Printf(lines)
lines = ""
}
}
}
t.Stop()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment