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
+17.7k How to enable MariaDB/MySQL logs ?
+6.8k Golang : Calculate pivot points for a cross
+12.7k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+9.7k Golang : Detect number of active displays and the display's resolution
+7.3k Golang : alternative to os.Exit() function
+5.1k Swift : Convert (cast) Float to Int or Int32 value
+5.8k Golang : Launching your executable inside a console under Linux
+8.3k Golang : Count leading or ending zeros(any item of interest) example
+3.7k Golang : Switch Redis database redis.NewClient
+24.1k Golang : Upload to S3 with official aws-sdk-go package
+12.4k Golang : Extract part of string with regular expression
+25.2k Golang : Storing cookies in http.CookieJar example