Skip to main content

Command Palette

Search for a command to run...

🧠 How Closures Work Under the Hood in Go.

Updated
â€ĸ2 min read
🧠 How Closures Work Under the Hood in Go.
M

A self-motivated and enthusiastic web developer with a deep interest in JavaScript (React.js). To work in the Software industry with modern web technologies of different local & multinational Software/ IT agencies of Bangladesh and grow rapidly with increasing responsibilities.

🧩 Go Closures āĻāϰ āĻ­āĻŋāϤāϰ⧇āϰ āĻ•āĻžāϜ – Struct, Heap Allocation & Memory Management

Go āϤ⧇ closure āĻŽāĻžāύ⧇āχ āĻļ⧁āϧ⧁ āĻāĻ•āϟāĻž anonymous function āύ⧟ — āĻāϰ āϏāĻžāĻĨ⧇ āĻĨāĻžāϕ⧇ āĻāĻ•āϟāĻž hidden struct, āϝāĻž captured variables āϧāϰ⧇ āϰāĻžāϖ⧇āĨ¤

āφāϜ āφāĻŽāĻŋ Go closure āĻāϰ under-the-hood memory behavior āύāĻŋā§Ÿā§‡ āĻŦāĻŋāĻ¸ā§āϤāĻžāϰāĻŋāϤ āφāϞ⧋āϚāύāĻž āĻ•āϰāĻŦ āĨ¤

🔹 āϏāĻžāϧāĻžāϰāĻŖ Go Closure āωāĻĻāĻžāĻšāϰāĻŖ:

func multiplier(factor int) func(int) int {
    return func(n int) int {
        return n * factor
    }
}

āĻāĻ–āĻžāύ⧇ factor closure āĻāϰ āĻ­āĻŋāϤāϰ captured āĻšā§Ÿā§‡āϛ⧇āĨ¤ āϤāĻžāϰ āĻŽāĻžāύ⧇ factor āĻāϰ value ke closure āĻšāĻŋāϏ⧇āĻŦ⧇ āϰ⧇āϖ⧇ āĻĻāĻŋāϤ⧇ āĻšāĻŦ⧇ āϝāĻžāϤ⧇ , stack Frame āĻĨ⧇āϕ⧇ multiplier function āϚāϞ⧇ āϗ⧇āϞ⧇ factor āĻāϰ āĻ­ā§āϝāĻžāϞ⧁ āύāĻž āĻšāĻžāϰāĻŋā§Ÿā§‡ āϝāĻžā§Ÿ āĨ¤

āĻāĻŦāĻžāϰ āφāϏ⧁āύ āĻĻ⧇āĻ–āĻŋ ,
🔍 Go Under the Hood āĻ•āĻŋ āĻ•āϰ⧇?

  • āϝāĻ–āύ āφāĻŽāϰāĻž go run build āĻ•āϰāĻŋ āĨ¤ Go compiler automatic āĻ­āĻžāĻŦ⧇ āĻāĻ•āϟāĻž hidden struct āϤ⧈āϰāĻŋ āĻ•āϰ⧇, āϝāĻž closure captured variables āϰāĻžāϖ⧇, āϝ⧇āĻŽāύ:
type multiplierEnv struct {
    factor int
}

func (e *multiplierEnv) closure(n int) int {
    return n * e.factor
}
  • āϝāĻ–āύ closure stack frame āĻāϰ āĻŦāĻžāχāϰ⧇ āϝāĻžā§Ÿ (e.g. return āĻšā§Ÿ), āϤāĻ–āύ Go āĻāϰ escape analysis āĻ•āϰ⧇ āĻŦāϞ⧇ āϝ⧇ āĻāχ struct āϟāĻŋ heap āĻ allocate āĻ•āϰāϤ⧇ āĻšāĻŦ⧇āĨ¤

  • āĻ…āĻ°ā§āĻĨāĻžā§Ž captured variables āφāϰ stack āĻ āĻĨāĻžāϕ⧇ āύāĻž, āĻŦāϰāĻ‚ heap āĻ āϚāϞ⧇ āϝāĻžā§Ÿ, āϝāĻžāϤ⧇ closure function lifetime āϝāϤāĻĻāĻŋāύ āĻĨāĻžāϕ⧇, variables āĻ“ āϤāϤ āϏāĻŽā§Ÿ āĻŦ⧇āĻšā§‡ āĻĨāĻžāϕ⧇āĨ¤

💡 Escape Analysis āύāĻŋāĻļā§āϚāĻŋāϤ āĻ•āϰāϤ⧇:

go build -gcflags="-m" main.go

Output āĻ āĻĻ⧇āĻ–āĻŦ⧇āύ
moved to heap: factor func literal escapes to heap

āϤāĻžāϰ āĻŽāĻžāύ⧇:

  • factor heap āĻ āϚāϞ⧇ āϗ⧇āϛ⧇āĨ¤

  • closure function āĻ“ heap allocatedāĨ¤

✨ āϕ⧇āύ Closure āĻ Struct āϞāĻžāϗ⧇?

  • Closure āϕ⧇ context āĻŦāĻž environment (captured variables) āĻĻāϰāĻ•āĻžāϰ āĻšā§Ÿ āϝāĻž function call āĻāϰ āĻŦāĻžāχāϰ⧇ āĻ“ āϝ⧇āύ āϤāĻžāϰ āĻ…āĻ¸ā§āĻĨāĻŋāϤāĻŦ āϟāĻŋāϕ⧇ āĻĨāĻžāϕ⧇āĨ¤

  • āĻāχ āϜāĻ¨ā§āϝ Go āĻāĻ•āϟāĻŋ struct āϤ⧈āϰāĻŋ āĻ•āϰ⧇ āϝāĻžāϤ⧇ āϏ⧇ context āϧāϰ⧇ āϰāĻžāĻ–āϤ⧇ āĻĒāĻžāϰ⧇āĨ¤


👉 Summary:

Anonymous function + captured variables āϕ⧇ āĻāĻ•āϏāĻžāĻĨ⧇ āϰāĻžāĻ–āĻžāϰ āϜāĻ¨ā§āϝ hidden struct + heap allocation (if escapes) + GC managementāĨ¤

āĻāϟāĻž Go āĻāϰ simplicity āφāϰ power āĻāϰ āĻāĻ•āϟāĻž āĻŦ⧜ āĻ•āĻžāϰāĻŖ āĻšāϞ⧋ āĻāχ ClosureāĨ¤

Morshedul islam Munna
- Software Engineer
Linkedin: https://www.linkedin.com/in/morshedulmunna/
Email: morshedulmunna1@gmail.com

More from this blog

DSA āĻŽāĻžāύ⧇ āĻļ⧁āϧ⧁ LeetCode āύāĻž — DSA āĻŽāĻžāύ⧇ Production System slow āύāĻž āĻ•āϰāĻž

āĻ…āύ⧇āϕ⧇āχ āĻŦāϞ⧇ — “āĻ­āĻžāχ, Web Development-āĻ DSA āϞāĻžāϗ⧇ āύāĻžâ€ āφāϏāϞ⧇ Junior Developer āĻĨāĻžāĻ•āϞ⧇ āĻāĻŽāύāϟāĻžāχ āĻŽāύ⧇ āĻšāϝāĻŧāĨ¤āφāĻŽāĻŋ āύāĻŋāĻœā§‡āĻ“ Junior āĻĨāĻžāĻ•āϤ⧇ āϤāĻžāχ āĻ­āĻžāĻŦāϤāĻžāĻŽāĨ¤ āφāĻŽāĻžāϰ āφāĻŦāĻžāϰ āĻāĻ•āϟāĻž āĻŦāĻžāĻœā§‡ āĻ¸ā§āĻŦāĻ­āĻžāĻŦ āφāϛ⧇ 😅👉 āϕ⧋āύ āĻ•āĻŋāϛ⧁āϰ āĻŦāĻžāĻ¸ā§āϤāĻŦ āĻĒā§āϰāϝāĻŧā§‹āϜāύ āύāĻž āĻŦ⧁āĻāϞ⧇ āϏ⧇āϟāĻž āφāĻŽāĻžāϰ āĻŽāĻžāĻĨāĻžāϝāĻŧ āĻĸ⧁āϕ⧇ āύāĻžāĨ¤ Junior āĻĨāĻžāĻ•āϤ⧇ DSA āĻļāĻŋāϖ⧇āĻ›āĻŋ...

Dec 24, 20253 min read
DSA āĻŽāĻžāύ⧇ āĻļ⧁āϧ⧁ LeetCode āύāĻž — DSA āĻŽāĻžāύ⧇ Production System slow āύāĻž āĻ•āϰāĻž

đŸĻ€ Rust-āĻ āĻŽā§‡āĻŽāϰāĻŋ āĻŽā§āϝāĻžāύ⧇āϜāĻŽā§‡āĻ¨ā§āϟ: GC āĻ›āĻžā§œāĻž In-depth.

Rust-āĻ Garbage Collector (GC) āύ⧇āχ, āĻ•āĻŋāĻ¨ā§āϤ⧁ āĻŽā§‡āĻŽāϰāĻŋ āύāĻŋāϰāĻžāĻĒāĻ¤ā§āϤāĻž (memory safety) āφāϰ performance Rust-āĻāϰ āϏāĻŦāĻšā§‡ā§Ÿā§‡ āĻļāĻ•ā§āϤāĻŋāĻļāĻžāϞ⧀ āĻĻāĻŋāĻ•āĨ¤āϤāĻžāχ, Go-āĻāϰ āĻŽāϤ⧋ runtime-based GC āύāĻž āĻĨāĻžāĻ•āĻž āϏāĻ¤ā§āĻ¤ā§āĻŦ⧇āĻ“ Rust compile-time āĻ āĻŽā§‡āĻŽāϰāĻŋ āĻŽā§āϝāĻžāύ⧇āϜ āĻ•āϰ⧇ Ownership System, Borrow Checker, āφāϰ Lifetime...

Oct 5, 202523 min read
đŸĻ€ Rust-āĻ āĻŽā§‡āĻŽāϰāĻŋ āĻŽā§āϝāĻžāύ⧇āϜāĻŽā§‡āĻ¨ā§āϟ: GC āĻ›āĻžā§œāĻž In-depth.

Morshedul Munna

45 posts

As a Software Developer, I am like an architect who designs and builds digital Products. I use my knowledge and expertise to create modern applications that are both efficient and elegant.