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
+24.1k Golang : Use regular expression to validate domain name
+4.2k Javascript : Empty an array example
+6.4k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+5.3k Python : Create Whois client or function example
+5.8k Golang : Error handling methods
+20.2k Golang : How to run your code only once with sync.Once object
+7.7k Golang : How to handle file size larger than available memory panic issue
+4.8k Javascript : Access JSON data example
+8.3k Golang : Tell color name with OpenCV example
+6.1k Linux/MacOSX : Search for files by filename and extension with find command
+5.3k Golang : The Tao of importing package
+19.9k Golang : Archive directory with tar and gzip