Skip to content

Instantly share code, notes, and snippets.

@thepabloaguilar
Created April 27, 2021 02:23
Show Gist options
  • Save thepabloaguilar/9762d5b1f62f73fff83b17dc2125ec16 to your computer and use it in GitHub Desktop.
Save thepabloaguilar/9762d5b1f62f73fff83b17dc2125ec16 to your computer and use it in GitHub Desktop.
A `map` implementation for go Slices
package main
import (
"fmt"
"strconv"
)
func Map[T1, T2 any](s []T1, f func(T1) T2) []T2 {
newSlice := make([]T2, 0)
for _, item := range s {
newSlice = append(newSlice, f(item))
}
return newSlice
}
func main() {
s := []int {1, 2, 3, 4}
newS := Map(s, strconv.Itoa)
fmt.Printf("%T -> %s", newS, newS)
// []string -> [1 2 3 4]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment