Sobes.tech
Junior — Middle

How to determine the current size or load used by a goroutine in Go?

sobes.tech AI

Answer from AI

In Go, the memory allocation for a goroutine starts with a small stack (usually around 2 KB). The stack grows dynamically during execution when more memory is needed for function calls or local variables.

This happens automatically: if the current stack is insufficient, the Go runtime allocates a new, larger stack, copies data, and continues execution. This approach allows goroutines to have very small stacks that grow as needed.

Example:

func recursive(n int) {
    if n > 0 {
        recursive(n-1) // stack will grow with deep recursion
    }
}

If the call depth becomes large, the goroutine's stack will automatically expand.