-
Notifications
You must be signed in to change notification settings - Fork 2
/
slice_int_to_string.go
88 lines (72 loc) · 1.4 KB
/
slice_int_to_string.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package slice
import (
"strconv"
"unsafe"
)
func SliceInt2String1(s []int) string {
if len(s) < 1 {
return ""
}
ss := strconv.Itoa(s[0])
for i := 1; i < len(s); i++ {
ss += "," + strconv.Itoa(s[i])
}
return ss
}
func SliceInt2String2(s []int) string {
if len(s) < 1 {
return ""
}
b := make([]byte, 0, 256)
b = append(b, strconv.Itoa(s[0])...)
for i := 1; i < len(s); i++ {
b = append(b, ',')
b = append(b, strconv.Itoa(s[i])...)
}
//return *(*string)(unsafe.Pointer(&b))
return string(b)
}
func SliceInt2String3(s []int) string {
if len(s) < 1 {
return ""
}
b := make([]byte, 0, 256)
b = append(b, strconv.Itoa(s[0])...)
for i := 1; i < len(s); i++ {
b = append(b, ',')
b = append(b, strconv.Itoa(s[i])...)
}
return *(*string)(unsafe.Pointer(&b))
}
func SliceInt2String4(s [][]int) []string {
res := make([]string, len(s))
for i, v := range s {
if len(v) < 1 {
res[i] = ""
continue
}
res[i] += strconv.Itoa(v[0])
for j := 1; j < len(v); j++ {
res[i] += "," + strconv.Itoa(v[j])
}
}
return res
}
func SliceInt2String5(s [][]int) []string {
res := make([]string, len(s))
b := make([]byte, 0, 256)
for i, v := range s {
if len(v) < 1 {
res[i] = ""
continue
}
b := b[:0]
b = append(b, strconv.Itoa(v[0])...)
for j := 1; j < len(v); j++ {
b = append(b, ',')
b = append(b, strconv.Itoa(v[j])...)
}
res[i] = string(b)
}
return res
}