Converting slice of structs to slice of empty interface

前端 未结 1 1751
既然无缘
既然无缘 2020-12-19 17:08

I\'m trying to assign a slice of structs to a slice []interface{} (to pass into AppEngine\'s datastore.PutMulti. However, this is causing compilati

1条回答
  •  南方客
    南方客 (楼主)
    2020-12-19 18:09

    You're going to have to copy one-at-a-time. There's no way around it.

    If it helps to accept this, you should think about the fact that wrapping a struct in an interface really does actually wrap it at the memory level. An interface contains a pointer to the original type and a descriptor for the type itself. When casting a single struct to an interface, you're really wrapping it. So copying them one-at-a-time is necessary in order to wrap the structs up in the interface.

    0 讨论(0)
提交回复
热议问题