Golang reflect.ChanDir type example
package reflect
Golang reflect.ChanDir type usage example
package main
import (
"fmt"
"reflect"
)
func main() {
var a chan<- int
var typeA reflect.Type = reflect.TypeOf(a)
fmt.Println("Channel A type is : ", typeA.ChanDir().String())
fmt.Println("Is channel A - send direction only ? : ", typeA.ChanDir() == reflect.SendDir)
fmt.Println("--------------------------------------------------------------")
var b <-chan int
var typeB reflect.Type = reflect.TypeOf(b)
fmt.Println("Channel B type is : ", typeB.ChanDir().String())
fmt.Println("Is channel B - send direction only ? : ", typeB.ChanDir() == reflect.SendDir)
fmt.Println("Is channel B - receive direction only ? : ", typeB.ChanDir() == reflect.RecvDir)
fmt.Println("--------------------------------------------------------------")
var c chan int
var typeC reflect.Type = reflect.TypeOf(c)
fmt.Println("Channel C type is : ", typeC.ChanDir()) // without String()
fmt.Println("Is channel C - send direction only ? : ", typeC.ChanDir() == reflect.SendDir)
fmt.Println("Is channel C - receive direction only ? : ", typeC.ChanDir() == reflect.RecvDir)
fmt.Println("Is channel C - both send receive direction? : ", typeC.ChanDir() == reflect.BothDir)
}
Output :
Channel A type is : chan<-
Is channel A - send direction only ? : true
--------------------------------------------------------------
Channel B type is : <-chan
Is channel B - send direction only ? : false
Is channel B - receive direction only ? : true
--------------------------------------------------------------
Channel C type is : chan
Is channel C - send direction only ? : false
Is channel C - receive direction only ? : false
Is channel C - both send receive direction? : true
SEE ALSO : https://www.socketloop.com/tutorials/golang-channels-and-buffered-channels-examples
Advertisement
Something interesting
Tutorials
+18.3k Golang : How to Set or Add Header http.ResponseWriter?
+17.3k Golang : Get path name to current directory or folder
+10.6k Golang : Handle API query by curl with Gorilla Queries example
+10.6k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+4.6k Unix/Linux : How to find out the hard disk size?
+8.8k Golang : Function wrapper that takes arguments and return result example
+8.2k Golang : does not implement flag.Value (missing Set method)
+11.7k Generate salted password with OpenSSL example
+3.5k Javascript : Detect when console is activated and do something about it
+20.6k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+7.5k Golang : Another camera capture GUI application with GTK and OpenCV