exit

package module
v0.0.0-...-6a90970 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2018 License: Unlicense Imports: 4 Imported by: 2

README

Exit

Exit provides a way to exit from a program while both running deferred functions and returning a non-zero exit code.

GoDoc status Go Walker GoSearch

Deferred functions are great for resource clean up. os.Exit exits a program immediately with an exit code, but doesn't run your deferred functions. This package provides a simple way to do both, exit a program immediately with an exit code, but also run the deferred functions on the way out.

Of course it's just an application of panic and recover but the package saves you writing that little bit of code for every command line program where you want this capability.

Example

package main

import (
   "fmt"

   "github.com/soniakeys/exit"
)

func main() {
  defer exit.Handler() // do this once at the beginning of the program
  f()
  fmt.Println("f returned")
}

func f() {
   defer fmt.Println("f cleanup")
   fmt.Println("f attempt")
   if !false {
      exit.Log("Fatal error in f") // use exit.Log anywhere
   }
   fmt.Println("f completed")
}
$ go build example.go
$ ./example 2>log
f attempt
f cleanup

$ echo $?
1

$ cat log
2014/10/02 20:03:59 Fatal error in f

See also

Similar package at https://github.com/youtube/vitess/tree/master/go/exit

Documentation

Overview

Exit provides a way to exit from a program that runs deferred functions, logs a message to stderr, and then exits with an operating system exit code.

The following example program is in source subdirectory "example".

package main

import (
   "fmt"

   "github.com/soniakeys/exit"
)

func main() {
  defer exit.Handler() // do this once at the beginning of the program
  f()
  fmt.Println("f returned")
}

func f() {
   defer fmt.Println("f cleanup")
   fmt.Println("f attempt")
   if !false {
      exit.Log("Fatal error in f") // use exit.Log anywhere
   }
   fmt.Println("f completed")
}

This prints "f attempt" and "f cleanup" to stdout, logs "Fatal error in f" to stderr, and exits with a program exit code of 1.

Index

Constants

This section is empty.

Variables

View Source
var Default = Exit{ExitCode: 1}

Default holds Exit setting for the Log and Logf functions.

Functions

func Code

func Code(code int)

Code runs any deferred functions, then exits the program with the given exit code.

func Handler

func Handler()

Handler is the fatal error handler. Defer at the start of main() before calling any of Code, Log, Logf, Exit.Log, or Exit.Logf.

When Handler is deferred first, Go ensures that all other deferred functions are run first. Handler then handles paincs originating from Code, Log, Logf, Exit.Log, or Exit.Logf by logging a message and then exiting with an exit code.

There are a couple of frills. Empty messages are not logged; the program simply exits with the specified code. With non-empty messages, if a logger is not specified and stderr seems to be directed to a terminal, Handler sets logging flags 0 to suppress the usual date and time stamp.

func Log

func Log(args ...interface{})

Log runs any deferred functions, logs a message as with log.Println, then exits the program with an exit code.

The logger and exit code used are those of exit.Default.

func Logf

func Logf(format string, args ...interface{})

Logf calls any deferred functions, logs a message as with log.Printf, then exits the program with an exit code.

The logger and exit code used are those of exit.Default.

Types

type Exit

type Exit struct {
	ExitCode int
	*log.Logger
}

Exit specifies an exit code and logger for logging a final message.

If Logger is nil, the log package default logger is used.

func (Exit) Log

func (x Exit) Log(args ...interface{})

Log runs any deferred functions, logs a message as with log.Println, then exits the program with an exit code.

The logger and exit code used are those of the receiver x.

func (Exit) Logf

func (x Exit) Logf(format string, args ...interface{})

Logf calls any deferred functions, logs a message as with log.Printf, then exits the program with an exit code.

The logger and exit code used are those of the receiver x.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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