Why the below doesn't work?
locations := make([]*LocationEvent, 0)
data := make([]Event, 0)
data = append(data, locations...)
where *LocationEvent
(struct) implements Event
(interface).
While the below works fine:
data = append(data, &LocationEvent{}, &LocationEvent{})
So how it is different when expanding the actual []*LocationEvent
slice using ...
?
The slice type must match the type of the variadic arguments in the append
function exactly. locations
is of type []*LocationEvent
, and thus not compatible with []Event
. There is no automatic "downcasting" in Go when working with slices.
You have to copy the locations to a new slice of Event
, or add the items of locations
one-by-one to the data
slice.
For more explanation look here: https://stackoverflow.com/a/12754757/6655315