Dnia 2023-07-29, o godz. 22:57:15 DrGo <salah.mah...@gmail.com> napisał(a):
> This involves introducing a new keyword "orelse" that is a syntactic sugar > for an "if err!=nil" block. You can implement it as a snippet under any editor in use today. If your goal is to personally have it in just one line, you can turn off gofmt and write `; if err !=nil {; return err }` I'd like to point out that considering > It is an error to not return an error from an orelse block. The `return err` in examples still is a boilerplate, so the overall savings are just four utf-8 bytes, ie. {\n}\n. I understand sentiment, 'cos there is a huge camp of devs claiming that any error handling proper is one that is done by something/someone up the callstack, even future-me — for this camp idiomatic Go can be annoying. But note that for the most pleasant coder experience the `orelse return err` is way too much of text to write. I would better stay with ^^ digraph instead. Lets name it a 'bat operator'. func CopyFile(src, dst string) error { r, err := os.Open(src) ^^ defer r.Close() w, err := os.Create(dst) ^^ defer w.Close() err = io.Copy(w, r) ^^ err = w.Close() ^^ } > It also works well with named returns. e.g., func returnsObjorErro() (obj Obj, err error) { obj, err := createObj() ^^ //returns nil and err } > otherwise ^^ is like "else" so e.g., it can be followed by a block if > additional cleanup or error formatting etc is needed before returning, eg w, err := os.Create(dst) ^^ { .... return err } P.S — bat operator is easily implementable in an IDE with onFileOpen/onFileWrite hooks. Its the piping of content to tools (VSCode and GoLand) that bars me from personally using it. Hope this helps, -- Wojciech S. Czarnecki << ^oo^ >> OHIR-RIPE -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/20230802225759.198a4de2%40xmint.