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
+13.4k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+20k Golang : Measure http.Get() execution time
+10.7k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+6.7k Golang : Embedded or data bundling example
+10.7k Golang : Flip coin example
+10.6k Generate Random number with math/rand in Go
+6.5k Unix/Linux : Use netstat to find out IP addresses served by your website server
+4.5k Linux/MacOSX : Search and delete files by extension
+25.7k Golang : Generate MD5 checksum of a file
+9.5k Golang : Create unique title slugs example
+14.6k Golang : How to determine if user agent is a mobile device example
+16k Golang : Update database with GORM example