-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
115 lines (99 loc) · 1.9 KB
/
utils.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package utils
import (
"errors"
"io/fs"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/google/uuid"
)
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if errors.Is(err, fs.ErrNotExist) {
return false, nil
}
return false, err
}
// 检查是否为URL
func IsURL(input string) bool {
u, err := url.Parse(input)
if err != nil {
return false
}
//存在协议,一定是url
if u.Scheme != "" {
return true
}
//没有Host,一定不是url
if u.Host == "" {
return false
}
//否则就一定是url
return true
}
func UUID() string {
return uuid.New().String()
}
func FileExt(filename string) string {
return filepath.Ext(filename)
}
func UUIDhex() string {
u := uuid.New().String()
uidWithoutDash := strings.ReplaceAll(u, "-", "")
return uidWithoutDash
}
func MergeSlices(slice1, slice2 []string) []string {
seen := make(map[string]bool)
result := make([]string, 0)
for _, item := range slice1 {
if item != "" {
seen[item] = true
result = append(result, item)
}
}
for _, item := range slice2 {
if item != "" && !seen[item] {
seen[item] = true
result = append(result, item)
}
}
return result
}
func SliceContain(slice []string, target string) bool {
for _, item := range slice {
if item == target {
return true
}
}
return false
}
func SliceContains(slice []string, targets []string) bool {
for _, target := range targets {
if !SliceContain(slice, target) {
return false
}
}
return true
}
func SliceRemove(sliceString []string, item string) []string {
result := []string{}
for _, s := range sliceString {
if item != s {
result = append(result, s)
}
}
return result
}
func SliceRemoves(sliceString []string, removeList []string) []string {
result := []string{}
for _, s := range sliceString {
if !SliceContain(removeList, s) {
result = append(result, s)
}
}
return result
}