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.
20 lines
406 B
20 lines
406 B
1 year ago
|
package misc
|
||
|
|
||
|
import "reflect"
|
||
|
|
||
|
// IsZero 泛型零值判断
|
||
|
// https://stackoverflow.com/questions/74000242/in-golang-how-to-compare-interface-as-generics-type-to-nil
|
||
|
func IsZero[T any](v T) bool {
|
||
|
return isZero(reflect.ValueOf(v))
|
||
|
}
|
||
|
|
||
|
func isZero(ref reflect.Value) bool {
|
||
|
if !ref.IsValid() {
|
||
|
return true
|
||
|
}
|
||
|
if ref.Type().Kind() == reflect.Ptr {
|
||
|
return isZero(ref.Elem())
|
||
|
}
|
||
|
return ref.IsZero()
|
||
|
}
|