Q: Should I cast a slice of
myint
to a slice ofStringer
then?
There are no casts in Go - only conversions. But conversion from a slice of concrete, non interface type to a slice of interface type is not possible, except by doing it explicitly in a loop. The reason is that, as for example in your case, the []myint
backing array has a different memory layout than []fmt.Stringer
has. (Related FAQ)
On a side note, you don't have to write:
parts := []myint{myint(1), myint(5), myint(6)}
This should work identically:
parts := []myint{1, 5, 6}