package main import ( "fmt" "os" "time" ) /* @Ref: https://livebook.manning.com/book/go-in-practice/chapter-3/65 1. time.After returns a channel 2. channel usage 3. select usage 4. channel declaration with specifying a direction */ func main() { done := time.After(30 * time.Second) echo := make(chan []byte) go readStdin(echo) for { select { case buf := <-echo: os.Stdout.Write(buf) case <-done: fmt.Println("Time out") os.Exit(0) } } } func readStdin(out chan<- []byte) { for { data := make([]byte, 1024) l, _ := os.Stdin.Read(data) if l > 0 { out <- data } } }