Skip to content

Instantly share code, notes, and snippets.

@monkseal
Created April 2, 2016 09:53
Show Gist options
  • Save monkseal/cc62cb3b669d964df5a523ea727e67e5 to your computer and use it in GitHub Desktop.
Save monkseal/cc62cb3b669d964df5a523ea727e67e5 to your computer and use it in GitHub Desktop.
Simple Collect func for an Array or Slice of strings
package main
import "strings"
import "fmt"
func Collect(list []string, f func(string) string) []string {
result := make([]string, len(list))
for i, item := range list {
result[i] = f(item)
}
return result
}
func main() {
var cars = []string{"Saab", "Volvo", "Lexus", "Subaru"}
fmt.Println(Collect(cars, strings.ToUpper))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment