Skip to content

Instantly share code, notes, and snippets.

@carolynvs
Created October 4, 2021 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carolynvs/e8c4c787a802d8be01646ab6b0d1c428 to your computer and use it in GitHub Desktop.
Save carolynvs/e8c4c787a802d8be01646ab6b0d1c428 to your computer and use it in GitHub Desktop.
exit main and execute defered statements
func main() {
run := func() int {
p := porter.New()
defer p.Close()
cmd := buildRootCommandFrom(p)
if err := cmd.Execute(); err != nil {
return 1
}
return 0
}
// Wrapping the main run logic in a function because os.Exit will not
// execute defer statements
os.Exit(run())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment