DEV Community

Cover image for Understanding Anonymous Functions in Go
Ganesh Kumar
Ganesh Kumar

Posted on

Understanding Anonymous Functions 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 what are maps and how to create them.

Now let's see how anonymous functions created and understand how it works.

Anonymous functions

We assign a function to a variable and then call it.


package main

import "fmt"


func main() {
    data := func() {
        fmt.Println("Hello World")
    }

    data()
}
Enter fullscreen mode Exit fullscreen mode
Hello World
Enter fullscreen mode Exit fullscreen mode

Similarly we can also pass arguments to the anonymous function.


package main

import "fmt"


func main() {
    data := func(a,b string) {
        fmt.Println(a,b)
    }

    data("dev","to")
}
Enter fullscreen mode Exit fullscreen mode

Output:

dev to
Enter fullscreen mode Exit fullscreen mode

Passing function as an argument

We are passing a function as an argument to another function.


package main

import "fmt"

func anonymous(a func()) {
    a()
}
func main() {
    data := func() {
        fmt.Println("Hello World")
    }
    anonymous(data)
}
Enter fullscreen mode Exit fullscreen mode

Output:

Hello World
Enter fullscreen mode Exit fullscreen mode

If we want to pass arguments to the anonymous function, we can do it like this:


package main

import "fmt"

func anonymous(a func(string,string)) {
    a("dev","to")
}
func main() {
    data := func(a,b string) {
        fmt.Println(a,b)
    }
    anonymous(data)
}
Enter fullscreen mode Exit fullscreen mode

As is the print statement is inside the anonymous function, it is printing the values passed to it.

Output:

dev to
Enter fullscreen mode Exit fullscreen mode

Conclusion

We understood how the anonymous functions are created and how to pass them as arguments to another function.

As go has the feature of passing functions as arguments to another function, go provides a feature called closure.

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…




https://github.com/golang/go/blob/master/src/runtime/slice.go

Top comments (0)