Map To in Go

Posted by yhuang
Public (Editable by Users)
Go
Edit
package main

import (
    "fmt"
    "strings"
)

func Map(vs []string, f func(string) string) []string {
    vsm := make([]string, len(vs))
    for i, v := range vs {
        vsm[i] = f(v)
    }
    return vsm
}

func main() {

    var strs = []string{"peach", "apple", "pear", "plum"}

    fmt.Println(Map(strs, strings.ToUpper))
}