Skip to content

Instantly share code, notes, and snippets.

@milanboers
Created July 21, 2022 13:41
Show Gist options
  • Save milanboers/466b74ad62bb92e12d32c1abe5ff8f95 to your computer and use it in GitHub Desktop.
Save milanboers/466b74ad62bb92e12d32c1abe5ff8f95 to your computer and use it in GitHub Desktop.
Map function over arguments golang
func mapF[T any, R any](f func(T) (R, error), values ...T) ([]R, error) {
result := make([]R, len(values))
for _, value := range values {
mappedValue, err := f(value)
if err != nil {
return nil, fmt.Errorf("mapF failure: %w", err)
}
result = append(result, mappedValue)
}
return result, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment