-
-
Notifications
You must be signed in to change notification settings - Fork 393
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
建议:丰富strutil 的方法 #88
Comments
str := "foo bar"
substring := str[0:2]
|
👍,合理建议,兼容性很重要。 |
数组那个我存个 1 2 3, find是匹配就返回原始数据1,存指针就返回指针,这样 |
是的,当初如此设计是考虑到未找到值时返回nil, 否则会返回零值。不过可以根据第二个返回值(bool)判断是否查找到。这个可以做个break change, 重新release. |
可以新增一个如下函数, 让go自己初始化零值 func FindBy[T any](slice []T, predicate func(index int, item T) bool) (v T, ok bool) {
index := -1
for i, v := range slice {
if predicate(i, v) {
index = i
break
}
}
if index == -1 {
return v, false
}
return slice[index], true
} |
这样不错,可以避免兼容性问题。然后将之前的Find/FindLast都标记为deprecated. |
同样的, datastructure包的一些方法也可以做类似调整, 如datastructure/list/list.go:29 |
feat: add example for FindBy chore: reduce code duplication
v2.1.20已发布,包含上述新方法。 |
常用的字符串截取, fromIndex~toIndex,左闭右开,省去计算长度
字符串查找子串,比如 abcdeabc, 找 abc, 返回0, 从index=1开始找 返回5 , indexFrom
字符串是否包含某个子串, contains 返回bool
数组,find建议在提供一个原始数据查找,目前的 find如果数组存的就是指针, 返回指针的指针,使用时还要在解除引用
有的包名称太长,可以缩短点
The text was updated successfully, but these errors were encountered: