Skip to content

Instantly share code, notes, and snippets.

@hxzhouh

hxzhouh/main.go Secret

Created December 17, 2024 10:22
Show Gist options
  • Select an option

  • Save hxzhouh/ca402c723faa78726baba7e56bff573a to your computer and use it in GitHub Desktop.

Select an option

Save hxzhouh/ca402c723faa78726baba7e56bff573a to your computer and use it in GitHub Desktop.
AddCleanup compare SetFinalizer
func main() {
x := MyStruct{Name: "X"}
y := MyStruct{Name: "Y"}
x.Other = &y
y.Other = &x
//runtime.SetFinalizer(&x, func(x *MyStruct) {
// fmt.Printf("Finalizer for %s is called\n", x.Name)
//})
//runtime.SetFinalizer(&y, func(y *MyStruct) {
// fmt.Printf("Finalizer for %s is called\n", y.Name)
//})
xName := x.Name
runtime.AddCleanup(&x, func(name string) {
fmt.Println("Cleanup for", x)
}, xName)
yName := y.Name
runtime.AddCleanup(&y, func(name string) {
fmt.Println("Cleanup for", x)
}, yName)
time.Sleep(time.Millisecond)
runtime.GC()
time.Sleep(time.Millisecond)
runtime.GC()
}
@578559967
Copy link

里面两个打印写错了吧?x应该是name吧?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment