cleanexit

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2015 License: MIT Imports: 2 Imported by: 5 modules, 4 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

cleanexit

Package to help cleanly terminate a golang program via ctrl-c.

Functions

SetUpSimpleExitOnCtrlC()

Sets up a separate goroutine to listen for ctrl-c. As soon as a ctrl-c is entered in the terminal, the program is terminated. An program exit code of 0 is returned.

Use SetUpExitOnCtrlC to allow for a custom function to run right before program termination.

SetUpExitOnCtrlC(cleanup func())

Similar to SetUpSimpleExitOnCtrlC except you can pass in a function to run right before the program terminates.

Can be used for clean up, or to print a nice exit message. I've used this to hide the ^C that is printed to a terminal when a user hits ctrl-c. You can do this by printing a couple \b characters followed by spaces to stdout:

fmt.Printf("\b\b  \n")

The \b characters are the equivalent of hitting the back arrow key. Wrap that in a function, pass it in to SetUpExitOnCtrlC and you're set.

You can use your own call to os.Exit(...) within the cleanup function to signify non-successful exit if necessary, otherwise an exit code of 0 (zero) will be used.

Example
import (
    "fmt"
    "os"
    
    "github.com/jeffbmartinez/cleanexit"
)

func main() {
    cleanexit.SetUpExitOnCtrlC(myExitFunc)
    
    // ...
}

func myExitFunc() {
    // clean up connections, etc
    
    if (/* something went wrong */) {
        os.Exit(FAILURE_CODE)
    }
    
    fmt.Printf("\b\b  \nGoodbye!\n")
}

Documentation

Index

Constants

View Source
const EXIT_SUCCESS = 0

Standard command line tool code for 'successful termination'.

Variables

This section is empty.

Functions

func SetUpExitOnCtrlC

func SetUpExitOnCtrlC(cleanup func())

Similar to SetUpSimpleExitOnCtrlC except you can pass in a function to run right before the program terminates.

Can be used for clean up, or to print a nice exit message. I've used this to hide the "^C" that is printed to a terminal when a user hits ctrl-c. You can do this by printing a couple \b characters followed by spaces to stdout. Like this:

fmt.Printf("\b\b \n")

The \b characters are the equivalent of hitting the back arrow key. Wrap that in a function, pass it in to SetUpExitOnCtrlC and you're set.

You can use your own os.Exit(...) within the cleanup function to signify non-successful exit if necessary.

func SetUpSimpleExitOnCtrlC

func SetUpSimpleExitOnCtrlC()

Sets up a separate goroutine to listen for ctrl-c. As soon as a ctrl-c is entered in the terminal, the program is terminated. An program exit code of 0 is returned.

Use SetUpExitOnCtrlC to allow for a custom function to run right before program termination.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL