reflect.MakeChan() Function in Golang with Examples
Last Updated :
28 Apr, 2020
Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.MakeChan() Function in Golang is used to create a new channel with the specified type and buffer size. To access this function, one needs to imports the reflect package in the program.
Syntax:
func MakeChan(typ Type, buffer int) Value
Parameters: This function takes only two parameters of Type type(typ) and int type (buffer).
Return Value: This function returns the newly created channel.
Below examples illustrate the use of above method in Golang:
Example 1:
package main
import (
"fmt"
"reflect"
)
func main() {
var val chan int
value := reflect.MakeChan(reflect.Indirect(reflect.ValueOf(&val)).Type(), 0)
fmt.Println( "Value :" , value)
}
|
Output:
Value : 0xc00005e060
Example 2:
package main
import (
"fmt"
"reflect"
)
func main() {
var val chan string
var strVal reflect.Value = reflect.ValueOf(&val)
indirectStr := reflect.Indirect(strVal)
value := reflect.MakeChan(indirectStr.Type(), 1024)
fmt.Printf( "Type : [%v] \nCapacity : [%v]" , value.Kind(), value.Cap())
}
|
Output:
Type : [chan]
Capacity : [1024]
Share your thoughts in the comments
Please Login to comment...