Open In App

reflect.Call() Function in Golang with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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:




// Golang program to illustrate
// reflect.Call() Function
    
package main
  
import "fmt"
import "reflect"
  
type T struct {}
  
func (t *T) Geeks() {
    fmt.Println("GeekforGeeks")
}
  
func main() {
    var t T
      
    // use of Call() method
    val := reflect.ValueOf(&t).MethodByName("Geeks").Call([]reflect.Value{})
      
    fmt.Println(val)
}        


Output:

GeekforGeeks
[]

Example 2:




// Golang program to illustrate
// reflect.Call() Function
     
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
    // use of Call() method
    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
Previous
Next
Share your thoughts in the comments
Similar Reads