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
+4.9k Golang : Check if a word is countable or not
+6.8k Golang : How to setup a disk space used monitoring service with Telegram bot
+19k Golang : Check if directory exist and create if does not exist
+15.8k Golang : How to reverse elements order in map ?
+7.6k Golang : How to execute code at certain day, hour and minute?
+9.3k Mac OSX : Get a process/daemon status information
+18.7k Golang : How to make function callback or pass value from function as parameter?
+33.8k Golang : Proper way to set function argument default value
+21.6k Golang : How to reverse slice or array elements order
+21.9k Golang : Convert seconds to minutes and remainder seconds
+11.1k Golang : How to use if, eq and print properly in html template
+7.6k Golang : Reverse a string with unicode