Channel Structure Field Declaration

Hi there,

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{}
}

Any help is appreciated. Thank You.

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.

Read more here: “How to improve your GO code with empty structs” by Lorenzo Peppoloni https://link.medium.com/6SJrTMLfCU

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.