Enjoy A Slice of Pie! Dealing With Go Slices

Image for post
Image for post
Photo by sheri silver on Unsplash

Everyone that has used Go has written some (perhaps many) slice utility methods like this:

It’s one of those things that’s not so painful as to investigate a better solution, but just painful enough that it can be annoying sometimes.

There are several great packages out there for making this task easier; thoas/go-funk, leaanthony/slicer, viant/toolbox and alxrm/ugo to name a few. However, these use reflect to make the interface on the consumer as easy and flexible as possible. Which is great, but comes at the expense of performance and type safety.

So I’ve created github.com/elliotchance/pie that focuses on performance and type safety. It’s basically a collection of all those utility methods you would normally write, but has the same high performance and type safety you would otherwise expect.

In my experience when dealing with slices (excluding custom types) I really only operate with []string, []int and []float64. It’s also not unreasonable to update existing code to fit into these slice types. Unfortunately, if you are dealing with custom slice types pie will not be of any help.

It can be used with the Go-style package functions:

Or, they can be chained for more complex operations:

There is a full list of all functions, conditionals and transforms in the README on the project page.

Originally published at http://elliot.land on April 4, 2019.

Written by

I’m a data nerd and TDD enthusiast originally from Sydney. Currently working for Uber in New York. My thoughts here are my own. 🤓 elliotchance@gmail.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store