Skip to content

Instantly share code, notes, and snippets.

@kjk
Last active June 5, 2020 04:51
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 kjk/0bd6cd3d4b2b20e9ebad85009c1a3f80 to your computer and use it in GitHub Desktop.
Save kjk/0bd6cd3d4b2b20e9ebad85009c1a3f80 to your computer and use it in GitHub Desktop.
// :glot
package main
import "fmt"
// :show start
func filterEvenValuesInPlace(a []int) []int {
// create a zero-length slice with the same underlying array
res := a[:0]
for _, v := range a {
if v%2 == 0 {
// collect only wanted values
res = append(res, v)
}
}
return res
}
func main() {
a := []int{1, 2, 3, 4}
res := filterEvenValuesInPlace(a)
fmt.Printf("%#v\n", res)
}
// :show end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment