Golang reflect.SelectCase and SelectDir types example
package reflect
Golang reflect.SelectCase and SelectDir types usage example
package main
import (
"fmt"
"reflect"
)
func main() {
var chr = make(chan string, 1)
var rCase reflect.SelectCase
rCase.Dir = reflect.SelectRecv
rCase.Chan = reflect.ValueOf(chr)
// see http://golang.org/pkg/reflect/#SelectDir
// A SelectDir describes the communication direction of a select case.
fmt.Println("Direction of case : ", rCase.Dir) // SelectRecv enum #2 - direction of case
fmt.Println("Channel to use : ", rCase.Chan) // channel to use (for send or receive)
// invalid because this is SelectRecv case, not SelectSend
fmt.Println("Value to send : ", rCase.Send)
fmt.Println("-------------------------------------")
var chs = make(chan string, 2)
var sCase reflect.SelectCase
sCase.Dir = reflect.SelectSend
sCase.Send = reflect.ValueOf(chs)
// see http://golang.org/pkg/reflect/#SelectDir
fmt.Println("Direction of case : ", sCase.Dir) // SelectSend enum #1- direction of case
fmt.Println("Channel to use : ", sCase.Chan) // invalid because ONLY send, not send or receive
fmt.Println("Value to send : ", sCase.Send) // valid because this the SelectSend case
}
Output :
Direction of case : 2
Channel to use :
Value to send :
Direction of case : 1
Channel to use :
Value to send :
References :
Advertisement
Something interesting
Tutorials
+27.7k PHP : Count number of JSON items/objects
+7.9k Golang Hello World Example
+7.5k Golang : Gorrila set route name and get the current route name
+15.6k Golang : Force download file example
+5.9k Unix/Linux : How to open tar.gz file ?
+7.5k Golang : Get YouTube playlist
+11.3k Golang : Characters limiter example
+16.3k Golang : How to extract links from web page ?
+18.4k Golang : How to get hour, minute, second from time?
+13.4k Golang : Get constant name from value
+8.1k Golang : How To Use Panic and Recover