-
Notifications
You must be signed in to change notification settings - Fork 0
/
slice.go
59 lines (52 loc) · 1.06 KB
/
slice.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
56
57
58
59
package util
import (
"bytes"
)
func UniqueIntSlice(values []int) []int {
m := map[int]bool{}
newSlice := make([]int, 0, len(values))
if values != nil && len(values) > 0 {
for _, v := range values {
if _, existed := m[v]; !existed {
newSlice = append(newSlice, v)
m[v] = true
}
}
} else {
newSlice = values
}
return newSlice
}
func UniqueInt64Slice(values []int64) []int64 {
m := map[int64]bool{}
newSlice := make([]int64, 0, len(values))
if values != nil && len(values) > 0 {
for _, v := range values {
if _, existed := m[v]; !existed {
newSlice = append(newSlice, v)
m[v] = true
}
}
} else {
newSlice = values
}
return newSlice
}
func RepeatString(times int, s, split string) string {
buf := bytes.NewBufferString("")
for times > 0 {
buf.WriteString(s)
times -= 1
if times > 0 {
buf.WriteString(split)
}
}
return buf.String()
}
func StringS2Interface(slice []string) []interface{} {
interfaces := make([]interface{}, 0)
for _, s := range slice {
interfaces = append(interfaces, s)
}
return interfaces
}