-
Notifications
You must be signed in to change notification settings - Fork 18k
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: must: Do #54297
Comments
I'd support this: we already have other similar APIs like https://pkg.go.dev/text/template#Must and https://pkg.go.dev/regexp#MustCompile, and there's really nothing special about them. Some standard support for |
I realise that some of what I wrote above overlaps with the |
I literally wrote a commit last night and thought, I should propose |
We're using this for our codebase: https://pkg.go.dev/tailscale.com/util/must#Get // Get returns v as is. It panics if err is non-nil.
func Get[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
} We've found it surprisingly useful ( |
I'm not sure if this should be in the standard library or not. We've all written quickie scripts with |
One question is intent. The tailscale In sufficiently large Go "scripts", you may want some degree of error handling, where the do-or-die approach of |
Some more prior art from the codebase I work on: https://pkg.go.dev/github.com/ridge/must/v2 - |
FYI https://pkg.go.dev/github.com/ridge/must/v2 is deprecated and the fork https://github.com/dottedmag/must is recommended |
I have thoughts on why sticking
If we introduce generic Must, the following problems will become relevant:
This seems to me to be too big a price to pay for saving a few lines of code. |
Only if you define "Must" as "panics on any programming error, and only on programming errors". Don't do that. Error values are used to propagate all kinds of non-errors, so "Must" ought to be a shortcut, not a semantically new idea.
The bigger impact is ability to use err-emitting code in an expression. It quite often allows one to convert a rambling 10-20 line procedure to 2-3 liner. |
Ok, I agree that in general defining “Must” as “panics on any programming error, and only on programming errors” is a bad idea. I implicitly assumed something like that when I wrote the backwards compatibility argument, so the argument doesn't work.
I meant a few lines of code to write your package in the repository. I don't deny that in the context of a particular repository the use of such a package (self-written or third-party) may be justified. |
I always find it tedious to use
httputil.ReverseProxy
because you usually need a*url.URL
and you can't make one easily without a few lines of boilerplate.Proposal: add
url.MustParse
like regexp.MustCompile, template.Must, etc.(Alternatively: a generic
must.Do
somewhere in std)cc @neild
The text was updated successfully, but these errors were encountered: