I have a process with a number of stages that need to be completed in sequence. Each stage is largely parallelisable, involving looping over a large data structure and processing each item independently, and collating the results into the data structure used by the next stage.My approach is like the following:var wg sync.WaitGroupvar control_routine sync.WaitGroupstage_complete = make(chan bool)stage_one_channel := make(chan map[typeFoo][]typeBar, 16)stage_two_channel := make(chan map[typeBaz][]typeQux, 16)stage_one_results := make(map[typeFoo][]typeBar)stage_two_results := make(map[typeBaz][]typeQux)// A routine to receive results as they arrive from each stagego func() {    for  {        select {        case new_stage_one_results := <- stage_one_channel:            // merge new_stage_one_results into stage_one_results...        case new_stage_two_results := <- stage_two_channel:            // merge new_stage_two_results into stage_two_results...        ...        case stay_alive := <-stage_complete:            control_routine.Done()            if !stay_alive {                return            }        }    }}()// Begin stage Onecontrol_routine.Add(1)for i, some_data := range my_data {    wg.Add(1)    go func(i, datum){        defer wg.Done()        some_stage_one_results = make(map[typeFoo][]typeBar)        // do some heavy lifting with some_data which populates some_stage_one_results...        stage_one_channel <- some_stage_one_results    }()}wg.Wait()stage_complete <- truecontrol_routine.Wait()// End stage one// More stages much like the first...// Finally send the control routine a terminal signal and wait for it to finishstage_complete <- falsecontrol_routine.Wait()This basically seems to work, but I need to be sure of the following assumption (which some behaviour is causing me to doubt). Can I be sure that all go routines in stage N will completely finish and the data sent through stage_N_channel will all be processed in the control routine before the stage_complete channel is read from?What I'm seeing is that sometimes, it looks like one or two results from a stage go missing, as if the control routine were moving on before processing them. Adding a one second sleep to the main routine after wg.Wait(), seems to help but I'm not sure is this is right.Is there anything else unusual or potentially dangerous about this pattern? Is there another pattern I should consider using?I finally settled on a solution where each stage resembles the following.for i, some_data := range my_data {  wg.Add(1)  go func(i, some_data){    // stage logic    ...    stage_one_channel <- some_stage_one_results  }(i, some_data)}go func() {    wg.Wait()    close(stage_one_channel)}()for new_stage_one_data := <- stage_one_channel {  // integrate received data  ...  wg.Done()}
Is this a good way of managing parallel go routines when I care about ordering of results?
concurrency;go
The answer to Can I be sure...? is no, there is nothing in your code that ensures that.  That is why results sometimes go missing.Missing results are a sign of a data race.  You can use the Go race detector to find and fix these problems.  Data races are categorically dangerous.  They can corrupt memory.You might be able to find and fix the problem by adding more synchronization, but your code is too complex as it is.  Since you asked, a pattern you should probably be using is a particular producer-consumer pattern.  It has two parts, 1) a producer goroutine that sends data to a channel and closes the channel after sending the last value, and 2) a consumer goroutine with a for loop that ranges over the channel.  With this pattern you should be able to dispense with the select statement, the stage complete channel, and the control routine wait group.Your code that begins for i, some_data := range my_data is what I'm calling a producer.  It produces data on the channel stage_one_channel.  Following wg.Wait(), you should replace the line stage_complete <- true with close(stage_one_channel).Replace your for loop containing the select statement with a separate for loop for each stage, something likefor new_stage_one_results := range stage_one_channel {    ...}When this for loop completes, you are sure that all data has been received from stage_one_channel and processed by the loop body.  This is the synchronization your code is missing.  Your current mechanism only waits for data to be sent, not received and processed.When the last loop completes, your program can exit.  You could retain some of your mechanism for waiting for this, but simpler would be swap what you currently have in your main goroutine for the consumer code that you currently have in that first function literal.  That is, run your producers in one or more separate goroutines so you can have your consumers--the last one especially--in main.  When the last consumer has processed the last data, you know your program is done, with nothing else to wait for.Finally, let me reinforce, if go run -race says you have a problem, you have a problem.