Skip to content

Instantly share code, notes, and snippets.

@duguying
Created November 16, 2016 06:42
Show Gist options
  • Save duguying/62c486477b8c3da2e39975330f499f8c to your computer and use it in GitHub Desktop.
Save duguying/62c486477b8c3da2e39975330f499f8c to your computer and use it in GitHub Desktop.
a demo for usage of goroutin, channel, select
package main
import (
"fmt"
"time"
)
func main() {
timeout := make(chan bool, 1)
ch := make(chan int)
hello := make(chan string)
go func() {
time.Sleep(1e9)
timeout <- true
ch <- 1
}()
go func() {
time.Sleep(10 * 1e9)
hello <- "hello world"
}()
for {
time.Sleep(1e8)
select {
case <-ch:
fmt.Println("\nch channel")
case <-timeout:
fmt.Println("\ntimeout!")
case str := <-hello:
fmt.Println("\n", str)
default:
fmt.Printf(".")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment