Johnny.sh

GOLANG SLICE TRIX

Golang’s slice primitive does not have a bunch of nice built-in functions like Javascript’s array. So, we have to come up with our own little tricks to make some things a bit easier.

Mostly siphoned from: Golang slice cheat sheet

Filter a slice

n := 0
for _, x := range a {
 if keep(x) {
  a[n] = x
  n++
 }
}
a = a[:n]

Merge Slices

If b is also a slice that you want to spread into a

a = append(a, b...)
Last modified: October 04, 2022
/about
/uses
/notes
/talks
/projects
/podcasts
/reading-list
/spotify
© 2024