Dynamic Function Calls
Calling functions and setting values via reflect
Dynamic Function Calls is a free Go Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.
reflect.Value.Call
Value.Call(args) calls the function represented by the Value with the given arguments (as a []reflect.Value) and returns the results as a []reflect.Value.
fn := reflect.ValueOf(add)
results := fn.Call([]reflect.Value{
reflect.ValueOf(2),
reflect.ValueOf(3),
})
fmt.Println(results[0].Int()) // 5Argument type checking
Ensure argument types match the function signature before calling. Mismatched types cause a panic.
t := reflect.TypeOf(fn)
if t.NumIn() != len(args) { panic("wrong arg count") }
for i, arg := range args {
if !reflect.TypeOf(arg).AssignableTo(t.In(i)) { panic("type mismatch") }
}Variadic functions
Call a variadic function with Value.Call (spreading the slice) or Value.CallSlice (passing the variadic args as a slice value):
sum := reflect.ValueOf(func(ns ...int) int {
total := 0
for _, n := range ns { total += n }
return total
})
args := reflect.ValueOf([]int{1,2,3})
results := sum.CallSlice([]reflect.Value{args})reflect.MakeFunc
reflect.MakeFunc creates a new function at runtime with a given type and a wrapper body. Used to generate adapter functions for interfaces.
t := reflect.TypeOf(func(int) string { return "" })
fn := reflect.MakeFunc(t, func(args []reflect.Value) []reflect.Value {
return []reflect.Value{reflect.ValueOf(strconv.Itoa(args[0].Interface().(int)))}
})Calling methods by name
Retrieve a method by name and call it:
v := reflect.ValueOf(myObj)
m := v.MethodByName("Process")
if m.IsValid() {
m.Call(nil)
}Checking if method exists
Always check m.IsValid() before calling — MethodByName returns the zero Value if the method is not found, and calling the zero Value panics.
Dynamic dispatch use cases
Dynamic function calls are used in: plugin systems (call functions by string name), test frameworks (call TestXxx functions), RPC dispatchers (map method names to handlers), and decorators.
Performance
reflect.Call is ~10-50× slower than a direct call. Cache the reflect.Value of frequently called functions; avoid in hot loops.
Type-safe wrapper
Wrap dynamic calls in a typed helper that performs the reflect machinery once during startup and calls the cached Value repeatedly:
type Processor func(context.Context, []byte) ([]byte, error)
// Build at startup, reuse at request timeError return handling
If the function returns an error as its last result, extract and check it:
results := fn.Call(args)
if !results[len(results)-1].IsNil() {
return results[len(results)-1].Interface().(error)
}Alternatives to reflection
If the set of functions is known at compile time, use a map of function values (map[string]func(...)) instead of reflection — it is safer and faster.
handlers := map[string]func(ctx context.Context, req []byte) []byte{
"greet": greetHandler,
}Quick Check
What does reflect.Value.Call return?
Recap: Dynamic Function Calls
Key points:
- Value.Call([]reflect.Value) invokes the function; returns []reflect.Value
- Check m.IsValid() before calling MethodByName result
- MakeFunc creates a new function with a given type at runtime
- Prefer map[string]func over reflection for known function sets
Learn Go with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Dynamic Function Calls” lesson free?
Yes — any 3 lessons of the Go Academy learning path, including “Dynamic Function Calls”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.
What will I learn in “Dynamic Function Calls”?
Calling functions and setting values via reflect You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
How long does the “Dynamic Function Calls” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- reflect.Type and reflect.Value
- Inspecting Struct Fields and Tags
- Dynamic Function Calls
- Practical Reflection: Serializers