I am trying to wait for a specific condition, and I would like advice as to how this is done best. I have a struct that looks like this (simplified):
type view struct {
timeFrameReached bool
Rows []*sitRow
}
In a goroutine, I am updating a file, which is read into the view
variable. The number of rows increases, and timeFrameReached
will ultimately be true
.
Elsewhere, I want to wait for the following condition to be true:
view.timeFrameReached == true || len(view.Rows) >= numRows
I am trying to learn channels and how Go's condition variables work, and I would like to know what is the best solution here. Theoretically, I could do something trivial like this:
for {
view = getView()
if view.timeFrameReached == true || len(view.Rows) >= numRows {
break
}
}
but that is obviously a naive solution. The value of numRows
comes from an HTTP request, so the condition method seems challenging. The goroutine would not know when to broadcast the condition because it wouldn't know the number of rows it is looking for.