Add select
This commit is contained in:
parent
7d762500e6
commit
9fad6d576e
30
cselect/cselect.go
Normal file
30
cselect/cselect.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package cselect
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Select() {
|
||||||
|
c1, c2 := make(chan string), make(chan string)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
c1 <- "First secret!"
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
c2 <- "Second secret!"
|
||||||
|
}()
|
||||||
|
|
||||||
|
for range 2 {
|
||||||
|
select {
|
||||||
|
case msg1 := <-c1:
|
||||||
|
fmt.Println("Received something from c1:", msg1)
|
||||||
|
case msg2 := <-c2:
|
||||||
|
fmt.Println("Received something from c2:", msg2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
6
main.go
6
main.go
@ -5,7 +5,8 @@ package main
|
|||||||
// import "git.sangeeth.dev/gobyexample/generics"
|
// import "git.sangeeth.dev/gobyexample/generics"
|
||||||
// import "git.sangeeth.dev/gobyexample/errors"
|
// import "git.sangeeth.dev/gobyexample/errors"
|
||||||
// import "git.sangeeth.dev/gobyexample/goroutines"
|
// import "git.sangeeth.dev/gobyexample/goroutines"
|
||||||
import "git.sangeeth.dev/gobyexample/channels"
|
// import "git.sangeeth.dev/gobyexample/channels"
|
||||||
|
import "git.sangeeth.dev/gobyexample/cselect"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// runes.Runes()
|
// runes.Runes()
|
||||||
@ -13,5 +14,6 @@ func main() {
|
|||||||
// generics.Generics()
|
// generics.Generics()
|
||||||
// errors.Errors()
|
// errors.Errors()
|
||||||
// goroutines.Goroutines()
|
// goroutines.Goroutines()
|
||||||
channels.Channels()
|
// channels.Channels()
|
||||||
|
cselect.Select()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user