drjun1994 2013-09-15 08:05
浏览 56

为什么不能在函数参数中使用[] chan * Message类型作为[] chan接口{}类型?

This is the error message I am getting:

cannot use c.ReceiverChans (type []chan *Message) as type []chan interface {} in function argument

  • 写回答

1条回答 默认 最新

  • duanbozhong9689 2013-09-15 09:23
    关注

    The types are different. A *Message implements the empty interface, but that doesn't mean you can take a slice or chan of *Message and pass it to something that expects a slice or chan of interfaces.

    The way I think of interfaces as a particular data structure; a pair of a pointer to a value and a pointer to the underlying type. It's not exactly how interfaces work, but it helps my intuition. Using this intuition, if I pass an int, say, to a function that wants an interface{}, I imagine my value getting wrapped inside this interface pair implicitly by the compiler before the function's called. If instead, the function expects a []interface{}, and I want to pass a []int, what can the compiler do? It would have to construct a new array of the interface pairs, but then (a) that'd be expensive, and (b) it wouldn't really work, since if for example the slice got sorted, the original slice would be left alone.

    Here's the question in the golang FAQ: http://golang.org/doc/faq#convert_slice_of_interface

    Here's a more detailed explanation about slices of interfaces from the go wiki which explains better than I just did. https://code.google.com/p/go-wiki/wiki/InterfaceSlice

    评论

报告相同问题?

悬赏问题

  • ¥15 做数电题要具体的步骤
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真
  • ¥15 关于#c语言#的问题,请各位专家解答!
  • ¥15 这个如何解决详细步骤