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
+10.6k Golang : Resolve domain name to IP4 and IP6 addresses.
+9k Golang : Capture text return from exec function example
+12.7k Android Studio : Highlight ImageButton when pressed on example
+4.6k Linux : sudo yum updates not working
+6.6k Golang : Warp text string by number of characters or runes example
+13.5k Golang : Read XML elements data with xml.CharData example
+9.1k Golang : Intercept and compare HTTP response code example
+7.7k Gogland : Where to put source code files in package directory for rookie
+5.6k Javascript : How to refresh page with JQuery ?
+20.7k Golang : Saving private and public key to files
+9.4k Golang : Terminate-stay-resident or daemonize your program?