I have an interface like this
type A interface {
SomeMethod()
}
I have an implementation of this interface via a struct pointer:
type Aimpl struct {}
func (a *Aimpl) SomeMethod() {}
I have a generic function that takes in a function with an A parameter like so:
func Handler[T A](callback func(result T)) {
// Essentially what I'd like to do is result := &Aimpl{} (or whatever T is)
callback(result)
}
Also I should add that I cannot modify the definition of A (it's coming from a library). I've tried this:
type MyA[T any] interface{
A
*T
}
But this code gives me an error:
func Handler[P any, T MyA[P]](callback func(result A)) {
result := new(P)
callback(result) // result does not implement interface. It's a pointer to a type, not a type
}
Example here: https://go.dev/play/p/NVo4plR1R-O