seniorGolang
What are finalizers in Go (`runtime.SetFinalizer`)?
Updated Apr 28, 2026
Short answer
Finalizers attach a function to an object that the Garbage Collector calls before sweeping the object from memory.
Deep explanation
They are akin to destructors in C++/C#. They are primarily used in Go for cleaning up CGO resources (C pointers) that the Go GC doesn't know how to free. They are highly unpredictable; the GC makes no guarantees about exactly when (or even if) a finalizer will run before the program exits.
Unlock with a Pro subscription to view this section.
View pricingReal-world example
No real-world example available yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProCommon mistakes
No common mistakes listed yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProFollow-up questions
No follow-up questions available yet.
Unlock with a Pro subscription to view this section.
Upgrade to Pro