Monday, December 9, 2024
HomeGolangExit an app in Go (Golang)

Exit an app in Go (Golang)



To exit an software in Go, use the os.Exit() operate from the os package deal. It causes this system to terminate instantly. The operate takes a standing code as an argument the place the code zero signifies success and the non-zero an error.

The os.Exit() operate terminates an app instantly, which signifies that if there are any deferred features, they don’t seem to be run.

Standing codes

Sometimes, the standing code 0 signifies an exit with no error, and 1 an exit with a basic error. You’ll be able to set the standing code no matter you need, however take into account that a few of them have particular meanings, which you’ll examine right here.

Instance

Exit with standing code 0

package deal important

import (
    "fmt"
    "os"
)

func important() {
    fmt.Println("Hi there gosamples.dev")

    os.Exit(0)

    fmt.Println("Bye gosamples.dev")
}

Output:

As you may see within the output, the app closes with none error instantly after calling the os.Exit(). The final line of the important() operate is rarely executed.

Exit with standing code 1

package deal important

import (
    "fmt"
    "os"
)

func important() {
    fmt.Println("Hi there gosamples.dev")

    os.Exit(1)

    fmt.Println("Bye gosamples.dev")
}

Output:

Hi there gosamples.dev
exit standing 1

When shutting down an software with a standing code aside from 0, we see an extra message exit standing <code> within the output, indicating that the appliance is terminated with an error.

When to make use of os.Exit() and when to make use of panic()?

  • The os.Exit() operate terminates this system instantly with no chance of restoration and working deferred features. The app returns an error code that different applications can learn to examine what occurred.
  • The panic() operate is used to report unrecoverable errors in this system, for instance, run-time errors resembling indexing a slice out of bounds. It instantly stops the execution of the present operate and begins to unwind the goroutine stack, working any deferred features alongside the best way. If that unwinding reaches the highest of the goroutine’s stack, this system dies.

Sometimes, when your software reaches an unrecoverable state and can’t proceed because of a particular error, it’s best to use the panic() operate. With the panic(), the appliance closes gracefully, all deferred features are executed, and the appliance prints an correct error message to the output. The os.Exit() operate, which closes the appliance instantly, can be utilized if you want out of your software an error code that may be learn by different scripts. It may also be used when your software has already performed every part it ought to have performed, and now it simply must exit, resembling after writing the outcomes to the usual output or when the consumer runs a command to shut the appliance.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments