Within a golang package I am writing, I often have to make 2 HTTP requests to get the data I require.
My package contains client functions, which usually have 0 arguments and return a struct and an error.
func (c Client) GetProduct() (*Product, error)
However, I would like to be able to write a generic function, which could accept two of these client functions as arguments, run them concurrently and simply return the structs filled with data from the API I am hitting.
Ideally, I would like the function call to look like this:
struct1, struct2, err := rrunFunctions(client.GetProduct, client.GetSizes)
So far I have written the following generic function: https://go.dev/play/p/IA8LJqY0FPe
The problem is that because GetProduct
and GetSizes
both return a struct
and error
rather than an interface{}
and error
I get the following error at compile time:
./prog.go:54:28: cannot use client.GetProduct (value of type func() (*Product, error)) as type func() (interface{}, error) in argument to runFunctions
./prog.go:54:47: cannot use client.GetSizes (value of type func() (*Sizes, error)) as type func() (interface{}, error) in argument to runFunctions
My question is how do I get past this? Is writing a function generic as this possible in go?
Any general tips on using concurrency in this manner would also be appreciated.