A package err2 <https://github.com/lainio/err2> helps you to implement the 
`errReturn` even though Go doesn't have macros or can return from the 
function enclosing the function checking the error.
Please see the playground that does exactly that:
https://go.dev/play/p/GvXFU1LbVvs
However, I recommend you to use `err2` with all of its power, i.e., 
automation. Could you look at the following playground? Even the error 
strings/messages are autogenerated from function names (yeah, localization, 
but in most cases, enough to start with). Declarative error handling helps 
to keep your code safe.
https://go.dev/play/p/6Ky5nMtqu4F

And yes, I'm the author of the err2.
On Tuesday, February 7, 2023 at 11:38:41 PM UTC+2 Rich wrote:

> In most of my code I create a function to handle errors. looks something 
> like this:
>
> func errorHandle(err error, str string, ex bool) {
>      if err != nil {
>           fmt.Printf("error: %s -- %v\n",str, err)
>      }
>      if ex {
>         os.Exit(1)
>      }
> }
>
> This cleans up my code:
>     inFile, err := os.ReadFile("Mydata.txt")
>     errorHandle(err,"read mydata.txt",true) // The true will exit
>
> So you see that I can exit the program when there is an error, what I want 
> to know is if it's possible to do the same thing inside a function, to 
> where I don't exit the program but return from the function:
>
> func OpenFile( filename string) []byte {
>     inFile,err := os.ReadFile(filename)
>     errReturn(err, "read "+filname,true)
>
> When there is an error it would return from the OpenFile function,
>
>

-- 
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/7292381a-655f-4ded-b193-19d85e64b2d2n%40googlegroups.com.

Reply via email to