Golang os.Exit: How to Terminate Program in Go

Golang os.Exit() is a built-in function to exit the Go program and return a status code to the operating system. The Exit() function takes a status code as an argument where code 0 suggests success and the non-zero code suggests an error.

The os.Exit() function terminates an app immediately, which means that if there are any deferred functions, they are not run.

Syntax

func Exit(code int)

Parameters

The code parameter is the integer status code that will be returned to the operating system.

Return Value

The return type of the os.Exit() function nothing because it terminates the program.

Status codes

The status code 0 indicates an exit with no error and 1 with a general error. You can set the status code to whatever you want.

Any other numerical value between 1 and 125 (golang) shows the program encountered an error.

Example 1

package main

import (
  "fmt"
  "os"
)

func main() {
  fmt.Println("We are the world!")
  os.Exit(0)
  fmt.Println("We are the children")
}

Output

We are the world!

It printed the first line, “We are the world!” but will not print the second line, “We are the children”.

The os.Exit(0) function call terminates the program immediately and returns the exit status code of 0 to the operating system.

The exit status code of 0 typically indicates a successful exit, while non-zero exit status codes indicate an error.

It is important to note that any code after the call to os.Exit() will not be executed, so it is a good practice to use it only when you want to terminate the program early, bypassing further processing.

Example 2

package main

import (
  "fmt"
  "os"
)

func main() {
  defer fmt.Println("Deferred function")
  fmt.Println("Starting program")
  os.Exit(3)
  fmt.Println("This line will not be executed")
}

Output

exit status 3

In this example, the os.Exit() function is used to exit the program with a status code of 3 instantly.

The defer statement is used to defer the execution of the fmt.Println(“Deferred function”) statement until the surrounding function has been completed.

However, since os.Exit() terminates the program immediately; the deferred function is not executed.

Key takeaways about the os.Exit() function

  1. Terminates the program: The os.Exit() function terminates the program without executing any deferred functions or cleaning up resources.

  2. Returns a status code to the operating system: The status code argument is the integer code that will be returned to the operating system. A status code of 0 indicates success, while a non-zero code indicates failure.

  3. Different from return: Unlike the return statement, which returns from the current function, os.Exit() terminates the entire program.

  4. Use with caution: Since the os.Exit() function terminates the program immediately and should be used cautiously, especially when working with resources that need to be cleaned up, such as file handles or network connections.

Conclusion

Use the os.Exit() function to terminate Go programs, but it should be used cautiously, especially when working with resources that need to be cleaned up.

Leave a Comment