reflect.Call() Function in Golang with Examples
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.Call() Function in Golang is used to calls the function v with the input arguments in. To access this function, one needs to imports the reflect package in the program.
Syntax:
func (v Value) Call(in []Value) []ValueParameters: This function takes the following parameters:
- in : This parameter is the []Value type.
Return Value: This function returns the output results as Values.
Below examples illustrate the use of the above method in Golang:
Example 1:
// Golang program to illustrate // reflect.Call() Function package main import "fmt" import "reflect" type T struct {} func (t *T) Beginner() { fmt.Println( "GeekforBeginner" ) } func main() { var t T // use of Call() method val := reflect.ValueOf(&t).MethodByName( "Beginner" ).Call([]reflect.Value{}) fmt.Println(val) } |
Output:
GeekforBeginner []
Example 2:
// Golang program to illustrate // reflect.Call() Function package main import "fmt" import "reflect" type T struct {} func (t *T) Beginner() { fmt.Println( "GeekforBeginner" ) } func (t *T) Beginner1() { fmt.Println( "Golang Package :" ) fmt.Println( "reflect" ) fmt.Println( "Call() Function" ) } func main() { var t T var t1 T // use of Call() method reflect.ValueOf(&t).MethodByName( "Beginner" ).Call([]reflect.Value{}) reflect.ValueOf(&t1).MethodByName( "Beginner1" ).Call([]reflect.Value{}) } |
Output:
GeekforBeginner Golang Package : reflect Call() Function