SoFunction
Updated on 2025-03-05

An example tutorial for dynamically calling functions

Preface

This kind of scenario is often encountered during development. Several modules have the same method, but different module methods will be called due to different types. Using a switch can easily solve the problem. But when you encounter a scenario where several modules are executed, it needs to execute the corresponding methods in different configurations.

func m1(){}
func m2(){}
func m3(){}

c := (())
(config1,func(){
    m1()   
})
(config2,func(){
    m2()   
})
(config3,func(){
    m3()  
})

The above uses simple pseudo-code to illustrate that different timing tasks are executed at different timing times when executing timing tasks. However, when there are many timing tasks, a lot of repetitive code will be written, but the specific methods of the core call are different. Reading can also make people feel headaches.

JavaScript dynamically calls functions

When encountering the above scenario, you can think of a way to use dynamic calls to functions. However, in Go, it may not be as simple as dynamically calling functions when developed using JavaScript. Eval can be used in JavaScript. The eval function can execute parameter strings, that is, use the string as the function name and then directly call the function.

function m1(){}
function m2(){}
function m3(){}
funMap ={
config1:'m1'
config2:'m2'
config3:'m3'
}

((val)=>{
   evl(val+"()")    
})

Of course, JavaScript has many ways to support dynamic calls of functions. But today we are mainly introducing dynamic calling functions in Go.

Dynamic calling methods in Go

In Go, if you want to call functions dynamically, you need to implement them through reflection. And it should reflect a certain type and then obtain its related properties.

type my struct{}
func (m *my)m1(){}

//----main---
mname="m1"
funcs := (&my{})
f := (mname)
()

From the implementation of the above code, you can easily learn to use dynamic calls. The principle should be to reflect a certain type of reflection, then obtain the corresponding function through the function name of the string, and finally use the call method to call the method normally.

When a function is a method with parameters, we also need to pass function parameters when calling dynamically. You just need to put the parameters into the call function to pass the parameters normally.

params := make([],len(args))
 for i, _ := range args {
        params[i] = (args[i])
  }

mname="m1"
funcs := (&my{})
f := (mname)
(params)

The above is to implement dynamic call functions with parameters. Learn to call dynamically easily. Not only can you optimize the code, you don’t have to write a lot of duplicate code too cumbersomely, so you only need to add the set corresponding to the method name in the future. Then loop execution.

mapMd :=map[string]string{
    "key1":"m1"
    "key2":"m2"
}

funcs := (&my{})
for key, val := range mapMd {
    f := (key)
	()
}

It should be noted that this is for dynamic calling methods. Because they are bound to the corresponding structure, the properties and methods of the corresponding structure can be obtained through mapping.

This is the end of this article about the example tutorial on Go dynamic calling functions. For more related content on Go dynamic calling functions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!