请教一个删除slice某个元素的问题

karl_zhao · · 2854 次点击
```go package main import "fmt" func DeleteElementInSlice(s []interface{},index int) []interface{}{ return append(s[:index],s[index+1:]...) } func DeleteOption(oldS []interface{},oldstr interface{})[]interface{}{ var idx int for i,v:=range oldS{ if v==oldstr{ idx=i break } } s1:=DeleteElementInSlice(oldS,idx) return s1 } func main(){ slice:=[]interface{}{"tom","hello","ok","love"} var str interface{}="hello" fmt.Println(DeleteOption(slice,str)) } ```
#4
更多评论
这个是泛型的列子,很遗憾go暂时不支持泛型,不考虑性能可以用反射实现,如果对性能敏感,可以使用unsafe包
#1