go - 从 vector.Vector 中删除元素

标签 go

//Remove cl (*client) from clients (vector.Vector)
for i := 0; i < clients.Len(); i++ {
    if cl == clients.At(i).(*client) {
        clients.Delete(i)
        break
    }
}

有没有更短的方法从向量中删除元素?

最佳答案

不是您真正要求的,但不要使用 Vector,而是使用 slice ,see here for a summary of some slice-idioms and their (deprecated/discouraged) Vector equivalents .

你可以这样做:

for i, c := range clients {
    if c == client {
         clients = append(clients[:i], clients[i+1:]...)
    }
}

显然,为您自己的类型定义自己的 delete 方法是微不足道的。

关于go - 从 vector.Vector 中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4377414/

相关文章:

go - GOPATH 的正确设置是什么?

xml - 将子元素的属性直接解析为 Go struct

go - 如何定义 `const *DataType`

curl - 如何在 golang 中使用 curl 发送身份验证 token ?

go - 尝试实现死锁时使用零的无限循环

go - golang 中的 TCPListener : error when number of connections is above 60/260

mongodb - 确保 MongoDB 以动态时间间隔使数据过期并且调用是幂等的

json - 如何从 MongoDB 获取数据并将其作为 Golang 中的 JSON 发送到 API

go - 命名成员方法时如何在 "updateSomething"和 "setSomething"之间进行选择?

go - 如何将 httprouter.Handle 传递给 Prometheus http.HandleFunc