You said: "and FTR, I'd also consider your function a bad idea, but that's
none of my business" <- I'll be the first to say not all my ideas are good
ones, maybe this one isn't? Thanks for your response.


On Tue, Feb 7, 2023 at 4:45 PM Axel Wagner <axel.wagner...@googlemail.com>
wrote:

> No, that is not possible. Only a `return` statement can return.
> You *can* unwind the stack, using `panic` or `runtime.GoExit` (which is
> what `t.Fatal` does) but it's a bad idea in general (and FTR, I'd also
> consider your function a bad idea, but that's none of my business).
>
> On Tue, Feb 7, 2023 at 10:39 PM Rich <rma...@gmail.com> 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/2eec0130-3deb-4f60-a13e-d7c9d132771dn%40googlegroups.com
>> <https://groups.google.com/d/msgid/golang-nuts/2eec0130-3deb-4f60-a13e-d7c9d132771dn%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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/CAJ0CmuGaHPF9ZZpHZf2G8N7eY_NM8%3DpE9z6vGp8tYvEnv6vbnQ%40mail.gmail.com.

Reply via email to