Last active
January 29, 2021 12:30
-
-
Save Integralist/aa651c6fdaf812b6ba7e3d04193b6434 to your computer and use it in GitHub Desktop.
[Go defer with os.Exit(N)] #go #golang #defer #exit
This file contains 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" | |
"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