如何仅使用一种方法删除不同类型的切片
How to delete different type of slice using only 1 methods
我有 2 个函数,如下所示
func removeL2McEntry(a []api.L2McEntry, index int) []api.L2McEntry {
a = append(a[:index], a[index+1:]...)
element
return a[:len(a)]
}
func removeVlagBasedGroup(a []api.VlanPortBased, index int) []api.VlanPortBased {
a = append(a[:index], a[index+1:]...)
return a[:len(a)]
}
如您所见,这两个函数都在做同样的工作。但我需要将它们分开,因为函数的输出和输入是不同类型的。
我试过:
func removeSlice(a interface{}, idx int) interface{} {
switch v := a.(type) {
case []string:
v = append(v[:idx], v[idx+1:]...)
fmt.Println("is ary", v)
return v[:len(v)]
case []int:
v = append(v[:idx], v[idx+1:]...)
fmt.Println("is ary", v)
return v[:len(v)]
default:
}
return nil
}
但是这种方式重复代码太多。
有什么办法可以让它只做一个功能,减少重复代码吗?
提前致谢。
正如 Adrian 指出的那样,从切片中删除一个元素通常是一行代码:
a = append(a[:i], a[i+1]...)
// or
a = a[:i+copy(a[i:], a[i+1:])]
真的不值得为它写一个函数,只需要在需要的地方使用这个代码片段。
如果您确实需要创建一个可以处理任何切片类型的函数,可以使用反射来创建。但是在使用它时,您将不得不对结果使用类型断言,因为该函数只能 return interface{}
的静态类型。它也比在您的具体切片值上使用上面的代码片段要慢!
以上删除步骤可以"reproduced"使用reflect
package. Slicing is the Value.Slice()
method, and the append operation is the reflect.AppendSlice()
函数。
这是它的样子(省略了类型和绑定检查):
func remove(s interface{}, i int) interface{} {
v := reflect.ValueOf(s)
return reflect.AppendSlice(v.Slice(0, i), v.Slice(i+1, v.Len())).Interface()
}
正在测试:
is := []int{0, 1, 2, 3}
is = remove(is, 2).([]int)
fmt.Printf("%#v\n", is)
ss := []string{"0", "1", "2", "3"}
ss = remove(ss, 2).([]string)
fmt.Printf("%#v\n", ss)
输出(在 Go Playground 上尝试):
[]int{0, 1, 3}
[]string{"0", "1", "3"}
但再次声明:我不建议任何人使用此(虽然有效)代码,只需使用原始代码段直接删除元素即可。
我有 2 个函数,如下所示
func removeL2McEntry(a []api.L2McEntry, index int) []api.L2McEntry {
a = append(a[:index], a[index+1:]...)
element
return a[:len(a)]
}
func removeVlagBasedGroup(a []api.VlanPortBased, index int) []api.VlanPortBased {
a = append(a[:index], a[index+1:]...)
return a[:len(a)]
}
如您所见,这两个函数都在做同样的工作。但我需要将它们分开,因为函数的输出和输入是不同类型的。
我试过:
func removeSlice(a interface{}, idx int) interface{} {
switch v := a.(type) {
case []string:
v = append(v[:idx], v[idx+1:]...)
fmt.Println("is ary", v)
return v[:len(v)]
case []int:
v = append(v[:idx], v[idx+1:]...)
fmt.Println("is ary", v)
return v[:len(v)]
default:
}
return nil
}
但是这种方式重复代码太多。 有什么办法可以让它只做一个功能,减少重复代码吗?
提前致谢。
正如 Adrian 指出的那样,从切片中删除一个元素通常是一行代码:
a = append(a[:i], a[i+1]...)
// or
a = a[:i+copy(a[i:], a[i+1:])]
真的不值得为它写一个函数,只需要在需要的地方使用这个代码片段。
如果您确实需要创建一个可以处理任何切片类型的函数,可以使用反射来创建。但是在使用它时,您将不得不对结果使用类型断言,因为该函数只能 return interface{}
的静态类型。它也比在您的具体切片值上使用上面的代码片段要慢!
以上删除步骤可以"reproduced"使用reflect
package. Slicing is the Value.Slice()
method, and the append operation is the reflect.AppendSlice()
函数。
这是它的样子(省略了类型和绑定检查):
func remove(s interface{}, i int) interface{} {
v := reflect.ValueOf(s)
return reflect.AppendSlice(v.Slice(0, i), v.Slice(i+1, v.Len())).Interface()
}
正在测试:
is := []int{0, 1, 2, 3}
is = remove(is, 2).([]int)
fmt.Printf("%#v\n", is)
ss := []string{"0", "1", "2", "3"}
ss = remove(ss, 2).([]string)
fmt.Printf("%#v\n", ss)
输出(在 Go Playground 上尝试):
[]int{0, 1, 3}
[]string{"0", "1", "3"}
但再次声明:我不建议任何人使用此(虽然有效)代码,只需使用原始代码段直接删除元素即可。