智慧食堂
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.

boolean.go 728 B

1 jaar geleden
1234567891011121314151617181920212223242526
  1. package utils
  2. import "reflect"
  3. // 检验一个值是否为空
  4. func Empty(val interface{}) bool {
  5. v := reflect.ValueOf(val)
  6. switch v.Kind() {
  7. case reflect.String, reflect.Array:
  8. return v.Len() == 0
  9. case reflect.Map, reflect.Slice:
  10. return v.Len() == 0 || v.IsNil()
  11. case reflect.Bool:
  12. return !v.Bool()
  13. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  14. return v.Int() == 0
  15. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  16. return v.Uint() == 0
  17. case reflect.Float32, reflect.Float64:
  18. return v.Float() == 0
  19. case reflect.Interface, reflect.Ptr:
  20. return v.IsNil()
  21. }
  22. return reflect.DeepEqual(val, reflect.Zero(v.Type()).Interface())
  23. }