Avoid taking a pointer of the loop variable.
authorFlorian Forster <ff@octo.it>
Thu, 1 Feb 2018 07:45:23 +0000 (08:45 +0100)
committerFlorian Forster <ff@octo.it>
Thu, 1 Feb 2018 07:45:25 +0000 (08:45 +0100)
By taking a pointer of the loop variable, subsequent goroutines will get
their subscription data changed because the loop is continuing.

kraftakt.go

index e2318e7..53d1764 100644 (file)
@@ -295,20 +295,20 @@ func handleNotifications(ctx context.Context, payload []byte) error {
                switch s.CollectionType {
                case "activities":
                        wg.Add(1)
-                       go func() {
+                       go func(s fitbit.Subscription) {
                                defer wg.Done()
                                if err := activitiesNotification(ctx, &s); err != nil {
                                        log.Warningf(ctx, "activitiesNotification() = %v", err)
                                }
-                       }()
+                       }(s) // copies s
                case "sleep":
                        wg.Add(1)
-                       go func() {
+                       go func(s fitbit.Subscription) {
                                defer wg.Done()
                                if err := sleepNotification(ctx, &s); err != nil {
                                        log.Warningf(ctx, "sleepNotification() = %v", err)
                                }
-                       }()
+                       }(s) // copies s
                default:
                        log.Warningf(ctx, "ignoring collection type %q", s.CollectionType)