Basically I want to have an interface which has a SetParams
method, the declaration of which declares it receives one parameter, and the return type of the method, but leaves the return type of the parameter up to the receiver.
Something like this interface:
type ParamsInterface interface {
SetParams(<someType>) ParamsInterface
}
Now, when XStruct
implements it, the type of the parameters is XParams
type XStruct struct {
params XParams
}
func (x *XStruct) SetParams(params *XParams) ParamsInterface {
x.params = params
return x
}
But when YStruct
implements it, the type of the parameters is YParams
type YStruct struct {
params YParams
}
func (y *YStruct) SetParams(params *YParams) ParamsInterface {
y.params = params
return y
}
This is called generics, you can only use empty interface interface{}
and type casting.
This is doc about generics in go: https://docs.google.com/document/d/1vrAy9gMpMoS3uaVphB32uVXX4pi-HnNjkMEgyAHX4N4/edit#.
This also can help you:
How is the empty interface different than a generic?
This article suggests some ways how to live without generics: https://appliedgo.net/generics/
Here you can find code generator for generics (in my honest opinion that's not the best idea) https://github.com/cheekybits/genny.