Created
March 7, 2017 13:44
-
-
Save mclarkson/88ad2aaad216e2134408d0c099e1e5a3 to your computer and use it in GitHub Desktop.
Output text received on input in 1 second chunks
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" | |
"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