First of all, thanks for all the answers. A standard use case in Python is 
to use with the a closing context manager which will just call close on a 
given object. That one could be easily done with a defer statement. 
However, coming from Java lately, retrieving a DB-connection from a pool, 
opening a transaction and committing/aborting and returningafterwards seems 
something which could be handled with such a construct. How would you do 
this in Golang?

Am Mittwoch, 12. September 2018 15:37:12 UTC+2 schrieb Robert Engels:
>
> Agree it can be done - not sure I would want to maintain that in a large 
> system as you are paying the LOC penalty at every call site. Seems easier 
> to just add the context callback in the methods themselves so then you are 
> in control of the code penalty.
>
> On Sep 12, 2018, at 8:17 AM, Sebastien Binet <bi...@cern.ch <javascript:>> 
> wrote:
>
>
>
> On Wed, Sep 12, 2018 at 2:55 PM robert engels <ren...@ix.netcom.com 
> <javascript:>> wrote:
>
>> Ok, now the coffee has kicked in, and it still has problems, because you 
>> cannot call a method and use a return value other than error without 
>> multiple signatures, unless you move all of the return values outside of 
>> the closure - like his example does with var a. 
>>
>
>> This would be extremely tedious (and probably error prone) to use IMO, 
>> especially with Go’s multiple return values.
>>
>
> one has to be careful, yes. but it's not completely out of reach.
>
> func Foo(a, b int) (int, error) { ... }
>
> func Do() {
>     var out = 0
>     With(ctx, func() error {
>         var err error
>         out, err = Foo(42, 66)
>         return err
>     })
> }
>
> closures are great! (like fezzes and bowties, actually :P)
>
> -s
>  
>
>>
>> On Sep 12, 2018, at 7:50 AM, robert engels <ren...@ix.netcom.com 
>> <javascript:>> wrote:
>>
>> Too early, and the coffee had not kicked in...
>>
>> On Sep 12, 2018, at 7:49 AM, robert engels <ren...@ix.netcom.com 
>> <javascript:>> wrote:
>>
>> Yes, you are correct. My bad :)
>>
>> On Sep 12, 2018, at 7:34 AM, Sebastien Binet <bi...@cern.ch <javascript:>> 
>> wrote:
>>
>>
>>
>> On Wed, Sep 12, 2018 at 2:23 PM robert engels <ren...@ix.netcom.com 
>> <javascript:>> wrote:
>>
>>> I am pretty sure that is not correct, I am referring to:
>>>
>>> func WithContext(context Context, f func() error) error {
>>> context.__entry__()
>>> defer context.__exit__()
>>> return f()
>>> }
>>>
>>>
>>> You will need a lot of WithContext methods - based on the different 
>>> signatures of function f.
>>>
>>> See the main method… I am assuming this is attempting to be a general 
>>> facility - and thus the signature of f will vary.
>>>
>>
>> you just need to wrap it with a closure, just as Eric did:
>>
>> e.g.:
>>  func QueryDB(a,b int) error { ... }
>>  
>>  WithContext(ctx, func() error {
>>     return QueryDB(42, 666)
>>  })
>>
>> and voila.
>>
>> -s
>>
>>
>>
>> -- 
>> 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...@googlegroups.com <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>>
>>
>>
>> -- 
>> 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...@googlegroups.com <javascript:>.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>

-- 
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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to