I’m aware that generics cannot be used on receiver methods. Therefore I want to ask what you would suggest for the following usecase: Say we have client that can send data.
type Data struct{
content string
}
type Client struct {
}
func (client *Client) Send(d Data){
// ...
}
func doit(){
client := Client{}
data := Data{}
client.Send(data)
}
All fine so far. Now we want to enable the client to send data of multiple types. My first idea would be to make the data struct generic:
type Data[T any] struct{
content T
}
But this does not work because:
- I cannot use generic parameters in receiver methods.
- I cannot type the client as it shold be able to send arbitary messages.
The only solutions I see are:
- Dont use generics (specify multiple methods, data type as property on Data struct, …)
- Dont use receiver methods.
But both features I would not like to miss.
What is a proposed solution for that kind of use case?