github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/base/breflect/isnil.go (about)

     1  package breflect
     2  
     3  import (
     4  	"reflect"
     5  )
     6  
     7  func IsNil(value interface{}) bool {
     8  	if value == nil {
     9  		return true
    10  	}
    11  	v := reflect.ValueOf(value)
    12  	switch v.Kind() {
    13  	case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Slice:
    14  		return v.IsNil()
    15  	case reflect.Ptr:
    16  		elem := v.Elem()
    17  		if !elem.IsValid() {
    18  			return true
    19  		}
    20  		return IsNil(elem.Interface())
    21  	default:
    22  		return false
    23  	}
    24  }