Last active
March 13, 2019 16:00
-
-
Save fedir/5020c14036913845bf4619c368db4d5d to your computer and use it in GitHub Desktop.
golang > the fastest version of a slice of structs filtering will be a recreation of new slice of structs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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