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.

29 lines
763 B

7 years ago
  1. package common
  2. import "reflect"
  3. // Go lacks a simple and safe way to see if something is a typed nil.
  4. // See:
  5. // - https://dave.cheney.net/2017/08/09/typed-nils-in-go-2
  6. // - https://groups.google.com/forum/#!topic/golang-nuts/wnH302gBa4I/discussion
  7. // - https://github.com/golang/go/issues/21538
  8. func IsTypedNil(o interface{}) bool {
  9. rv := reflect.ValueOf(o)
  10. switch rv.Kind() {
  11. case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice:
  12. return rv.IsNil()
  13. default:
  14. return false
  15. }
  16. }
  17. // Returns true if it has zero length.
  18. func IsEmpty(o interface{}) bool {
  19. rv := reflect.ValueOf(o)
  20. switch rv.Kind() {
  21. case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String:
  22. return rv.Len() == 0
  23. default:
  24. return false
  25. }
  26. }