首次提交:初始化项目代码

This commit is contained in:
sunct
2026-07-31 15:18:32 +08:00
commit d6393266b0
193 changed files with 14287 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
package main
import "fmt"
type Person4 struct {
Name string
Age int
}
// closureEscape 返回闭包,捕获的变量逃逸
func closureEscape() func() *Person4 {
p := &Person4{Name: "Closure Person", Age: 30} // p 逃逸至堆
return func() *Person4 {
return p // 闭包引用p,延长其生命周期
}
}
// goroutineClosure 协程中使用闭包,捕获变量逃逸
func goroutineClosure() {
x := 100 // x 逃逸至堆
go func() {
fmt.Printf("协程闭包捕获:%d\n", x)
}()
}
func main() {
closureFunc := closureEscape()
p := closureFunc()
fmt.Printf("闭包捕获变量:%+v\n", *p)
goroutineClosure()
}