You can use something like `go build -gcflags="-m -m" ...` to get more
information about why something escape. David Chase has added excellent
descriptions of why something escapes.
There's a lot of detail about fmt.Print functions escape, in general, but
without going into detail, we can look at the functions themselves.
rand.Intn has a signature of `func Intn(n int) int`. To start, non-pointer
type parameters never escape unless their address is taken, which is not
the case for rand.Intn.
fmt.Print has a signature of `func Print(a ...interface{}) (n int, err
error)`, that is, it takes in a slice of interface values. This means `v`,
of type `int`, must be converted into an interface value and a slice must
be allocated to hold it. Later on, `printArg` is called with that
interface{} argument. `printArg` makes various calls, some of which escape
from `printArg`, meaning that arg must be heap-allocated in order to ensure
calls to `printArg` are always valid.
Hope that helps.
Thanks,
Chris
On Wed, Aug 31, 2016 at 8:06 AM, T L <[email protected]> wrote:
>
> Why does the parameter passed to fmt.Print escape to heap?
>
>
> // 11.go
> package main
> import "fmt"
> import "math/rand"
>
> func f1(v int) {
> fmt.Print(v) // v escapes to heap
> }
>
> var a int
> func f2(v int) {
> a = rand.Intn(v) // v doesn't escape
> }
>
> func main() {
> }
>
> /*
> > go build -gcflags=-m 11.go
> # command-line-arguments
> ./11.go:15: can inline main
> ./11.go:7: v escapes to heap
> ./11.go:7: f1 ... argument does not escape
> */
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.