Re: [go-nuts] Golang equivalent of Python's context manager

2018-09-12 Thread Justin Israel
On Thu, Sep 13, 2018, 10:13 AM Mirko Friedenhagen wrote: > Thanks again. I think some of the use cases, especially those which > implement some kind of finally (which includes locking use case) may just > be implemented with simple functions. At least throwing exceptions is a > rare event in

Re: [go-nuts] Golang equivalent of Python's context manager

2018-09-12 Thread Mirko Friedenhagen
Thanks again. I think some of the use cases, especially those which implement some kind of finally (which includes locking use case) may just be implemented with simple functions. At least throwing exceptions is a rare event in Golang (panicking is what I meant with rare). Are closures, like

Re: [go-nuts] Golang equivalent of Python's context manager

2018-09-12 Thread Justin Israel
For about the same amount of typing that you have to do to create a closure for using with a predefined WithContext handler, I tend to do this inline: func start() { fmt.Println("start") } func stop() { fmt.Println("stop") } func main() { func() (string, error) { start()

Re: [go-nuts] Golang equivalent of Python's context manager

2018-09-12 Thread Sam Whited
On Wed, Sep 12, 2018, at 14:12, Mirko Friedenhagen wrote: > 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

Re: [go-nuts] Golang equivalent of Python's context manager

2018-09-12 Thread Mirko Friedenhagen
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,

Re: [go-nuts] Golang equivalent of Python's context manager

2018-09-12 Thread 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

Re: [go-nuts] Golang equivalent of Python's context manager

2018-09-12 Thread Sebastien Binet
On Wed, Sep 12, 2018 at 2:55 PM robert engels 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

Re: [go-nuts] Golang equivalent of Python's context manager

2018-09-12 Thread robert engels
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

Re: [go-nuts] Golang equivalent of Python's context manager

2018-09-12 Thread robert engels
Too early, and the coffee had not kicked in... > On Sep 12, 2018, at 7:49 AM, robert engels wrote: > > Yes, you are correct. My bad :) > >> On Sep 12, 2018, at 7:34 AM, Sebastien Binet > > wrote: >> >> >> >> On Wed, Sep 12, 2018 at 2:23 PM robert engels >

Re: [go-nuts] Golang equivalent of Python's context manager

2018-09-12 Thread robert engels
Yes, you are correct. My bad :) > On Sep 12, 2018, at 7:34 AM, Sebastien Binet wrote: > > > > On Wed, Sep 12, 2018 at 2:23 PM robert engels > wrote: > I am pretty sure that is not correct, I am referring to: > >> func WithContext(context Context, f func()

Re: [go-nuts] Golang equivalent of Python's context manager

2018-09-12 Thread Sebastien Binet
On Wed, Sep 12, 2018 at 2:23 PM robert engels 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

Re: [go-nuts] Golang equivalent of Python's context manager

2018-09-12 Thread robert engels
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.

Re: [go-nuts] Golang equivalent of Python's context manager

2018-09-12 Thread Sebastien Binet
On Wed, Sep 12, 2018 at 2:10 PM robert engels wrote: > I think it would be problematic, as the callee signature is limited - > func() error - and with lack of method overloading you will need a lot of > With methods… > apart from the non-Go idiomatic names for the enter/exit interface

Re: [go-nuts] Golang equivalent of Python's context manager

2018-09-12 Thread robert engels
I think it would be problematic, as the callee signature is limited - func() error - and with lack of method overloading you will need a lot of With methods… > On Sep 12, 2018, at 6:58 AM, Mirko Friedenhagen wrote: > > Hello, > > in Python I may define context managers which do stuff

[go-nuts] Golang equivalent of Python's context manager

2018-09-12 Thread Mirko Friedenhagen
Hello, in Python I may define context managers which do stuff before and after an action has taken place. E.g.: ``` class MyContext(object): def __enter__(self): print("Entering context") def __exit__(self, extype_unused, value_unused, traceback_unused): print("Exiting