Lets jump straight into a code snippet.
type Animal[T any] struct {
Name string
Type string
Params T
}
type DogParams struct {
TailLength int
}
type CatParams struct {
MeowVolume int
}
type Dog = Animal[DogParams]
type Cat = Animal[CatParams]
type Tiger = Animal[CatParams]
var animals = []Animal[any]{
{
Name: "biggie doggie",
Type: "dog",
Params: DogParams{TailLength: 5},
},
{
Name: "persia",
Type: "cat",
Params: CatParams{MeowVolume: 2},
},
}
What is the best way to convert an animal from Animal[any]
to Animal[CatParams]
?
I have tried
cat, ok := any(animals[1]).(Cat)
and it's not ok.
Thanks in advance!