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
+17k Golang : Get input from keyboard
+20.2k Golang : Count number of digits from given integer value
+22.7k Golang : Strings to lowercase and uppercase example
+7.3k Golang : Not able to grep log.Println() output
+9.1k Golang : Gonum standard normal random numbers example
+6k Golang : Compound interest over time example
+11k Golang : Create Temporary File
+6.5k PHP : Shuffle to display different content or advertisement
+9.8k Golang : Qt get screen resolution and display on center example
+8.2k Golang : HttpRouter multiplexer routing example
+8.2k Golang : Reverse text lines or flip line order example
+14.6k Golang : How to get URL port?