1

a := make(chan struct{})

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

thanks.

BufBills
  • 8,005
  • 12
  • 48
  • 90

2 Answers2

5

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

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.

Harold R. Eason
  • 563
  • 2
  • 9