Golang : Exit, terminating or aborting a program
There are times where a controlled termination of a program is required rather than proceeding ahead. This is a quick example on how to exit/terminating/aborting a program in Golang.
package main
import (
"fmt"
"os"
)
func main() {
// will not be executed because of defer causing to Exit happen before this line
defer fmt.Println("Doing something...")
fmt.Println("Oh no, fatal error!")
os.Exit(1)
}
Sample output :
go run exit.go
Oh no, fatal error!
exit status 1
If the program is compiled, there WILL BE NO exit status number.
go build exit.go
./exit
Oh no, fatal error!
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.1k Golang : How to setup a disk space used monitoring service with Telegram bot
+6.4k Golang : Selection sort example
+23.6k Golang : Check if element exist in map
+22k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+5.4k Javascript : Shuffle or randomize array example
+12.1k Golang : Determine if time variables have same calendar day
+26.6k Golang : Convert(cast) string to uint8 type and back to string
+11.8k Golang : Convert(cast) float to int
+21.2k Golang : Sort and reverse sort a slice of strings
+24.1k Golang : Fix type interface{} has no field or no methods and type assertions example
+31k error: trying to remove "yum", which is protected
+13.7k Golang : reCAPTCHA example