Search code examples
goshutdown-hookapplication-shutdown

Is there something like finally() in Go just opposite to what init()?


Is there something in Go which do just opposite to what init() do inside a package?


Solution

  • This was discussed before by the Go team, and the conclusion was not to add support for it. Quoting minux:

    Personally, I prefer the style where program exit is handled exactly same as program crash. I believe no matter how hard you try, your program can still crash under some unforeseen situations; for example, memory shortage can bring any well-behave Go program to a crash, and there is nothing you can do about it; so it's better to design for them. If you follow this, you won't feel the need for atexit to clean up (because when your program crash, atexit won't work, so you simply can't depend on it).

    But you still have some options:

    Handling CTRL+C

    If you want to do something when your program is terminated by CTRL+C (SIGINT), you can do so, see:

    Golang: Is it possible to capture a Ctrl+C signal and run a cleanup function, in a "defer" fashion?

    Object Finalizer

    Also note that you can register a finalizer function for a pointer value. When the garbage collector finds an unreachable block with an associated finalizer, it clears the association and runs f(x) in a separate goroutine.

    You can register such finalizer with runtime.SetFinalizer() which might be enough for you, but note:

    There is no guarantee that finalizers will run before a program exits, so typically they are useful only for releasing non-memory resources associated with an object during a long-running program.

    See this example:

    type Person struct {
        Name string
        Age  int
    }
    
    func main() {
        go func() {
            p := &Person{"Bob", 20}
            runtime.SetFinalizer(p, func(p2 *Person) {
                log.Println("Finalizing", p2)
            })
            runtime.GC()
        }()
    
        time.Sleep(time.Second * 1)
        log.Println("Done")
    }
    

    Output (Go Playground):

    2009/11/10 23:00:00 Finalizing &{Bob 20}
    2009/11/10 23:00:01 Done