Hello, I'm Ganesh Kumar. I'm working on git-lrc: a Git hook for Checking AI generated code.AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.
In my previous post, I explained how anonymous functions created and understand how it works.
In this article we will understand how closure.
What is closure
A closure is a function value that references variables from outside its body.
The function may access and assign to the referenced variables; in this sense the function is "bound" to the variables.
How closure is used?
Each time new value updates the sum variable.
Next time when we call the function, it will use the updated value of sum.
So, It remembers the value of sum.
package main
import "fmt"
func adder(x int) func(int) int {
sum := 0
return func(y int) int {
sum += y
return sum
}
}
func main() {
pos,neg := adder(0),adder(0)
for i := 0; i < 10; i++ {
fmt.Println(
pos(i),
neg(-i),
)
}
}
Output:
gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
0 0
1 -1
3 -3
6 -6
10 -10
15 -15
21 -21
28 -28
36 -36
45 -45
Fibonacci series
Here is the example of fibonacci series.
initially we are initializing a and b with 0 and 1.
then with each time calling the function it will return the return value of a.
and update the value of a and b.
So, hence creating a fibonacci series.
package main
import "fmt"
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
a, b := 0, 1
return func() int {
ret := a
a, b = b, a+b
return ret
}
}
func main() {
f := fibonacci()
for i := 0; i < 10; i++ {
fmt.Println(f())
}
}
Output:
gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go
0
1
1
2
3
5
8
13
21
34
Conclusion
In this article we understood how closure is used. closure is a function value that references variables from outside its body.
π Check out: git-lrc
Any feedback or contributors are welcome! Itβs online, open-source, and ready for anyone to use.
β Star it on GitHub:
HexmosTech
/
git-lrc
Free, Unlimited AI Code Reviews That Run on Commit
AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.
git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.
See It In Action
See git-lrc catch serious security issues such as leaked credentials, expensive cloud operations, and sensitive material in log statements
git-lrc-intro-60s.mp4
Why
- π€ AI agents silently break things. Code removed. Logic changed. Edge cases gone. You won't notice until production.
- π Catch it before it ships. AI-powered inline comments show you exactly what changed and what looks wrong.
- π Build a habit, ship better code. Regular review β fewer bugs β more robust code β better results in your team.
- π Why git? Git is universal. Every editor, every IDE, every AIβ¦

Top comments (0)