Generic helper to concatenate arbitrary numbers of slices

This commit is contained in:
Kovid Goyal
2023-06-24 09:13:20 +05:30
parent 41a0e60064
commit 2787f305f3

View File

@@ -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
}