想做一个通用的从slice中delete一个指定的元素,我是这样做的:
`func DeleteElementInSlice(sl []interface{}, idx int) []interface{} {`
` return append(sl[:idx], sl[idx+1:]...)`
`}`
可是我在使用的这个函数时不知道怎么使用了?
下面是我的调用示例,没法编译。
`func DeleteOption(optSl []string, oldStr string) []string {`
` var idx int`
` for i, v := range optSl {`
` if v == oldStr {`
` idx = i`
` break`
` }`
` }`
` sl := DeleteElementInSlice(optSl, idx)`
`}`
大佬们,指点一下吧
```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
更多评论