The last function in goroutine does not return a result

I can’t understand where the last value of the parseXMLResultFile function goes, the function itself is executed as many times as necessary, but the last execution it does not return a result, can someone explain what is wrong?

go func() {
for res := range resultsCh {
if res.stateProc == 0 {
host, err := parseXMLResultFile(res.fileScan) // This function does not return the last result, although the last execution is in progress
if err != nil {
log.Printf(“Parsing file not done: %s”, err)
results = append(results, host)

This more code, here is my pull request

Here’s a possible series of events:

  1. last result sent to channel: resultsCh <- newStateCmd
  2. main receives from result channel in for res := range resultsCh
  3. last call to defer wg.Done() from cmdDef completes the wait group.
  4. in main, wgAllCmd.Wait() completes and the results are printed.

The solution should be should be to defer wg.Done() in the anonymous goroutine function in main, not in cmdDef.

in a separate goroutine or in the one that I have already created in the mine? If I do defer wg.Done () in it, it does not terminate at all

I mean cut line 56, and paste it in between lines 104 and 105.

Oh, thank you, you helped me a lot, I forgot to remove it “defer”, because it is no longer needed there

Good point, you have to remove the defer.

:+1: Confused in Wait(), thanks again for the information.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.