Skip to content

Instantly share code, notes, and snippets.

@fedir
Last active March 13, 2019 16:00
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 fedir/5020c14036913845bf4619c368db4d5d to your computer and use it in GitHub Desktop.
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
// 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