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.

34 lines
838 B

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