-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.go
55 lines (51 loc) · 1.28 KB
/
array.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package gotool
import (
"reflect"
"strconv"
)
// ArrayDuplicate 数组去重
//
// elems 传入同种类型数组
func ArrayDuplicate[T int64 | int32 | int16 | int8 | int | string](elems ...[]T) []T {
var ans = make([]T, 0)
var m = make(map[T]struct{})
for _, elem := range elems {
for _, v := range elem {
if _, ok := m[v]; !ok {
ans = append(ans, v)
}
m[v] = struct{}{}
}
}
return ans
}
// ArrayIn 数组是否包含某个值
//
// target 目标值
// arr 待遍历的数组
func ArrayIn[T int64 | int32 | int16 | int8 | int | uint | uint8 | uint16 | uint32 | uint64 | string |
bool | float64 | float32](target T, arr []T) bool {
for _, v := range arr {
if target == v {
return true
}
}
return false
}
// ArrayJoin 整型拼接
//
// elems 待拼接的数值
// sep 拼接用的字符串
func ArrayJoin[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64](elems []T, sep string) string {
var ans string
for _, elem := range elems {
switch reflect.TypeOf(elem).Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
ans = ans + strconv.Itoa(int(elem)) + sep
}
}
if ans == "" {
return ""
}
return ans[:len(ans)-len(sep)]
}