1 Star 0 Fork 0

地瓜2015 / go-utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
utils.go 3.11 KB
一键复制 编辑 原始数据 按行查看 历史
kai.zhang1 提交于 2021-11-28 23:10 . add storage
package utils
import (
"crypto/md5"
"crypto/sha512"
"encoding/hex"
"errors"
"fmt"
"io"
"math/rand"
"net"
"net/url"
"os"
"strings"
"time"
"unsafe"
"reflect"
"runtime"
"github.com/google/uuid"
"github.com/intel-go/cpuid"
)
// FuncName return func name
func FuncName(f interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}
// LocalIP return local ip
func LocalIP() (string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String(), nil
}
}
}
return "", errors.New("Can not find the client ip address")
}
// RandomStr random str
func RandomStr(len int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
bytes := make([]byte, len)
for i := 0; i < len; i++ {
b := r.Intn(26) + 97
bytes[i] = byte(b)
}
return string(bytes)
}
// RandomIP ip address
func RandomIP() string {
rand.Seed(time.Now().Unix())
return fmt.Sprintf("%d.%d.%d.%d", rand.Intn(255), rand.Intn(255), rand.Intn(255), rand.Intn(255))
}
// Md5 for string
func Md5(data string) string {
h := md5.New()
h.Write(Str2Bytes(data))
return hex.EncodeToString(h.Sum(nil))
}
// FileMd5 calc file md5
func FileMd5(file string) string {
f, err := os.Open(file)
defer f.Close()
if err != nil {
return ""
}
h := md5.New()
io.Copy(h, f)
return hex.EncodeToString(h.Sum(nil))
}
// Sha512 for string
func Sha512(data string) string {
h := md5.New()
h.Write(Str2Bytes(data))
return hex.EncodeToString(h.Sum(nil))
}
// FileSha512 calc file sha512
func FileSha512(file string) string {
f, err := os.Open(file)
defer f.Close()
if err != nil {
return ""
}
h := sha512.New()
io.Copy(h, f)
return hex.EncodeToString(h.Sum(nil))
}
// UUID unique id
func UUID() string {
return strings.Replace(uuid.New().String(), "-", "", -1)
}
// CPUID unique cpu id
func CPUID() string {
features := []string{}
for i := uint64(0); i < 64; i++ {
if cpuid.HasExtendedFeature(1 << i) {
features = append(features, cpuid.ExtendedFeatureNames[1<<i])
}
}
return Md5(strings.Join(features, " "))
}
// URLJoin join urls
func URLJoin(base, href string) string {
uri, err := url.Parse(href)
if err != nil {
return href
}
baseURL, err := url.Parse(base)
if err != nil {
return href
}
return baseURL.ResolveReference(uri).String()
}
// Exists check
func Exists(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
// IsDir check
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
// IsFile check
func IsFile(path string) bool {
return Exists(path) && !IsDir(path)
}
// ResetColor reset terminal color
func ResetColor() {
fmt.Printf("%c[0m", 0x1B)
}
// Str2Bytes convert string to byte
func Str2Bytes(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}
// Bytes2Str convert bytes to str
func Bytes2Str(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/zkdfbb/go-utils.git
git@gitee.com:zkdfbb/go-utils.git
zkdfbb
go-utils
go-utils
v0.2.0

搜索帮助

344bd9b3 5694891 D2dac590 5694891