Standard library / reflect

reflect.MakeFunc()

Creates a typed function from a callback that accepts and returns reflected values; callback result types must match the declared signature. Expected stdout: '12\n'.

Formal Syntax & Structure

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

Parameters & Attributes

NameTypeDescription
typTypeRuntime type descriptor for the new value.
fnfunc(args []Value) (results []Value)Callback receiving reflected arguments and returning reflected results.

Return Value

Value

Creates a typed function from a callback that accepts and returns reflected values; callback result types must match the declared signature.

Working Example

Example Snippetgo
package main
import (  "fmt"  "reflect")
func main() {  v := reflect.MakeFunc(reflect.TypeFor[func(int) int](), func(args []reflect.Value) []reflect.Value {    return []reflect.Value{reflect.ValueOf(int(args[0].Int()) * 2)}  })  f := v.Interface().(func(int) int)  fmt.Println(f(6))}
Go Reference