feat(utils): 新增查找、MD5及随机字符串工具
This commit is contained in:
1 parent
320d7c2e49
commit
ba8c1f1078
3 files changed
+43
No files matched your search
@@ -0,0 +1,11 @@
|
||||
package find
|
||||
|
||||
func InList[T comparable](list []T, key T) (ok bool) {
|
||||
for _, t := range list {
|
||||
if t == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package md5
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
)
|
||||
|
||||
func Md5WithFile(file io.Reader) string {
|
||||
m := md5.New()
|
||||
_, err := io.Copy(m, file)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
sum := m.Sum(nil)
|
||||
return hex.EncodeToString(sum)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package random
|
||||
|
||||
import "math/rand"
|
||||
|
||||
var letters = []rune("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
|
||||
func RandStr(n int) string {
|
||||
b := make([]rune, n)
|
||||
for i := range b {
|
||||
b[i] = letters[rand.Intn(len(letters))]
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
Reference in new issue
Block a user