diff --git a/lib/slicesutil/slicesutil.go b/lib/slicesutil/slicesutil.go new file mode 100644 index 000000000..4ce954224 --- /dev/null +++ b/lib/slicesutil/slicesutil.go @@ -0,0 +1,10 @@ +package slicesutil + +// ExtendCapacity returns a with the capacity extended to len(a)+n if needed. +func ExtendCapacity[T any](a []T, n int) []T { + aLen := len(a) + if n := aLen + n - cap(a); n > 0 { + a = append(a[:cap(a)], make([]T, n)...) + } + return a[:aLen] +}