Ver código fonte

添加反射函数

yjp 6 meses atrás
pai
commit
dbb15ce42a
1 arquivos alterados com 26 adições e 0 exclusões
  1. 26 0
      reflectutils/value.go

+ 26 - 0
reflectutils/value.go

@@ -353,3 +353,29 @@ func ToFloat64(data any) (float64, error) {
 		return 0, errors.New("不支持的类型: " + dataVal.Type().Elem().String())
 	}
 }
+
+func Zero(v *reflect.Value) {
+	if v.Kind() == reflect.Ptr {
+		elemValue := PointerValueElem(*v)
+		zero(&elemValue)
+		v.Set(elemValue.Addr())
+	} else if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
+		v.Set(reflect.MakeSlice(v.Type(), 0, 0))
+	} else if v.Kind() == reflect.Map {
+		v.Set(reflect.MakeMap(v.Type()))
+	} else {
+		v.Set(reflect.New(v.Type()).Elem())
+	}
+}
+
+func zero(v *reflect.Value) {
+	if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
+		v.Set(reflect.MakeSlice(v.Type(), 0, 0))
+	} else if v.Kind() == reflect.Map {
+		v.Set(reflect.MakeMap(v.Type()))
+	} else if v.Kind() == reflect.Chan {
+		v.Set(reflect.MakeChan(v.Type(), 0))
+	} else {
+		v.Set(reflect.New(v.Type()).Elem())
+	}
+}