开发者

Remove element from vector.Vector

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

Is there a shorter way to remove an element from a vector?


Not really what you asked for, but do not use Vector, use a slice instead, see here for a summary of some slice-idioms and their (deprecated/discouraged) Vector equivalents.

You could do something like:

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

And obviously it is trivial to define your own delete method for your own types which does the same.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜