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
+8.8k Golang : Test a slice of integers for odd and even numbers
+21.2k Golang : Convert Unix timestamp to UTC timestamp
+15.1k Golang :Trim white spaces from a string
+5k Golang : Calculate US Dollar Index (DXY)
+4.3k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+11.4k Golang : zlib compress file example
+26.8k Golang : Read, Write(Create) and Delete Cookie example
+5.5k Unix/Linux : Use netstat to find out IP addresses served by your website server
+32.9k Golang : Integer is between a range
+7.1k Golang : Auto-generate reply email with text/template package
+9.7k PHP : Convert(cast) bigInt to string