我想从切片中删除某些元素,但是它不起作用。
package main
import (
"fmt"
)
func main() {
a := []string{"a", "b", "c"}
for _, command := range a {
if command == "a" || command == "b" || command == "c" {
a = deleteSlice(a, command)
}
}
fmt.Println(a)
}
func deleteSlice(strings []string, str string) []string {
out := strings[:0]
for _, s := range strings {
if s != str {
out = append(out, s)
}
}
return out
}
预期结果是[]
,但实际上是[b]
。有人告诉我原因?
root