Golang 1.18beta supports generic, I want to add an extension method on a generic slice. e.g. a map function is defined as this:
func Map[E, V any](slice *[]E, iteratee func(E) V) *[]V {
result := []V{}
for _, item := range *slice {
result = append(result, iteratee(item))
}
return &result
}
Then I want to make this method as an extension method of slice, something like this, but cannot compile successfully:
func (slice *[]E) Map[E, V any](iteratee func(E) V) *[]V {
result := []V{}
for _, item := range *slice {
result = append(result, iteratee(item))
}
return &result
}
go build
with Go 1.18 gives the error:
main.go: method must have no type parameters
What is the correct way to implement the second code block?
I want to use like this:
slice := []string{"a", "b", "c"}
newSlice := slice.Map(func(s string) string {
return s + "122"
})
Map
method to what type? – Amiss