-2

I'm trying to make http queries in parallel and then wait for all to finish:

  g1, _ := go http.Get('http://example.com/1')
  g2, _ := go http.Get('http://example.com/2')

  res1 := g1.wait() or { panic(err) }
  res2 := g2.wait() or { panic(err) }

  data1 := json.decode(ApiResult, res1.text) or { panic(err) }
  data2 := json.decode(ApiResult, res2.text) or { panic(err) }

I get the following error:

src/main.go:159:11: syntax error: unexpected go, expecting expression
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
chovy
  • 72,281
  • 52
  • 227
  • 295
  • 3
    `go` is a keyword, not a function call. It does not have return values. Launch a function as a goroutine which does all the things you want. Use a `sync.WaitGroup` to wait for all launched goroutines to finish. – icza Mar 04 '21 at 13:55

1 Answers1

5

The semantics of spawning a goroutine with go do not allow the automatic capture returned variables automatically. The idiomatic way to achieve that is to use a wait group or an err group.

For instance, code similar to this:

var group errgroup.Group

var resp1 *http.Response
group.Go(func() error {
   var err error
   resp1, err = http.Get('http://example.com/1')
   return err
}

var resp2 *http.Response
group.Go(func() error {
   var err error
   resp2, err = http.Get('http://example.com/2')
   return err
}

err := group.Wait()
if err != nil {
   ...
}

...

Note: resp1 & resp2 are able to be safely read after group.Wait() because they are set in the functions.

dolan
  • 1,716
  • 11
  • 22