Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 240
Prashant Avatar asked Jun 04 '15 09:06

Prashant


People also ask

Is INIT called before main Golang?

Golang init() In Go, init() is a very special function meant to execute prior to the main() function. Unlike main(), there can be more than one init() function in a single or multiple files.

Can you define multiple init () functions in the same .Go file?

Multiple Init Functions in the Same File At first I didn't think this was possible, but Go does indeed support having 2 separate init() functions within the one file.

When init function is called?

init() Function This function is present in every package and this function is called when the package is initialized. This function is declared implicitly, so you cannot reference it from anywhere and you are allowed to create multiple init() function in the same program and they execute in the order they are created.


Video Answer


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
like image 147
icza Avatar answered Sep 19 '22 11:09

icza