从切片中删除元素不起作用

问题描述 投票:0回答:1

我想从切片中删除某些元素,但是它不起作用。

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]。有人告诉我原因?

arrays go slice
1个回答
1
投票

root

© www.soinside.com 2019 - 2024. All rights reserved.