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) []Value
Parameters: 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:
package main
import "fmt"
import "reflect"
type T struct {}
func (t *T) Geeks() {
fmt.Println( "GeekforGeeks" )
}
func main() {
var t T
val := reflect.ValueOf(&t).MethodByName( "Geeks" ).Call([]reflect.Value{})
fmt.Println(val)
}
|
Output:
GeekforGeeks
[]
Example 2:
package main
import "fmt"
import "reflect"
type T struct {}
func (t *T) Geeks() {
fmt.Println( "GeekforGeeks" )
}
func (t *T) Geeks1() {
fmt.Println( "Golang Package :" )
fmt.Println( "reflect" )
fmt.Println( "Call() Function" )
}
func main() {
var t T
var t1 T
reflect.ValueOf(&t).MethodByName( "Geeks" ).Call([]reflect.Value{})
reflect.ValueOf(&t1).MethodByName( "Geeks1" ).Call([]reflect.Value{})
}
|
Output:
GeekforGeeks
Golang Package :
reflect
Call() Function
Last Updated :
03 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...