Most idiomatic way to select elements from an array in Golang?

user2490003 picture user2490003 · Jun 1, 2016 · Viewed 49.5k times · Source

I have an array of strings, and I'd like to exclude values that start in foo_ OR are longer than 7 characters.

I can loop through each element, run the if statement, and add it to a slice along the way. But I was curious if there was an idiomatic or more golang-like way of accomplishing that.

Just for example, the same thing might be done in Ruby as

my_array.select! { |val| val !~ /^foo_/ && val.length <= 7 }

Answer

icza picture icza · Jun 1, 2016

There is no one-liner as you have it in Ruby, but with a helper function you can make it almost as short.

Here's our helper function that loops over a slice, and selects and returns only the elements that meet a criteria captured by a function value:

func filter(ss []string, test func(string) bool) (ret []string) {
    for _, s := range ss {
        if test(s) {
            ret = append(ret, s)
        }
    }
    return
}

Using this helper function your task:

ss := []string{"foo_1", "asdf", "loooooooong", "nfoo_1", "foo_2"}

mytest := func(s string) bool { return !strings.HasPrefix(s, "foo_") && len(s) <= 7 }
s2 := filter(ss, mytest)

fmt.Println(s2)

Output (try it on the Go Playground):

[asdf nfoo_1]

Note:

If it is expected that many elements will be selected, it might be profitable to allocate a "big" ret slice beforehand, and use simple assignment instead of the append(). And before returning, slice the ret to have a length equal to the number of selected elements.

Note #2:

In my example I chose a test() function which tells if an element is to be returned. So I had to invert your "exclusion" condition. Obviously you may write the helper function to expect a tester function which tells what to exclude (and not what to include).