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.4k Golang : Generate random integer or float number
+14.4k Golang : How to pass map to html template and access the map's elements
+22.4k Golang : Read directory content with filepath.Walk()
+14.8k Golang : Get URI segments by number and assign as variable example
+10.6k Golang : Get local time and equivalent time in different time zone
+5.8k Golang : List all packages and search for certain package
+16.7k Golang : Gzip file example
+14.8k Golang : Normalize unicode strings for comparison purpose
+4.6k MariaDB/MySQL : How to get version information
+6.1k Golang : Grab news article text and use NLP to get each paragraph's sentences
+5.2k JavaScript/JQuery : Redirect page examples
+24.5k Golang : GORM read from database example