A function has loop which call a go routine inside it with a channel passed into it. After this, I try receiving from channel till it has values.
The go function passes value in channel upon each call.
My channel runs infinitely.
func (m *StreamsDAO) FindOutput(input model.Input) ([]model.Output, error) {
// SOME CODE
var chanNumber int = (input.EndTime - input.StartTime)/60
outputChan := make(chan model.Output, chanNumber)
for i := input.StartTime; i < input.EndTime ;i = i+(slider*60) {
// SOME CODE
go ForEachSlide(i, outputChan)
for outputC := range outputChan {
outputs = append(outputs, outputC)
}
}
return outputs, err
}
func ForEachSlide(i int, outputChan chan model.Output) {
// SOME CODE
outputChan <- output
// close(outputChan)
}
Its looping infinite as there in no stop for receiving from channel. If close channel is used, only single element is received from channel thats as expected, but I need all values from the channel.
for range
construct over a channel receives all values that were sent on it before close. Then it terminates when the channel is closed.for range
terminates when all values are received that were sent before the channel was closed. If the channel is not closed, it will not terminate.for range
here is not terminating as its expecting more values in channel. I want this issue to be resolved, as soon as all values from channel are consumed,for range
should terminate or some other method that can do similar thing.