-
Notifications
You must be signed in to change notification settings - Fork 1
/
drop.go
39 lines (30 loc) · 947 Bytes
/
drop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package array
import (
"reflect"
"github.com/warriors-vn/go-dash/constants"
)
// Drop returns a new array with `n` elements dropped from the beginning.
// The function accepts an array of any type (interface{}) and an optional number `num` of elements to Drop.
// It returns a new array with the specified number of elements removed from the beginning.
func Drop(array interface{}, num ...int) (interface{}, error) {
arrValue, n := reflect.ValueOf(array), 0
if arrValue.Kind() != reflect.Slice && arrValue.Kind() != reflect.Array {
return nil, constants.ErrNotSlice
}
if num != nil {
n = num[0]
} else {
return array, nil
}
if n == 0 || arrValue.Len() == 0 {
return array, nil
}
if n < 0 {
return nil, constants.ErrParamLessThanZero
}
result := reflect.MakeSlice(arrValue.Type(), 0, 0)
for i := n; i < arrValue.Len(); i++ {
result = reflect.Append(result, arrValue.Index(i))
}
return result.Interface(), nil
}