Add select

This commit is contained in:
Sangeeth Sudheer 2024-04-28 15:33:42 +05:30
parent 7d762500e6
commit 9fad6d576e
Signed by: x
GPG Key ID: F6D06ECE734C57D1
2 changed files with 34 additions and 2 deletions

30
cselect/cselect.go Normal file
View 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)
}
}
}

View File

@ -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()
} }