DEV Community

Cover image for Understanding Closures in Go
Ganesh Kumar
Ganesh Kumar

Posted on

Understanding Closures in Go

#go

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),
        )
    }

}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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())
    }
}


Enter fullscreen mode Exit fullscreen mode

Output:

gk@jarvis:~/exp/code/rd/go-exmaple$ go run main.go 
0
1
1
2
3
5
8
13
21
34
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this article we understood how closure is used. closure is a function value that references variables from outside its body.

git-lrc

πŸ‘‰ 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:

GitHub logo HexmosTech / git-lrc

Free, Unlimited AI Code Reviews That Run on Commit

git-lrc logo

git-lrc

Free, Unlimited AI Code Reviews That Run on Commit


git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

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)