I am writing a DNS protocol parser in golang, the idea is to use a map like this
var tidMap map[uint16] (chan byte)
So for the
tidMap map, key is the tid (transaction ID), value is a byte array channel.
The idea is that a goroutine will try get value from the channel, another goroutine will try read bytes by listening every imcoming packet, and once found transaction ID, will set response data to the tidMap, so the former goroutine will continue handle the response.
One problem with the design is that I need the make sure the channel has buffer length of 1, so extra values can be pushed into channel without blocking.
So how can I specify channel buffer length in
var tidMap map[int] make(chan int, 1)
You can’t use
The length of the channel buffer doesn’t convey type, so you will have to add logic to test if the map entry exists, if it doesn’t:
tidMap = make(chan int, 1)
Answered By – Martin Gallagher
Answer Checked By – Jay B. (GoLangFix Admin)