Stack vs heap allocation of structs in Go, and how they relate to garbage collection

前端 未结 5 1070
谎友^
谎友^ 2020-11-30 16:19

I\'m new to Go and I\'m experiencing a bit of congitive dissonance between C-style stack-based programming where automatic variables live on the stack and allocated memory l

5条回答
  •  不知归路
    2020-11-30 16:48

    You don't always know if your variable is allocated on the stack or heap.
    ...
    If you need to know where your variables are allocated pass the "-m" gc flag to "go build" or "go run" (e.g., go run -gcflags -m app.go).

    Source: http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/index.html#stack_heap_vars

提交回复
热议问题