Skip to content

Instantly share code, notes, and snippets.

@mhewedy
Created May 7, 2020 11:30
Show Gist options
  • Select an option

  • Save mhewedy/6d8bd72d34b59dfdae1747022e585f75 to your computer and use it in GitHub Desktop.

Select an option

Save mhewedy/6d8bd72d34b59dfdae1747022e585f75 to your computer and use it in GitHub Desktop.
Defer trick to around wrap function code
package main
import (
"fmt"
)
type Tnx struct {
}
func main() {
tnx := &Tnx{}
defer manageTnx(tnx)()
fmt.Printf("Working with the DB Transaction %p\n", tnx)
}
func manageTnx(tnx *Tnx) func() {
fmt.Printf("Open DB Transaction %p \n", tnx)
return func() {
fmt.Printf("Close DB Transaction %p \n", tnx)
}
}
@mhewedy
Copy link
Copy Markdown
Author

mhewedy commented May 7, 2020

Output:

Open DB Transaction 0x58fd18 
Working with the DB Transaction 0x58fd18
Close DB Transaction 0x58fd18 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment