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

找了一遍,发现没有 func RemoveString(slice []string, str string) []string #280

Open
J4sonZ opened this issue Dec 20, 2024 · 3 comments

Comments

@J4sonZ
Copy link

J4sonZ commented Dec 20, 2024

func RemoveString(slice []string, str string) []string {
result := make([]string, 0, len(slice))
for _, item := range slice {
if item != str {
result = append(result, item)
}
}
return result
}

@duke-git
Copy link
Owner

@J4sonZ , 使用slice.Filter实现。

@J4sonZ
Copy link
Author

J4sonZ commented Dec 21, 2024

@J4sonZ , 使用slice.Filter实现。
@duke-git

感觉上是没问题的,我只是提个建议哈, RemoveString(slice []string, str string) []string 这个功能常用级别应该非常 top,如果是 slice.Filter,每次使用的时候带一个函数在参数中感觉不够简洁。有一个库 goutils,把这个功能单独抽出来
image

@duke-git
Copy link
Owner

duke-git commented Dec 23, 2024

@J4sonZ RemoveString不是通用函数。如果要实现RemoveString,还需实现RemoveInt, RemoveFloat64, RemoveXXX. 实现泛型版本:RemoveT[T comparable](slice []T, item T) []T. 这个T还要求是comparable的,且实现都要调用slice.Filter,会产生大量浅函数。如果需要大量调用RemoveString,可以在项目内部封装实现。

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

2 participants