go-prompt
go-prompt copied to clipboard
[Feature Request] Call arbitrary code on exit
Feature Request
Currently, it seems that there is no way to execute some code between the moment the program receives an exit signal / ctrl D and the call to os.Exit(code)
.
It would be very useful to be able to call some arbitrary code right before termination (e.g. closing resources, displaying a last message, flushing history, etc.)
This can be achieved using Go's defer
. When you exit the program Go makes sure to call any functions pushed on to the stack via defer and so you can do something like so:
func handleExit() {
fmt.Println("Exiting...")
}
func main() {
defer handleExit()
p := prompt.New(
executor,
completer,
)
p.Run()
}