Golang reflect.Select() and SelectCase() function example
package reflect
Golang reflect.Select() and SelectCase() function usage example
package main
import (
"fmt"
"reflect"
)
func main() {
var sendCh = make(chan int) // channel to use (for send or receive)
var increaseInt = func(c chan int) {
for i := 0; i < 8; i++ {
c <- i
}
close(c)
}
go increaseInt(sendCh)
var selectCase = make([]reflect.SelectCase, 1)
selectCase[0].Dir = reflect.SelectRecv
selectCase[0].Chan = reflect.ValueOf(sendCh)
counter := 0
for counter < 1 {
chosen, recv, recvOk := reflect.Select(selectCase) // <--- here
if recvOk {
fmt.Println(chosen, recv.Int(), recvOk)
} else {
counter++
}
}
}
References :
Advertisement
Something interesting
Tutorials
+5.3k Javascript : Shuffle or randomize array example
+6.2k Linux/Unix : Commands that you need to be careful about
+12.1k Golang : md5 hash of a string
+5k Google : Block or disable caching of your website content
+5k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+27.6k Golang : dial tcp: too many colons in address
+26.6k Golang : Encrypt and decrypt data with AES crypto
+8.2k How to show different content from website server when AdBlock is detected?
+7.3k Golang : Not able to grep log.Println() output
+21.8k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+15.9k Golang : Get file permission
+8.1k Golang : Check from web if Go application is running or not