Go slices.DeleteFunc
Retain the stock bins that still contain items to pick.
A simple example
slices.DeleteFunc
- Start with 1, 2, 3
- Do this Remove values below 2
- Result 2, 3
Syntax
slices.DeleteFunc(counts, predicate)
Run these examples in Go
Save one example at a time as main.go. Use Go 1.21 or later.
go run main.goClear empty stock bins from the pick list
A stockroom prints only bins that contain items a picker can collect. Empty bins appear twice in today's count, and both should disappear while the remaining quantities keep their order.
package main
import (
"fmt"
"slices"
)
func main() {
counts := []int{4, 0, 2, 0}
counts = slices.DeleteFunc(counts, func(n int) bool { return n == 0 })
fmt.Printf("%d, %d\n", counts[0], counts[1])
}
4, 2
Both zeros are excluded and the positive counts stay in order.
Count packs on the delivery slip
A loading desk receives four item counts in sequence. Only positive quantities go to the packing desk. Which entries survive, in their original order?
Review print quantities
A print room records four job quantities, including a cancelled job. Only positive quantities go to the packing desk. Which entries survive, in their original order?
What to watch for
The predicate identifies elements to remove. Keep the returned slice to get the revised length.