DEV Community


Posted on

Generics function as an argument of another function

type genericType Interface{
 type1 | type2 | type3
type genericFn[T genericType](req, resp T) T

func test[T genericType](fn genericFn[T]) {}

Enter fullscreen mode Exit fullscreen mode

appreciate guys do me a big favor.
I am wondering how to pass a function supporting generictype as an argument to another function?

I got problems when I passed a parameter with type1 among the genericType, then compiler told me type1 is not the type of genericType.

As I passed a function implemented genericFn, I cannot passed any argument in it.

does anybody know what’s going on or willing to share some use case?

thanks a lot !

Top comments (2)

winterxhrown profile image

sry for missing some syntax coz I typed with mobile.
thanks for your reply I’m going to check that !