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
+22.3k Golang : Round float to precision example
+6.9k Golang : Of hash table and hash map
+28k Golang : Read, Write(Create) and Delete Cookie example
+33.4k Golang : Create x509 certificate, private and public keys
+14.1k Golang : Find network of an IP address
+16.5k Golang : How to generate QR codes?
+9.9k Golang : Random Rune generator
+39.1k Golang : Remove dashes(or any character) from string
+10k Golang : Convert file unix timestamp to UTC time example
+7.7k Golang : Get all countries phone codes
+5.5k Cash Flow : 50 days to pay your credit card debt
+7.6k Javascript : Put image into Chrome browser's console