Anonymous Functions in Go

π Hey there!
I'm a community-driven software engineer, and I absolutely love diving into the world of cloud-native development and exploring the endless possibilities of open-source technologies. π»
My skill set revolves around Kubernetes, GoLang, Python, Docker, and other fascinating container technologies. π And hey, just to add to the mix, I'm also CKA certified! π
But you know what? My journey doesn't stop at coding. I have a genuine passion for technical evangelism. π€ I've had the privilege to speak at world-renowned events, sharing my knowledge and insights with others. It's such an exhilarating experience! And when I'm not on stage, you can find me pouring my thoughts into engaging technical blogs. π
If you've made it this far and you're intrigued by what you've read, let's not leave it at that! Reach out, and let's connect. Who knows what exciting opportunities may be awaiting us? π
The anonymous function is a feature in Golang which let us define a function without a name. This feature is also called a function literal. This is useful when you want an inline function or to form a closure.
Declaring the anonymous function
The syntax is pretty straightforward and much similar to normal function.
func(parameter_list)(return_type){
return
}()
Parameter list and return type are optional.
() this will invoke the function as soon as it is defined.
package main
import "fmt"
func main() {
func() {
fmt.Println("Golang Rocks!")
}()
}
Run this code in Go Playground
Assigning an anonymous function to a variable
In Go, you can assign an anonymous function to a variable. The assigned variable will be of function type and it can be called as a regular function.
package main
import "fmt"
func main() {
v := func() {
fmt.Println("Golang Rocks!")
}
fmt.Printf("Type of variable v: %T\n", v)
v()
}
Type of variable v: func()
Golang Rocks!
Run this code in Go Playground
Passing an argument to an anonymous function
An anonymous function can take any number of arguments similar to a regular function.
package main
import "fmt"
func main() {
func(name string) {
fmt.Println("Hello, ", name)
}("Jack")
}
Run this code in Go Playground
With any number of trailing arguments similar to Variadic functions
package main
import "fmt"
func main() {
func(i ...int) {
fmt.Println(i)
}(1, 2, 3, 4)
}
Run this code in Go Playground
Passing an anonymous function as an argument
You can pass an anonymous function as an argument to a regular function or an anonymous function.
- Anonymous functions as an argument to a regular function
package main
import "fmt"
func sayHello(af func(s string) string) {
fmt.Println(af("Jack"))
}
func main() {
sayHello(func(s string) string {
return "Hello, " + s
})
}
Run this code in Go Playground
- Anonymous function as an argument to an anonymous function
package main
import "fmt"
func main() {
func(v string) {
fmt.Println(v)
}(func(s string) string {
return "Hello, " + s
}("Jack"))
}
Run this code in Go Playground
This above code looks a bit complicated and hard to fathom so another way we can achieve this is the following:
package main
import "fmt"
func main() {
af := func(s string) string {
return "Hello, " + s
}
func(af func(s string) string) {
fmt.Println(af("Jack"))
}(af)
}
Run this code in Go Playground
Return an anonymous function from another function
We can return an anonymous function from another function
- Returning from regular function
package main
import "fmt"
func sayHello() func(s string) string {
r := func(s string) string {
return "Hello, " + s
}
return r
}
func main() {
f := sayHello()
fmt.Printf("Type of variable f: %T\n", f)
fmt.Println(f("Jack"))
}
Run this code in Go Playground
- Returning from the anonymous function
package main
import "fmt"
func main() {
f := func() func(s string) string {
r := func(s string) string {
return "Hello, " + s
}
return r
}
fmt.Printf("Type of variable f: %T\n", f)
c := f()
fmt.Printf("Type of variable c: %T\n", c)
fmt.Println(c("Jack"))
}
Run this code in Go Playground
Thank you for reading this blog, and please give your feedback in the comment section below.




