title: using Go Routines and Wait group
body: <p>I am trying to using Goroutine in my APIs. I need to check the method I am using is correct or whether it need any improvement .</p>
  <p>I am creating REST-APIs using go. using SQL-boiler also.</p>
  <p>In many of my APIs I am using some other functions like, Notification sending using AWS, Email Sending using AWS, Mailchimp integrations.</p>
  <p>Some APIS using these 3 features other than our database activity.</p>
  <p>So I found out it takes a time in API for displaying the result..  Then I found Goroutine and wait-group.</p>
  <p>Here I don't want to take  time for complete my apis. But I want to excecated the other functionality behind code.</p>
  <p>So I just checked and I am confused about goroutine and wait-group.
  When I using wait-group  in 3 functionality I understand that.. after completing all functionality api is giving response.</p>
  <p>But when using goroutine API is easily giving response and other functionalities are doing in background..</p>
  <p>using go routine API is taking less time than Waitgroup..</p>
  <p>Is this correct ?.
  The way I am approaching is correct or not ? Or anything is missing in my work flow ?</p>
  <pre><code>{
  if impartWealthIds != &quot;&quot; {
          impartWealthIds = strings.Trim(impartWealthIds, &quot;,&quot;)
          updateUser := fmt.Sprintf(`Update user 
          set deleted_at='%s' ,
           email=CONCAT(email, &quot;-&quot;, impart_wealth_id),
           screen_name=CONCAT(screen_name, &quot;-&quot;, impart_wealth_id),
           deleted_by_admin=true 
           where impart_wealth_id in(%s);
          `, golangDateTime, impartWealthIds)
  
          query = updateUser
      }
      _, err = queries.Raw(query).ExecContext(ctx, m.db)
      m.logger.Info(query)
      if err != nil {
          m.logger.Error(&quot;query failed&quot;, zap.Any(&quot;query&quot;, err))
          return err
      }
      go func() {
          for _, user := range userDetails {
              email := fmt.Sprintf(&quot;%s-%s&quot;, user.ImpartWealthID, user.Email)
              userUp := management.User{
                  Email: &amp;email,
              }
              err = mngmnt.User.Update(*&amp;user.AuthenticationID, &amp;userUp)
              if err != nil {
                  m.logger.Error(&quot;Auth update failed&quot;, zap.Any(&quot;user.Email&quot;, user.Email), zap.Any(&quot;query&quot;, err))
              }
          }
      }()
      go impart.UserDemographicsUpdate(ctx, m.db, true, true)
          if user.R.MemberHiveHives != nil {
              if user.R.MemberHiveHives[0].NotificationTopicArn.String != &quot;&quot; {
                  go func() {
                      err := m.notificationService.UnsubscribeTopicForAllDevice(ctx, user.ImpartWealthID, user.R.MemberHiveHives[0].NotificationTopicArn.String)
                      if err != nil {
                          m.logger.Error(&quot;SubscribeTopic&quot;, zap.String(&quot;DeviceToken&quot;, user.R.MemberHiveHives[0].NotificationTopicArn.String),
                              zap.Error(err))
                      }
                  }()
              }
          }
      go impart.UserDemographicsUpdate(ctx, m.db, true, true)
      return &quot;success&quot;
  }
  </code></pre>
  <p>here APi return success after database operation and other goroutines will be run in background</p>
  <p>I have an another double also</p>
  <p>if I am using a for-loop of users . and I need to Update some data for each user..</p>
  <pre><code>for _, user := range userDetails {
  // calling auth 0
  // calling mailchimp
  // calling aws
  }
  </code></pre>
  <p>I need to update data for each user</p>
  <p>then my question is which is the best method for using goroutine??</p>
  <p>method-1</p>
  <pre><code>for _, user := range userDetails {
          go calling auth 0
          go calling mailchimp
          go calling aws
  
          }
  </code></pre>
  <p>method - 2</p>
  <pre><code>  for _, user := range userDetails {
          go func(user *dbmodels.User) {
              // calling auth 0
              // calling mailchimp
              // calling aws
          }(user)
              }
  </code></pre>
  <p>which one is the best method?</p>