Is there something in Go which do just opposite to what init()
do inside a package?
1 Answers
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
-
1I'd argue that (ab)using finalizers to do "user-space" cleanups is a clear code smell. Instead, one should supposedly design the whole application around the idea of controlled shutdown, and then perform that controlled shutdown when certain conditions are met (like receiving `SIGINT` or `SIGTERM`). [This](http://blog.labix.org/2011/10/09/death-of-goroutines-under-control) is a good starting point. – kostix Jun 04 '15 at 10:35
-
[See also](http://www.reddit.com/r/golang/comments/1y5fg1/critique_graceful_http_shutdown/). – kostix Jun 04 '15 at 10:36
-
2I'd also add that creating a package that needs an "atexit" hook is a bad design whatsoever. The package should just export "something" which the user has to initialize explicitly and deinitialize explicitly, and then just let the user to that -- when it's really needed. – kostix Jun 04 '15 at 10:38
-
1@kostix I agree, it's bad design to rely on such functions, that's why there is no first-class support for it. Just showing the available options. – icza Jun 04 '15 at 10:42