-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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
strings: add First function? #14423
Comments
Why does it have to be in the strings package?
I think it's so trivial that you can just put it in
whatever package that needs it.
|
I agree with Minux. Any addition to the std library would need to be supported with data showing that it'd help a large number of programs, including the standard library itself, and lead to more legible programs. But I do think there are a number of funcs in various places in the standard library that would use something like a Here are at least two:
|
Hi, thanks for your feedback. I searched go/src for src/cmd/asm/internal/asm/endtoend_test.go:210
src/cmd/cgo/out.go:316
src/cmd/cgo/out.go:631
src/cmd/cgo/out.go:1072
src/cmd/cgo/out.go:1106
src/cmd/dist/build.go:99
src/cmd/dist/build.go:104
src/cmd/dist/build.go:110
src/cmd/dist/build.go:119
src/cmd/dist/build.go:151
Best regards, |
This is not an issue, this is a feature request. Please discuss this first on one of the popular Go forums (mailing list, etc.). I agree with @minux and @bradfitz that this doesn't belong into the strings package: It's trivial to write if needed. Moreover, the examples you found are clearer as is, than with an extra function call. For instance:
would become
Now a reader has to learn what Coalesce means (and it doesn't seem a very good name for the functionality either). Also, the 2nd argument always has to be evaluated (and in this case called) even if not needed (possibly an expensive operation). Finally, invoking a variadic function (...) requires allocation and population of a slice. If the pattern
is very common in a package it's trivial to define
which doesn't allocate a slice. It's also easy to see locally what it does. The functionality is simply too simple to warrant inclusion in strings. Or, looking from the other perspective: If we add this to strings, where do we say no? Closing. |
Hi,
i would like to have a string.coalesce function, that returns the first not empty string.
I think, it's an clean solution for realizing default values.
It exists in T-SQL for example.
What do you think?
Best regards,
Steffen
The text was updated successfully, but these errors were encountered: