func CopyFile(src, dst string) error {
r, err := os.Open(src) orelse return err
defer r.Close()
w, err := os.Create(dst) orelse return err
defer w.Close()
err = io.Copy(w, r) orelse return err
err = w.Close() orelse return err
}
obj, err := createObj() orelse return //returns nil and err
}
err = io.Copy(w, r)
err = w.Close() orelse return err
On Sunday, 30 July 2023 at 06:57:15 UTC+1 DrGo wrote:I looked at the long list of proposals to improve error handling in go but I have not seen the one I am describing below.
orelse must return an error (ie satisfies the error interface); the specific type and variable name do not matter.
func myFirstFunction() (string, err) {
result, err := myFunction() orelse return rest, err
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/b87365af-9a72-4f8d-ad0b-1ee69cc1ad35n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/9453a6fe-04c2-4c6d-88e9-4ec1cbd59361n%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/330e88d9-8072-4614-ae56-8ce9c59517f3n%40googlegroups.com.
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/dRLR4hxxI8A/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/60596671-D3AC-49D4-8575-F8EB3D9B6BF6%40ix.netcom.com.
Compare the following; which one would you prefer to read a lot of?
“we would like to make error checks more lightweight, reducing the amount of Go program text dedicated to error checking. We also want to make it more convenient to write error handling, raising the likelihood that programmers will take the time to do it.
Both error checks and error handling must remain explicit, meaning visible in the program text. We do not want to repeat the pitfalls of exception handling.
Existing code must keep working and remain as valid as it is today. Any changes must interoperate with existing code.”
Fair enough. But many would prefer shorter functions if there is no loss to explicitness or clarity.
>> What happens if all of a sudden, the makeSomeNoise returns 2 errors? Then you have to undo the orelse construction, for what I understand.No as long as both errors are meant to be handled as errors. Orelse will be triggered if any or both were not nil.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/e678eaf8-699c-4818-bbfd-92556944ccefn%40googlegroups.com.
And of course I forgot the "if" at the beginning of all those conditional. *sigh*
--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/dRLR4hxxI8A/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CADtPBF2%3DTNBorhCCamWGb29qkNkXxgFZ%2BmnhkOC0kG2sxzp%3DWw%40mail.gmail.com.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/36110a8d-a26f-48be-83fd-73af755e88f4n%40googlegroups.com.
@Miguel Angel Rivera NotararigoThanks for taking the time to write...In my proposal, people are free to add as much context as they want...
but as a demonstration, I am using the example fromRoss Cox's paper on error handling that is used by all error handling proposals to show case their approach. I do not think Ross willtake your criticism personally :)
I on the other hand take exception to your generalization re people who complain about error handling in Go.I am sure you did not make that claim without having some sort of solid research to support it. But, Go designers themselves admit that this is an issue and have writtentons on it.
In one or two replies above we were discussing how error handling opinions can become religions each with its own priests who think they are the only saved faction, and that their rituals are the only right approach for all situations.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/2c9fa4b1-e536-4743-ac20-181e550bd14fn%40googlegroups.com.
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/dRLR4hxxI8A/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAF9DLCkTKJz8tpOwFQzV%3DoXe6R6aS-0ssnXdwDraDS112gnQ0w%40mail.gmail.com.
I understand, but there is a little difference, you can find code out there improving performance thanks to generics, I am not sure if we can get any performance improvement by using "orelse return err".
Yes, we can handle Go errors without language changes; that’s true. But how we should annotate errors? The presented version of CopyFile leads to following 'stuttering':
cannot copy '/tmpfs/play' to './tmp33/not-exist.txt': cannot create destination: open ./tmp33/not-exist.txt: no such file or directoryYou can get compact but informative error messages just by following K&D annotation rules:
All of this is just basic error propagation, which can be automated to avoid annoying human errors. You can see three different error annotation scenarios of the FileCopy implementations in this playground.
And the function itself is compact as well — and fully automated (tip: change the name of the function):
func copyFile(src, dst string) (err error) { defer err2.Handle(&err) r := try.To1(os.Open(src)) defer r.Close() w := try.To1(os.Create(dst)) defer err2.Handle(&err, func() { os.Remove(dst) }) defer w.Close() try.To1(io.Copy(w, r)) return nil }Yes, we can handle Go errors without language changes; that’s true. But how we should annotate errors? The presented version of CopyFile leads to following 'stuttering':
cannot copy '/tmpfs/play' to './tmp33/not-exist.txt': cannot create destination: open ./tmp33/not-exist.txt: no such file or directory
You can get compact but informative error messages just by following K&D annotation rules:
copy file: open ./tmp33/not-exist.txt: no such file or directory
- Annotate the function you are implementing
- Let the functions that you are calling do their own job
All of this is just basic error propagation, which can be automated to avoid annoying human errors. You can see three different error annotation scenarios of the FileCopy implementations in this playground.
And the function itself is compact as well — and fully automated (tip: change the name of the function):
func copyFile(src, dst string) (err error) { defer err2.Handle(&err) r := try.To1(os.Open(src)) defer r.Close() w := try.To1(os.Create(dst)) defer err2.Handle(&err, func() { os.Remove(dst) }) defer w.Close() try.To1(io.Copy(w, r)) return nil }