How fmt.print() affects memory allocation?


First, I’m new to Go language.

I came across an example that demonstrates how stack growth works. The code simply passes an [1024]int array and an arbitrary string to a recursive function to bloat stack memory.
So when it exceeded its size, the stack will be reallocated, the size is doubled every reallocation. We know the reallocation taking place by looking at that arbitrary string’s address change.

However, if we use standard library print - fmt.print() instead (or both built-in and fmt), the demonstration fails.

The only explanation that I can think of is that the Go decides to allocate that string in the heap during compile-time if we use fmt.print. But what causes that?

I’ve been perplexed for three days, still not yet figure out why.

Passing something as an interface-typed parameter generally causes it to escape, as the compiler can’t see much about how the values is going to be used or retained in the called function.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.