From 2787f305f39ff7d7bfd8ecfa9b4b5216a072a7b0 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 24 Jun 2023 09:13:20 +0530 Subject: [PATCH] Generic helper to concatenate arbitrary numbers of slices --- tools/utils/misc.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tools/utils/misc.go b/tools/utils/misc.go index ec57e95a4..baba9f4ae 100644 --- a/tools/utils/misc.go +++ b/tools/utils/misc.go @@ -201,3 +201,16 @@ func Samefile(a, b any) bool { return os.SameFile(sta, stb) } + +func Concat[T any](slices ...[]T) []T { + var total int + for _, s := range slices { + total += len(s) + } + result := make([]T, total) + var i int + for _, s := range slices { + i += copy(result[i:], s) + } + return result +}