While I am researching a piece of code, I noticed there is a “strange” syntax on declaring channel type structure field chan<-struct{}. I am wondering what is the meaning of that syntax?
type eofSignal struct {
data io.Reader
count uint32
eof chan<- struct{}
}
Hi. The empty struct is kind of placeholder value which uses no storage at all. So this channel is used to wait for a value to be written then end of file occurs but because the value isn’t important is the empty struct used.
One other usage I have seen is as a value in a map. Then the map is used like a set. You are only interested if the map has a certain key but the value is unimportant.