Go语言的切片相当于弹性数组,但是Slice自带的方法不多,进行Slice各类操作时,可以使用Lancet库提供的一些方法,增强Slice的功能
仓库地址GitHub - duke-git/lancet: A comprehensive, efficient, and reusable util function library of Go.
按指定条件删除Slice中的元素
_List = slice.DropWhile(_List , func(item Item) bool {
if item.KeyId== receiver.KeyId {
return true
}
return false
})
按指定条件过滤出Slice的元素
res := slice.Filter(_List, func(index int, item Item) bool {
if item.Token == token {
return true
}
return false
})
Lancet还提供UniqueBy去重逻辑、Unique直接去重,非常好用。