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

proposal: strings: implement Include function. #27825

Closed
dennypenta opened this issue Sep 24, 2018 · 3 comments
Closed

proposal: strings: implement Include function. #27825

dennypenta opened this issue Sep 24, 2018 · 3 comments

Comments

@dennypenta
Copy link

What version of Go are you using (go version)?

go version go1.10.3 darwin/amd64

Feature request.
Implement strings.Includes([]string, string) bool and same thing for other types.
At least once a week we need to check that slice of strings includes another string.
It's common code that we have to implement in every project and every package.

@agnivade agnivade changed the title Feature request: implement Include function. proposal: strings: implement Include function. Sep 24, 2018
@gopherbot gopherbot added this to the Proposal milestone Sep 24, 2018
@mvdan
Copy link
Member

mvdan commented Sep 24, 2018

A couple of observations:

  • This should wait until a decision is made on generics. If the language ends up with generics, this function should work for all element types.
  • This is an extra API to avoid a four-line function. I'm not sure if it warrants being in the standard library. In comparison, most other APIs in the strings package are either more complex or fairly optimized.

@networkimprov
Copy link

We have this for sorted arrays: https://golang.org/pkg/sort/#SearchStrings
That doesn't mention case-folding: https://golang.org/pkg/strings/#EqualFold
Is that an oversight?

A strings.Includes(slice, str) could use EqualFold() or equivalent. Might be more complex that way ;-)

The proposal could be directed instead to https://godoc.org/golang.org/x/text/search

It appears evident that a decision was already made to implement generics somehow, but it will be a couple years before anything lands!

@rsc
Copy link
Contributor

rsc commented Oct 3, 2018

At least once a week we need to check that slice of strings includes another string.
It's common code that we have to implement in every project and every package.

Can't you write a library and use it from those projects?

Also note that if the slice is large then that's not the best signature - better to use a map[string]bool instead of a []string.

In any event, this should be held for generics. This is an obvious thing we'd write in a 'slices' packages. (It's the canonical example of generic code, dating back to CLU.)

@rsc rsc closed this as completed Oct 3, 2018
@golang golang locked and limited conversation to collaborators Oct 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants