chore: slice convert util

refactor/fs
Noah Hsu 2022-07-27 17:08:29 +08:00
parent e707d6b26e
commit b399c924b7
1 changed files with 12 additions and 0 deletions

View File

@ -20,3 +20,15 @@ func SliceContains[T comparable](arr []T, v T) bool {
}
return false
}
func SliceConvert[S any, D any](srcS []S, convert func(src S) (D, error)) ([]D, error) {
var res []D
for i, _ := range srcS {
dst, err := convert(srcS[i])
if err != nil {
return nil, err
}
res = append(res, dst)
}
return res, nil
}