From 1008dd4956fde96eecdf3eee50a5fd27d4dac47e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=87=95=E5=BD=92=E6=9D=A5?= Date: Fri, 18 Oct 2024 15:09:28 +0800 Subject: [PATCH] fix: fix compile error (#255) (#258) --- slice/slice_concurrent.go | 8 ++------ slice/slice_internal.go | 7 +++++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/slice/slice_concurrent.go b/slice/slice_concurrent.go index 6216739..4b3746a 100644 --- a/slice/slice_concurrent.go +++ b/slice/slice_concurrent.go @@ -195,18 +195,14 @@ func UniqueByConcurrent[T comparable](slice []T, comparator func(item T, other T chunks = append(chunks, slice[i:end]) } - type resultChunk struct { - index int - data []T - } - resultCh := make(chan resultChunk, numThreads) + resultCh := make(chan resultChunk[T], numThreads) var wg sync.WaitGroup for i, chunk := range chunks { wg.Add(1) go func(index int, chunk []T) { defer wg.Done() - resultCh <- resultChunk{index, removeDuplicate(chunk, comparator)} + resultCh <- resultChunk[T]{index, removeDuplicate(chunk, comparator)} }(i, chunk) } diff --git a/slice/slice_internal.go b/slice/slice_internal.go index 95fde19..f6c275e 100644 --- a/slice/slice_internal.go +++ b/slice/slice_internal.go @@ -7,6 +7,13 @@ import ( "golang.org/x/exp/constraints" ) +// resultChunk is used to store the intermediate results of UniqueByConcurrent. +// It is defined separately to be compatible with versions of go up to 1.20. +type resultChunk[T comparable] struct { + index int + data []T +} + // sliceValue return the reflect value of a slice func sliceValue(slice any) reflect.Value { v := reflect.ValueOf(slice)