Created
January 5, 2018 00:32
-
-
Save jsm/a85d1b1d846466008336deb2b26d26f7 to your computer and use it in GitHub Desktop.
Go patterns
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
func something(id uint, wg *sync.WaitGroup, errChan chan error) error { | |
if wg != nil { | |
defer wg.Done() | |
} | |
err := func(id uint) error { | |
var user models.User | |
if err := db.First(&user, id).Error; err != nil { | |
return errors.WithStack(err) | |
} | |
}(id) | |
if err != nil && errChan != nil { | |
errChan <- nil | |
} | |
return err | |
} | |
func somethingOuter(id uint, wg *sync.WaitGroup, errChan chan error) error { | |
if wg != nil { | |
defer wg.Done() | |
} | |
err := somethingInner(id) | |
if err != nil && errChan != nil { | |
errChan <- nil | |
} | |
return err | |
} | |
func somethingInner(id uint) error { | |
var user models.User | |
if err := db.First(&user, id).Error; err != nil { | |
return errors.WithStack(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment