Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New func to add error to response #410

Open
woniu317 opened this issue Dec 19, 2023 · 0 comments
Open

New func to add error to response #410

woniu317 opened this issue Dec 19, 2023 · 0 comments

Comments

@woniu317
Copy link

woniu317 commented Dec 19, 2023

When we convert an array of strings to numbers, the Map function is called.

numbers := Map(strs, func(item string, index int) int {
    number, err := strconv.Atoi(item)
    if err != nil {
        // log
        return 0
    }

    return number
})

Errors during conversion must be ignored and the loop cannot be terminated early. I want to increase the definition of the function with error:

func Map[T any, R any](collection []T, iteratee func(item T, index int) (R, error)) ([]R, error) {
	result := make([]R, len(collection))
        var err error

	for i, item := range collection {
		result[i],err = iteratee(item, i)
                 if err != nil {
                        // log
                       return nil, err
                  }
	}

	return result, nil
}
@woniu317 woniu317 changed the title Add error to response New func to add error to response Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant