Re: [go-nuts] Alternative of defer?

2019-02-09 Thread Michael Jones
ok, accepting that people use libraries with lurking panics, here's my tweak of Sam Whitted's answer: *package main* *func example() {* * // robust in face of unruly/panic-possible Operation1* * lockedOperation1 := func() error {* * lock1.Lock()* * defer lock1.Unlock()* * return

Re: [go-nuts] Alternative of defer?

2019-02-09 Thread robert engels
I was referring to this comment (below) made by a different commentor, which was related to the lock not being released if it panics - and that only matters if the application doesn’t terminate. > Recovering from panics is a common and IMO perfectly acceptable practice... > For example in a

Re: [go-nuts] Alternative of defer?

2019-02-09 Thread Michael Jones
The original question--the one I answered--was about a small block of user code and asked nothing about http or unexpected panics. The spirit of panic/recover, and with defer, includes in its use "local" panic, a kind of successor to setjump/longjump, that unwinds the stack, calls defers, and is

Re: [go-nuts] Alternative of defer?

2019-02-09 Thread robert engels
I agree, but in that case the ‘panic’ is as you say recognized (and in this particular case used to unwind from a potentially very deep call), but is then still returned as an error. The previous commentor referred to catching “panics" in a top-level http request processor and continuing to

Re: [go-nuts] Alternative of defer?

2019-02-09 Thread Ian Lance Taylor
On Fri, Feb 8, 2019 at 7:48 PM Robert Engels wrote: > > The way Go is designed a panic must terminate the application. Anything else > is so indeterminate to be unusable. I think one can reasonably argue that an unrecognized panic should terminate the application. But there is nothing wrong

Re: [go-nuts] Alternative of defer?

2019-02-09 Thread Robert Engels
Btw, before someone gets the wrong impression. You might trap the panic in a web container, rather than terminating the process, but every request should just auto respond with a 500 error. You might need the server live to perform critical error analysis, but even then the best solution is

Re: [go-nuts] Alternative of defer?

2019-02-08 Thread Robert Engels
Sorry, but I disagree, at least in the context of Go, and the difference between errors and panics. Imagine any financial service, if the code is encountering a condition it deems should be impossible, yet is occurring, bad things will happen. Very old adage, garbage in = garbage out. > On

Re: [go-nuts] Alternative of defer?

2019-02-08 Thread Ivan Bertona
Recovering from panics is a common and IMO perfectly acceptable practice... For example in a web server where you do not want a panic that occurs while handling a request to affect other request, especially ones that are in flight. On Fri, Feb 8, 2019 at 10:47 PM Robert Engels wrote: > The way

Re: [go-nuts] Alternative of defer?

2019-02-08 Thread Sam Whited
It's not pretty, but if you absolutely must write this code and can't refactor it, you can always use a closure to scope the defer: lock1.Lock() err = func() error { defer lock1.Unlock() return performOperation1() }() if err != nil { return err } performExpensiveOperation2() On Fri, Feb

Re: [go-nuts] Alternative of defer?

2019-02-08 Thread Ivan Bertona
What's suboptimal with the first one (or the second one) is that if performOperation1() panics the lock will not be released. It may or may not be a problem depending on the situation. Your assessment of defer used with locks is correct - it works well only if the lock doesn't need to be

Re: [go-nuts] Alternative of defer?

2019-02-08 Thread Burak Serdar
On Fri, Feb 8, 2019 at 11:28 AM vincent163 wrote: > > I am thinking about how to write programs like this: > lock1.Lock() > err = performOperation1() > if err != nil { > lock1.Unlock() > return err > } > lock1.Unlock() > performExpensiveOperation2() How about this: lock1.Lock() err =

[go-nuts] Alternative of defer?

2019-02-08 Thread vincent163
I am thinking about how to write programs like this: lock1.Lock() err = performOperation1() if err != nil { lock1.Unlock() return err } lock1.Unlock() performExpensiveOperation2() The lock1 must be locked while performing operation1, and I need to use its result to perform operation2. Since