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
+15k Golang : How do I get the local IP (non-loopback) address ?
+16k Golang : Read large file with bufio.Scanner cause token too long error
+19.1k Mac OSX : Homebrew and Golang
+5.2k Golang : The Tao of importing package
+13.2k Golang : Convert(cast) int to int64
+12.4k Golang : Extract part of string with regular expression
+14.4k Golang : How to pass map to html template and access the map's elements
+5.2k PHP : See installed compiled-in-modules
+25.6k Golang : convert rune to integer value
+51.1k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+43.3k Golang : Convert []byte to image
+13.6k Golang : reCAPTCHA example