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) ValueParameters: 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:
// Golang program to illustrate // reflect.MakeChan() Function package main import ( "fmt"
"reflect"
) // Main function func main() { var val chan int
// create new channel
value := reflect.MakeChan(reflect.Indirect(reflect.ValueOf(&val)).Type(), 0)
fmt.Println( "Value :" , value)
} |
Output:
Value : 0xc00005e060
Example 2:
// Golang program to illustrate // reflect.MakeChan() Function package main import ( "fmt"
"reflect"
) // Main function func main() { var val chan string
var strVal reflect.Value = reflect.ValueOf(&val)
indirectStr := reflect.Indirect(strVal)
// create new channel
value := reflect.MakeChan(indirectStr.Type(), 1024)
fmt.Printf( "Type : [%v] \nCapacity : [%v]" , value.Kind(), value.Cap())
} |
Output:
Type : [chan] Capacity : [1024]