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.

18 lines
510 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. }