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
+5k Nginx and PageSpeed build from source CentOS example
+18.9k Golang : How to make function callback or pass value from function as parameter?
+12.8k Golang : Remove or trim extra comma from CSV
+22.7k Generate checksum for a file in Go
+6.2k Golang : Create new color from command line parameters
+5k JQuery : Calling a function inside Jquery(document) block
+8k Golang : Gomobile init produce "iphoneos" cannot be located error
+8.7k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+6.9k Swift : substringWithRange() function example
+34.1k Golang : Proper way to set function argument default value
+10.8k Golang : Get currencies exchange rates example
+15.9k Golang : Get digits from integer before and after given position example