-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
120 lines (108 loc) · 2.37 KB
/
common.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
116
117
118
119
120
package GoLib
import (
"fmt"
"math/rand"
"net"
"os"
"strconv"
"strings"
"time"
)
//获取主域名url
func GetDomainUrl(url string) string {
//https://www.baidu.com/index.php?a=1
//黑名单域名
blackDomain := []string{"syulmd.com"}
for _, b := range blackDomain {
if strings.Contains(url, b) {
return ""
}
}
blackDomain = nil
u := strings.Split(url, "/")
if len(u) >= 3 {
return u[0] + "//" + strings.TrimSpace(u[2]) + "/"
}
return ""
}
//从Url中获取主域名
func GetDomain(url string) string {
//https://www.baidu.com/index.php?a=1
return strings.Split(url, "/")[2]
}
//处理url
func DealUrl(url string) string {
var result []string
u := strings.Split(url, "/")
result = append(result, u[0:3]...)
result[0] = u[0]
result[1] = u[1]
result[2] = u[2]
for i := 3; i < len(u); i++ {
if strings.TrimSpace(u[i]) != "" {
result = append(result, u[i])
}
}
return strings.Join(result, "/")
}
// 字符串数组去重
func RemoveDuplicateElement(languages []string) []string {
result := make([]string, 0, len(languages))
temp := map[string]struct{}{}
for _, item := range languages {
if _, ok := temp[item]; !ok {
temp[item] = struct{}{}
result = append(result, item)
}
}
return result
}
//判断字符串数组中是否存在字符串,flag为是否大小写敏感,1就是敏感,0就是不敏感
func StrInList(str string, strList []string, flag bool) bool {
for _, v := range strList {
if flag {
str = strings.ToLower(str)
v = strings.ToLower(v)
}
if str == v {
return true
}
}
return false
}
//获取随机字符串
func RandString(length int) string {
var strByte = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
var strByteLen = len(strByte)
bytes := make([]byte, length)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < length; i++ {
bytes[i] = strByte[r.Intn(strByteLen)]
}
return fmt.Sprintf("%s", bytes)
}
//判断文件是否存在
func FileExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
return false
}
//判断是否为ip
func IsIp(ip string) bool {
address := net.ParseIP(ip)
if address == nil {
return false
}
return true
}
//字符串转int
func Str2Int(text string) int {
d, _ := strconv.ParseInt(text, 10, 64)
return int(d)
}
// 整数转字符串
func Int2Str(num int) string {
return fmt.Sprintf("%d", num)
}