Skip to content

Instantly share code, notes, and snippets.

@ta1kt0me
Created January 22, 2017 07:59
Show Gist options
  • Select an option

  • Save ta1kt0me/22609870436dab39cd35a5e6d2da28d7 to your computer and use it in GitHub Desktop.

Select an option

Save ta1kt0me/22609870436dab39cd35a5e6d2da28d7 to your computer and use it in GitHub Desktop.
Create slice filtering some condition from array
package main
import "fmt"
func main() {
s := []int{2, 3, 5, 7, 11, 13}
printSlice(s)
// https://github.com/golang/go/wiki/SliceTricks#filtering-without-allocating
ns := s[:0]
for _, v := range s {
if v % 3 == 2 {
ns = append(ns, v)
}
}
printSlice(ns)
}
func printSlice(s []int) {
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment