Home > Backend Development > Golang > How Can I Pass an Arbitrary Function as a Parameter in Go, Considering Type Compatibility Limitations?

How Can I Pass an Arbitrary Function as a Parameter in Go, Considering Type Compatibility Limitations?

Linda Hamilton
Release: 2025-01-03 09:16:39
Original
427 people have browsed it

How Can I Pass an Arbitrary Function as a Parameter in Go, Considering Type Compatibility Limitations?

Passing an Arbitrary Function as a Parameter in Go

Passing functions as parameters in Go allows for dynamic creation and execution of code. However, there are limitations to what types of functions can be passed.

Consider the following scenario: we want to create a decorator function that can wrap any function accepting one parameter and returning a single value. To handle functions accepting or returning interface{} types, we can define a decorator function with func(interface{}) interface{} as its argument.

However, it is not possible to convert a function of type func(string) string to a type of func(interface{}) interface{} implicitly. This is because the parameter and return type mechanics differ between functions. Passing an interface{} argument does not guarantee compatibility with all types.

For example, a function accepting a struct as an argument will receive individual members of that struct. In contrast, a function accepting an interface{} containing that struct will receive two words: one for the type and one for the pointer to the struct.

Solution

Without generics, there is no direct way to achieve this functionality. However, we can use an adapter function as a workaround:

// Adapter function for func(string) string to func(interface{}) interface{}
func adapter(inner func(string) string) func(interface{}) interface{} {
    return func(arg interface{}) interface{} {
        // Convert the interface{} arg to a string
        argString := arg.(string)
        // Call the inner function with the converted string
        result := inner(argString)
        // Return the result
        return result
    }
}
Copy after login

Now, we can pass funcB into the decorator using the adapter function:

fmt.Println(decorate(adapter(funcB), "(This is B's argument)"))
Copy after login

The above is the detailed content of How Can I Pass an Arbitrary Function as a Parameter in Go, Considering Type Compatibility Limitations?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template