12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package common
- import (
- "bytes"
- "confrontation-training/constant"
- "encoding/binary"
- "encoding/hex"
- "fmt"
- "github.com/google/uuid"
- "log"
- "os"
- "path/filepath"
- "strings"
- )
- // GenerateUUID 生成UUID
- //Deprecated
- func GenerateUUID() string {
- UUID, err := uuid.NewRandom()
- if err != nil {
- _ = fmt.Sprintf("UUID generate err:%s", err.Error())
- return ""
- }
- return strings.Replace(UUID.String(), "-", "", -1)
- }
- // CheckFile 检查文件扩展名
- //Deprecated
- func CheckFile(fileName string) string {
- fileNames := []string{"json", "txt", "wav", "npy", "xlsx", "xls"}
- for _, temp := range fileNames {
- if strings.HasSuffix(strings.ToLower(fileName), temp) {
- return temp
- }
- }
- return ""
- }
- // GetCurrentAbPathByExecutable 获取当前执行程序所在的绝对路径
- //Deprecated
- func GetCurrentAbPathByExecutable() string {
- exePath, err := os.Executable()
- if err != nil {
- log.Fatal(err)
- }
- res, _ := filepath.EvalSymlinks(filepath.Dir(exePath))
- return res
- }
- func Int2Bytes(i int) [4]byte {
- var result [4]byte
- result[0] = byte(i >> 24 & 0xFF)
- result[1] = byte(i >> 16 & 0xFF)
- result[2] = byte(i >> 8 & 0xFF)
- result[3] = byte(i & 0xFF)
- return result
- }
- func Bytes2HexStr(bytes [4]byte) string {
- var result string
- for _, value := range bytes {
- if &value == nil {
- result = ""
- break
- }
- result = fmt.Sprint(result, value&0xFF)
- }
- return result
- }
- // GenerateHexStringCmdPlaceHolder 生成名称占位符
- func GenerateHexStringCmdPlaceHolder(length int) string {
- if length <= 0 {
- return ""
- }
- if length%2 != 0 {
- fmt.Println("占位符个数为奇数")
- }
- var sb strings.Builder
- for i := 0; i < length; i++ {
- sb.WriteString(constant.DefaultPlaceHolder)
- }
- return sb.String()
- }
- func Int2Byte(i int64) string {
- bytesBuffer := bytes.NewBuffer([]byte{})
- err := binary.Write(bytesBuffer, binary.BigEndian, i)
- if err != nil {
- fmt.Println("数据转换异常:" + err.Error())
- return ""
- }
- return hex.EncodeToString(bytesBuffer.Bytes())
- }
|