Skip to content

Instantly share code, notes, and snippets.

@fedir
Last active Mar 13, 2019
Embed
What would you like to do?
golang > the fastest version of a slice of structs filtering will be a recreation of new slice of structs
// ref. https://pauladamsmith.com/blog/2016/07/go-modify-slice-iteration.html
y := x[:0]
for _, n := range x {
if n.property !=42 {
y = append(y, n)
}
}
// If some further sorting should be done, requires Go 1.8+
// https://stackoverflow.com/a/42872183/634275
sort.Slice(y, func(i, j int) bool {
return y[i].ID < y[j].ID
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment