go build -gcflags '-m -l' main.go

通过编译参数可获知变量逃逸信息

func hello(x []byte) int {
    return len(x)
}

func main() {
    b := make([]byte, 10)
    b[3] = 'd'
    fmt.Println(hello(b))
}

# command-line-arguments
./main.go:5:12: x does not escape
./main.go:10:11: make([]byte, 10) does not escape
./main.go:12:13: ... argument does not escape
./main.go:12:19: hello(b) escapes to heap

标签: none

添加新评论