Finalisers execute clean-up operations before the garbage collector deallocates an object. They are invoked by the garbage collector as soon as it notices an object is not reachable.
Used to free non-memory resources:
- Network connections
- Database handlers
- File descriptors
NOTE: Go does not guarantee that a finaliser will be run - for example when os.Exit()
is called. This means that finalisers are not determinisitic and add to complexity.