dwburke / atexit

Go package to run things when program ends

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

atexit

Quick and dirty docs, will fill out later...

My primary use case is for embedded services, and loggers... it's tedious to make sure everything properly exits / closes files, etc... with atexit you can "register" a function to run at program exit in other packages.

It's not perfect, but it's "good enough" for me, hopefully it's good enough for you.

Pull requests welcome.

cmd/root.go

import "github.com/dwburke/atexit"

func init() {
    cobra.OnInitialize(initConfig)
    cobra.OnInitialize(natsqueue.Run)

    // there's other things going on there
    ...

}

main.go

func main() {
    defer atexit.AtExit()
    cmd.Execute()
}

natsqueue/server.go

func Execute() {
    // initializer code here
    ...

    atexit.Add(Shutdown)
}

func Shutdown() {
    // do shutdown things
    ...
}

About

Go package to run things when program ends


Languages

Language:Go 100.0%