Skip to content

Instantly share code, notes, and snippets.

@ysqi
Created May 22, 2019 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ysqi/268d4fb866ef3e2366e8d3b58b143d6c to your computer and use it in GitHub Desktop.
Save ysqi/268d4fb866ef3e2366e8d3b58b143d6c to your computer and use it in GitHub Desktop.
fast remove item from slice
func clearExpiredTx(txs types.Transactions, now time.Time) types.Transactions {
for i := 0; i < len(txs); i++ {
if !txs[i].Expired(now) {
continue
}
if len(txs) == 1 {
return nil
}
var last *types.Transaction
for {
last = txs[len(txs)-1]
if !last.Expired(now) {
break
}
//remove last
txs[len(txs)-1] = nil
txs = txs[:len(txs)-1]
last = nil
if len(txs) == 0 {
break
}
}
if last == nil || len(txs)-1 < i {
return txs
}
txs[i] = last
txs[len(txs)-1] = nil
txs = txs[:len(txs)-1]
}
return txs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment