-
-
Save hxzhouh/ca402c723faa78726baba7e56bff573a to your computer and use it in GitHub Desktop.
AddCleanup compare SetFinalizer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
里面两个打印写错了吧?x应该是name吧?