In (*Plm).masterControl(), there is:
for {
[...]
timeout := time.NewTicker(plmDelay) //if we hit this with no data received, we
//can assume the PLM has finished sending data.
defer timeout.Stop() // 1) Is this necessary? 2) is this local to the for{}?
select {
[...]
case <-timeout.C:
[...]
}
}
To answer #2, no, this is not local to the for{}. The Go spec says defer defers execution until the function returns (http://golang.org/ref/spec#Defer_statements), not merely the block scope ends.
The timeout.Stop() call is necessary, but unfortunately the way you're doing it here will cause a large backlog of time.Tickers to accumulate and keep running because the function never returns. For a one-shot timeout like this, what you really want to use is case <-time.After(plmDelay):.
In
(*Plm).masterControl(), there is:}
To answer #2, no, this is not local to the
for{}. The Go spec saysdeferdefers execution until the function returns (http://golang.org/ref/spec#Defer_statements), not merely the block scope ends.The
timeout.Stop()call is necessary, but unfortunately the way you're doing it here will cause a large backlog of time.Tickers to accumulate and keep running because the function never returns. For a one-shot timeout like this, what you really want to use iscase <-time.After(plmDelay):.