Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active January 29, 2021 12:30
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 Integralist/aa651c6fdaf812b6ba7e3d04193b6434 to your computer and use it in GitHub Desktop.
Save Integralist/aa651c6fdaf812b6ba7e3d04193b6434 to your computer and use it in GitHub Desktop.
[Go defer with os.Exit(N)] #go #golang #defer #exit
package main
import (
"fmt"
"os"
"runtime"
)
/*
Goexit terminates the goroutine that calls it. No other goroutine is affected. Goexit runs all deferred calls before terminating the goroutine. Because Goexit is not a panic, any recover calls in those deferred functions will return nil.
Calling Goexit from the main goroutine terminates that goroutine without func main returning. Since func main has not returned, the program continues execution of other goroutines. If all other goroutines exit, the program crashes.
https://golang.org/pkg/runtime/#Goexit
*/
func main() {
defer os.Exit(0)
fmt.Println("Hello, playground")
defer fmt.Println("end")
runtime.Goexit() // instead of os.Exit(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment