这是我的环境:
[lorneli@localhost GoTest]$ go version
go version go1.9 linux/amd64
Run Code Online (Sandbox Code Playgroud)
这是我的计划:
package main
type request struct {
ID string
size uint32
off uint64
}
func main() {
r := request{}
iter := interface{}(&r) // &r escapes to heap
iters := make([]interface{}, 0)
iters = append(iters, iter)
}
Run Code Online (Sandbox Code Playgroud)
我分配一个request实例并将其指针转换为interface{}.但是在使用flag进行分析时-gcflags "-m",我发现转换时实例会转义为堆.为什么会这样?
这是分析结果:
[lorneli@localhost GoTest]$ go build -gcflags "-m"
# _/mnt/hgfs/vmfolder/workspace/GoTest
./main.go:9:6: can inline main
./main.go:11:21: (interface {})(&r) escapes to heap
./main.go:11:22: &r escapes to heap
./main.go:10:15: moved …Run Code Online (Sandbox Code Playgroud)