Skip to content
Related Articles

Related Articles

Improve Article

reflect.MakeFunc() Function in Golang with Examples

  • Last Updated : 03 May, 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.MakeFunc() Function in Golang is used to get the new function of the given Type that wraps the function fn. To access this function, one needs to imports the reflect package in the program.

Syntax:

func MakeFunc(typ Type, fn func(args []Value) (results []Value)) Value

Parameters: This function takes the following parameters:

  • typ : This parameter is the Type.
  • fn : This parameter is the function func.

Return Value: This function returns a new function of the given Type that wraps the function fn.

Below examples illustrate the use of the above method in Golang:



Example 1:




// Golang program to illustrate
// reflect.MakeFunc() Function
   
package main
   
import (
    "fmt"
    "reflect"
)
  
func InvertSlice(args []reflect.Value) (result []reflect.Value) {
    inSlice, n := args[0], args[0].Len()
    outSlice := reflect.MakeSlice(inSlice.Type(), 0, n)
    for i := n-1; i >= 0; i-- {
        element := inSlice.Index(i)
        outSlice = reflect.Append(outSlice, element)
    }
    return []reflect.Value{outSlice}
}
  
func Bind(p interface{}, f func ([]reflect.Value) []reflect.Value) {
  
    invert := reflect.ValueOf(p).Elem()
      
    //Use of MakeFunc() method
    invert.Set(reflect.MakeFunc(invert.Type(), f))
}
  
   
// Main function
func main() {
  
     var invertInts func([]int) []int
    Bind(&invertInts, InvertSlice)
    fmt.Println(invertInts([]int{1, 2, 3, 4, 2, 3, 5}))
  
}

Output:

[5 3 2 4 3 2 1]

Example 2:




// Golang program to illustrate
// reflect.MakeFunc() Function
   
package main
   
import (
    "fmt"
    "reflect"
)
  
func sum(args []reflect.Value) []reflect.Value {
    a, b := args[0], args[1]
    if a.Kind() != b.Kind() {
        fmt.Println("??? ????.")
        return nil
    }
  
    switch a.Kind() {
    case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
        return []reflect.Value{reflect.ValueOf(a.Int() + b.Int())}
    case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
        return []reflect.Value{reflect.ValueOf(a.Uint() + b.Uint())}
    case reflect.Float32, reflect.Float64:
        return []reflect.Value{reflect.ValueOf(a.Float() + b.Float())}
    case reflect.String:
        return []reflect.Value{reflect.ValueOf(a.String() + b.String())}
    default:
        return []reflect.Value{}
    }
}
  
func makeSum(fptr interface{}) {
    fn := reflect.ValueOf(fptr).Elem()
  
    v := reflect.MakeFunc(fn.Type(), sum)
  
    fn.Set(v)
}
   
// Main function
func main() {
  
     var intSum func(int, int) int64
    var floatSum func(float32, float32) float64
    var stringSum func(string, string) string
  
    makeSum(&intSum)
    makeSum(&floatSum)
    makeSum(&stringSum)
  
    fmt.Println(intSum(1, 2))
    fmt.Println(floatSum(2.1, 3.5))
    fmt.Println(stringSum("Geeksfor", "Geeks"))
  
}

Output:

3
5.599999904632568
GeeksforGeeks



My Personal Notes arrow_drop_up
Recommended Articles
Page :