Passing an arbitrary function as a parameter in Go

This is not possible. One reason for that is the mechanics of passing parameters differ from function to function, and using an interface{} arg does not mean “accept anything”. For example, a function taking a struct as an arg will receive each member of that struct, but a function taking an interface{} containing that struct will receive two words, one containing the type of the struct, and the other containing a pointer to it.

So, without using generics, the only way to implement this is by using an adapter function.

Leave a Comment