How about this?
//locking/unlocking mechanism is handled inside performOperation1
if err:= performOperation1(lock1); err!=nil {
return err
}
performExpensiveOperation2()
On Saturday, February 9, 2019 at 1:28:12 AM UTC+7, 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()
>
> The lock1 must be locked while performing operation1, and I need to use
> its result to perform operation2. Since operation2 is expensive, I don't
> want to hold the lock while performing it, and lock1.Unlock() needs to be
> called before calling operation2.
> Go's defer mechanism doesn't seem to handle this case well since the
> resource is used only within a block and not throughout the function. Is
> there a recommended way to write programs in this case?
> I know I could wrap the lock block in a closure, but that creates a
> completely new scope, so I can't return directly or break out of a loop
> within the closure, etc.
>
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.