Go: Finalizers

Vincent Blanchon
Jul 20, 2019 · 4 min read
Illustration created for “A Journey With Go”, made from the original Go Gopher, created by Renee French.

ℹ️ This article is based on Go 1.12.

Go runtime provides a method runtime.SetFinalizer that allows developers to attach a function to a variable that will be called when the garbage collector sees this variable as garbage ready to be collected since it became unreachable. This feature is highly subject to debate and this article does not aim to participate in it, but rather to explain the implementation of the method.