Created
May 7, 2020 11:30
-
-
Save mhewedy/6d8bd72d34b59dfdae1747022e585f75 to your computer and use it in GitHub Desktop.
Defer trick to around wrap function code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: