-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: os: add method (*File).MustClose #34407
Comments
There is some precedent here:
However, ignoring the syscall ones, all the existing Must APIs deal with static input. So if they don't panic when run once in one machine, they technically shouldn't panic anywhere. The same doesn't apply here, since this could depend on the user's filesystem. Having said that, it doesn't look like the error proposals are making progress at the moment, and I agree that the current state is ugly for examples and simple programs. So I agree that maybe it's time :) Edit: if added, I think the doc should clarify that |
I agree that we'd want a larger doc + example. |
You could do something like this without adding new api: func assert(f func() error, msg string) {
if err := f(); err != nil {
log.Fatalf(msg + ": %v", err)
}
}
//...
defer assert(f.Close, "failed to close CPU profile") With a MustClose method, in the linked use case, you'd have to recover the panic and log.Fatalf anyway so there'd be more boilerplate than with a small helper like this. |
Why would I "have to" do that? I could just, like, let it panic. |
I meant that you "have to" do that it in order to get the same behavior as in the code as authored currently. Just letting it panic is fine, too. |
My question for not using |
@cherrymui, I'm not proposing we take away the Close method. If that's what you want, use it. |
Or |
I don't think we want to encourage An alternative would be func (f *File) CheckClose(f func(error)) {
if err := f.Close(); err != nil {
f(err)
}
} To be used as defer f.CheckClose(func(err error) { log.Fatalf("failed to close CPU profile: %v", err) }) I'm not sure I like that either but I like it better that |
SGTM: https://godoc.org/go4.org/must#Close defer must.Close(f) |
Seeing that this can be done in an external package, like go4 above, I think that there's no need to add this to the standard Go library. |
@beoran note that the origin of this proposal is that multiple code samples in the standard library ignore Close errors when they shouldn't. They can't depend on third-party modules, and adding the manual This is not to say that we absolutely need something in std, but a third-party function doesn't really help the original problem.
Combining this with @cespare's idea to make this apply to any closer:
It would be much nicer if we could provide a std function directly, when no extra context is needed:
But I don't see any way to accomplish that without |
CloseOrPrintf probably isn't the right name... Fmtf? Logf? |
I retract. Not worth keeping it open. Maybe one day something will happen with Go error handling. |
Maybe it's time to add:
That would make for more readable code compared to all the stuff like:
https://go-review.googlesource.com/c/go/+/196497/1/src/runtime/pprof/pprof.go#b23
(MustClose of course only makes sense on files opened for write)
The text was updated successfully, but these errors were encountered: