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()
        defer stop()
        fmt.Println("run")
        return "hi", nil
    }()
}

https://play.golang.org/p/3B63iTY4pla

It means I can return any signature, as long as I have the enter/exit
functions defined already.

Justin

On Thu, Sep 13, 2018, 7:35 AM Sam Whited <s...@samwhited.com> wrote:

> 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 in Golang?
>
> I frequently do something similar to this:
>
> https://gist.github.com/SamWhited/d81d081aed0351a1c1d128acf3a16b5c
>
> (there may be some extra parameters, I cut out some row-level-security
> stuff from something being used in prod and pasted everything into the gist
> without really checking it)
>
> —Sam
>
> --
> 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.
>

-- 
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