PS D:\Code\project\study_go\base\escape> go build -gcflags=-m # study_go/base/escape .\main.go:8:6: can inline NewStudent .\main.go:18:6: can inline main .\main.go:19:12: inlining call to NewStudent .\main.go:8:17: leaking param: name .\main.go:12:9: &Student{...} escapes to heap .\main.go:19:12: &Student{...} does not escape PS D:\Code\project\study_go\base\escape>
PS D:\Code\project\study_go\base\escape> go build -gcflags=-m # study_go/base/escape .\main.go:19:6: can inline slice .\main.go:27:6: can inline main .\main.go:33:7: inlining call to slice .\main.go:20:11: make([]int, 1000, 1000) does not escape .\main.go:33:7: make([]int, 1000, 1000) does not escape
没有发生情况。那如果我把代码slice()函数切片分配的长度修改为10000后再编译呢?
1 2 3 4 5 6 7
PS D:\Code\project\study_go\base\escape> go build -gcflags=-m # study_go/base/escape .\main.go:19:6: can inline slice .\main.go:27:6: can inline main .\main.go:33:7: inlining call to slice .\main.go:20:11: make([]int, 1000, 10000) escapes to heap .\main.go:33:7: make([]int, 1000, 10000) escapes to heap
Go的最新版本都将 interface{} 修改为 any 了,但实际上也就是把 interface{} 起了一个类型别名:type any interface{}
1 2 3 4
func main() { s := "escape" fmt.Println(s) }
编译查看结果
1 2 3 4 5
PS D:\Code\project\study_go\base\escape> go build -gcflags=-m # study_go/base/escape .\main.go:37:13: inlining call to fmt.Println .\main.go:37:13: ... argument does not escape .\main.go:37:13: s escapes to heap