So I've been reading these two articles and this answer
Cannot convert []string to []interface {} says that the memory layout needs to be changed.
http://jordanorelli.com/post/32665860244/how-to-use-interfaces-in-go says that understanding the underlying memory makes answering this question easy, and
http://research.swtch.com/interfaces, explains what is going on under the hood.
But for the life of me I can't think of a reason, in terms of the implementation of interfaces as to why []T cannot be cast to []interface.
So Why?
Read the blog article The Laws of Reflection, section The representation of an interface.
So if you have a value of
[]T
(a slice ofT
) whereT
is not an interface, the elements of such a slice only stores values of typeT
, but it does not store the type information, it belongs to the slice type.If you have a value of type
[]inteface{}
, the elements of such a slice holds the concrete values and the type descriptors of those values.So elements in a
[]interface{}
require more info (more memory) than in a non-interface[]T
. And if the occupied memory of those 2 slices are not the same, they cannot be just "looked at" differently (looked at as a differnet type). Producing one from the other requires additional work.The article "InterfaceSlice" try to detail:
See also "what is the meaning of
interface{}
in golang?""why
[]string
can not be converted to[]interface{}
in Go" adds a good illustration: