From a7c719ed7ed8bdbd67ab528fda6de81b8383318d Mon Sep 17 00:00:00 2001 From: Vincent Boutour Date: Wed, 4 May 2022 23:44:18 +0200 Subject: [PATCH] refactor: Using shadowing for launching goroutine in a loop Signed-off-by: Vincent Boutour --- pkg/crud/list.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/pkg/crud/list.go b/pkg/crud/list.go index 0a6470db..497bd07f 100644 --- a/pkg/crud/list.go +++ b/pkg/crud/list.go @@ -67,19 +67,20 @@ func (a App) List(ctx context.Context, request provider.Request, message rendere }) for index, item := range files { - func(item absto.Item, index int) { - wg.Go(func() { - aggregate, err := a.exifApp.GetAggregateFor(ctx, item) - if err != nil { - logger.WithField("fn", "crud.List").WithField("item", item.Pathname).Error("unable to read: %s", err) - } + index := index + item := item + + wg.Go(func() { + aggregate, err := a.exifApp.GetAggregateFor(ctx, item) + if err != nil { + logger.WithField("fn", "crud.List").WithField("item", item.Pathname).Error("unable to read: %s", err) + } - renderItem := provider.StorageToRender(item, request) - renderItem.Aggregate = aggregate + renderItem := provider.StorageToRender(item, request) + renderItem.Aggregate = aggregate - items[index] = renderItem - }) - }(item, index) + items[index] = renderItem + }) } wg.Wait()