We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 521f737 commit b3b6db8Copy full SHA for b3b6db8
slices/slices.go
@@ -148,12 +148,16 @@ func Contains[T any](s []T, fn func(T) bool) bool {
148
return IndexFunc(s, fn) >= 0
149
}
150
151
-// Copy returns a slice of V with the last n elements removed.
+// Copy copies first n elements. If n is greater than the length of the slice, it will copy the whole slice.
152
func Copy[S ~[]V, V any](s S, n int) S {
153
if s == nil {
154
return nil
155
156
157
+ if n > len(s) {
158
+ n = len(s)
159
+ }
160
+
161
result := make([]V, n)
162
copy(result, s)
163
0 commit comments