func remove(slice []interface{}, elem interface{}) []interface{}{ if len(slice) == 0 { return slice } for i, v := range slice { if v == elem { slice = append(slice[:i], slice[i+1:]...) return remove(slice,elem) break } } return slice } func
func remove(slice []interface{}, elem interface{}) []interface{}{
if len(slice) == 0 {
return slice
}
for i, v := range slice {
if v == elem {
slice = append(slice[:i], slice[i+1:]...)
return remove(slice,elem)
break
}
}
return slice
}
func removeZero(slice []interface{}) []interface{}{
if len(slice) == 0 {
return slice
}
for i, v := range slice {
if ifZero(v) {
slice = append(slice[:i], slice[i+1:]...)
return removeZero(slice)
break
}
}
return slice
}
//判断一个值是否为零值,只支持string,float,int,time 以及其各自的指针,"%"和"%%"也属于零值范畴,场景是like语句
func IfZero(arg interface{}) bool {
if arg == nil {
return true
}
switch v := arg.(type) {
case int, int32, int16, int64:
if v == 0 {
return true
}
case float32:
r:=float64(v)
return math.Abs(r-0)<0.0000001
case float64:
return math.Abs(v-0)<0.0000001
case string:
if v == "" || v == "%%" || v == "%" {
return true
}
case *string, *int, *int64, *int32, *int16, *int8, *float32, *float64, *time.Time:
if v == nil {
return true
}
case time.Time:
return v.IsZero()
default:
return false
}
return false
}