You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package utils
  2. import (
  3. "os"
  4. "path/filepath"
  5. "reflect"
  6. "strings"
  7. )
  8. //@author: [songzhibin97](https://github.com/songzhibin97)
  9. //@function: FileMove
  10. //@description: 文件移动供外部调用
  11. //@param: src string, dst string(src: 源位置,绝对路径or相对路径, dst: 目标位置,绝对路径or相对路径,必须为文件夹)
  12. //@return: err error
  13. func FileMove(src string, dst string) (err error) {
  14. if dst == "" {
  15. return nil
  16. }
  17. src, err = filepath.Abs(src)
  18. if err != nil {
  19. return err
  20. }
  21. dst, err = filepath.Abs(dst)
  22. if err != nil {
  23. return err
  24. }
  25. var revoke = false
  26. dir := filepath.Dir(dst)
  27. Redirect:
  28. _, err = os.Stat(dir)
  29. if err != nil {
  30. err = os.MkdirAll(dir, 0755)
  31. if err != nil {
  32. return err
  33. }
  34. if !revoke {
  35. revoke = true
  36. goto Redirect
  37. }
  38. }
  39. return os.Rename(src, dst)
  40. }
  41. //@author: [songzhibin97](https://github.com/songzhibin97)
  42. //@function: TrimSpace
  43. //@description: 去除结构体空格
  44. //@param: target interface (target: 目标结构体,传入必须是指针类型)
  45. //@return: err error
  46. func TrimSpace(target interface{}) {
  47. t := reflect.TypeOf(target)
  48. if t.Kind() != reflect.Ptr {
  49. return
  50. }
  51. t = t.Elem()
  52. v := reflect.ValueOf(target).Elem()
  53. for i := 0; i < t.NumField(); i++ {
  54. switch v.Field(i).Kind() {
  55. case reflect.String:
  56. v.Field(i).SetString(strings.TrimSpace(v.Field(i).String()))
  57. }
  58. }
  59. return
  60. }