This is the code I have so far:
for {
select {
case event := <-events:
if event.Msg != nil && len(activeFilters) > 0 {
//filter for messages
matches := false
for k, v := range activeFilters {
if k == event.Msg.Namepace {
for _, code := range v {
if code != event.Msg.Code {
matches = true
break //BREAK1
}
}
if matches {
break //BREAK2
}
}
}
if !matches {
break //BREAK3
}
}
if err := sendEvent(event); err != nil {
sendErr(err)
return
}
case <-closed:
return
}
}
First, I am a beginner in Go. The above code generally looks too complex for me, I guess it can be optimized.
What I want to do: when an event arrives, I want to check if a given Code is inside a list of filters. The filters have two "dimensions": a namespace and a list of codes. activeFilters
is a map[string][]uint64
.
(Note: I am allowed to redesign activeFilters
to some other structure).
So I just want to prevent the code to execute sendEvent
if the filter doesn't match, while sendEvent
should be executed if the protocol matches any key in activeFilters
AND the message code is contained in the value array for that key.
So can the above code be simplified? Is the usage of break
(BREAK1 and BREAK2) correct? BREAK2 seems to be needed for me because the inner for
loop may already find a match, in which case there's no need to continue iterating on activeFilters
.
And finally, am I correct in using BREAK3? My thinking there is that if there is no match, I do not want to execute sendEvent
, so I want to jump out of the case
. In my limited Go
understanding, using return
there would close the channel or something like that....
:blush