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.

38 lines
909 B

  1. // 空值校验工具 仅用于检验空字符串 其余类型请勿使用
  2. package utils
  3. import (
  4. "errors"
  5. "fmt"
  6. "reflect"
  7. )
  8. func HasGap(input interface{}) error {
  9. getType := reflect.TypeOf(input)
  10. getValue := reflect.ValueOf(input)
  11. // 获取方法字段
  12. for i := 0; i < getType.NumField(); i++ {
  13. field := getType.Field(i)
  14. value := getValue.Field(i).Interface()
  15. switch value.(type) {
  16. case string:
  17. if value == "" {
  18. fmt.Printf("%s为空", field.Name)
  19. return errors.New(fmt.Sprintf("%s为空", field.Name))
  20. }
  21. default:
  22. if value == nil {
  23. fmt.Printf("%s为空", field.Name)
  24. return errors.New(fmt.Sprintf("%s为空", field.Name))
  25. }
  26. }
  27. }
  28. // 获取方法
  29. // 1. 先获取interface的reflect.Type,然后通过.NumMethod进行遍历
  30. //for i := 0; i < getType.NumMethod(); i++ {
  31. // m := getType.Method(i)
  32. // fmt.Printf("%s: %v\n", m.Name, m.Type)
  33. //}
  34. return nil
  35. }