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
+34k Golang : Proper way to set function argument default value
+18k Golang : Check if a directory exist or not
+25.4k Golang : Convert long hexadecimal with strconv.ParseUint example
+9.1k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+12.4k Golang : Search and extract certain XML data example
+27.9k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+22k Golang : Join arrays or slices example
+13.7k Golang : Activate web camera and broadcast out base64 encoded images
+14.7k Golang : Get URI segments by number and assign as variable example
+7.9k Golang : Trim everything onward after a word
+30k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+7.5k Golang : Gorrila set route name and get the current route name