dongzang7182
dongzang7182
2014-08-28 06:11
浏览 25
已采纳

去通道以空结构作为输入。 这个输入可以是什么吗?

a := make(chan struct{})

Such this, go channel take empty struct as input. Can this input be anything?

thanks.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsvjw20866
    dsvjw20866 2014-08-28 06:12
    已采纳

    No: it can be an empty struct (struct{}{}), not "anything"

    To be "anything", you would need interface{}.

    As I explain in "Go Golang : anonymous struct and empty struct", an empty struct is useful to pass an object of size... 0! It is a good way to signal the completion that something has happened.

    点赞 评论
  • dtz55359
    dtz55359 2014-08-29 01:48

    struct{} is much like the unit type in other languages; it only has one value with no information. The values you get from such a channel would contain no information, but such a channel could be used for signalling when some event happened. A goroutine receiving from that channel would wake up whenever something is sent to it.

    点赞 评论

相关推荐