utils.go 875 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package common
  2. import (
  3. "fmt"
  4. "github.com/google/uuid"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. )
  10. // GenerateUUID 生成UUID
  11. func GenerateUUID() string {
  12. UUID, err := uuid.NewRandom()
  13. if err != nil {
  14. _ = fmt.Sprintf("UUID generate err:%s", err.Error())
  15. return ""
  16. }
  17. return strings.Replace(UUID.String(), "-", "", -1)
  18. }
  19. // CheckFile 检查文件扩展名
  20. func CheckFile(fileName string) string {
  21. fileNames := []string{"json", "txt", "wav", "npy", "xlsx", "xls", "sql"}
  22. for _, temp := range fileNames {
  23. if strings.HasSuffix(strings.ToLower(fileName), temp) {
  24. return temp
  25. }
  26. }
  27. return ""
  28. }
  29. // GetCurrentAbPathByExecutable 获取当前执行程序所在的绝对路径
  30. func GetCurrentAbPathByExecutable() string {
  31. exePath, err := os.Executable()
  32. if err != nil {
  33. log.Fatal(err)
  34. }
  35. res, _ := filepath.EvalSymlinks(filepath.Dir(exePath))
  36. return res
  37. }