75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
package common
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/golang/protobuf/ptypes/timestamp"
|
|
)
|
|
|
|
// GenerateBytesUUID 生成一个byte数组的UUID
|
|
func GenerateBytesUUID() []byte {
|
|
uuid := make([]byte, 16)
|
|
_, err := io.ReadFull(rand.Reader, uuid)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("Error generating UUID: %s", err))
|
|
}
|
|
|
|
// variant bits; see section 4.1.1
|
|
uuid[8] = uuid[8]&^0xc0 | 0x80
|
|
|
|
// version 4 (pseudo-random); see section 4.1.3
|
|
uuid[6] = uuid[6]&^0xf0 | 0x40
|
|
|
|
return uuid
|
|
}
|
|
|
|
// GenerateUUID 生成一个string类型的生成UUID
|
|
func GenerateUUID() string {
|
|
uuid := GenerateBytesUUID()
|
|
return idBytesToStr(uuid)
|
|
}
|
|
|
|
// CreateUtcTimestamp 创建时间戳
|
|
func CreateUtcTimestamp() *timestamp.Timestamp {
|
|
now := time.Now().UTC()
|
|
secs := now.Unix()
|
|
nanos := int32(now.UnixNano() - (secs * 1000000000))
|
|
return &(timestamp.Timestamp{Seconds: secs, Nanos: nanos})
|
|
}
|
|
|
|
// idBytesToStr 将byte数组转为字符串
|
|
// 函数返回一个格式为 "xxxx-xx-xx-xx-xxxxxxxxxxxx" 的字符串,其中 "x" 表示一个十六进制数字
|
|
func idBytesToStr(id []byte) string {
|
|
return fmt.Sprintf("%x-%x-%x-%x-%x", id[0:4], id[4:6], id[6:8], id[8:10], id[10:])
|
|
}
|
|
|
|
// ConcatenateBytes 连接byte数组
|
|
func ConcatenateBytes(data ...[]byte) []byte {
|
|
finalLength := 0
|
|
for _, slice := range data {
|
|
finalLength += len(slice)
|
|
}
|
|
result := make([]byte, finalLength)
|
|
last := 0
|
|
for _, slice := range data {
|
|
for i := range slice {
|
|
result[i+last] = slice[i]
|
|
}
|
|
last += len(slice)
|
|
}
|
|
return result
|
|
}
|
|
|
|
// dirExists 判断一个路径是否存在
|
|
func dirExists(path string) bool {
|
|
fi, err := os.Stat(path)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return fi.IsDir()
|
|
}
|