Golang 2 min read

Anonymous Function in GO Language

Takdirul Islam Shishir

Takdirul Islam Shishir

Anonymous Function in GO Language

Anonymous functions in Go: declaring functions without names, passing arguments in parenthesis, and using them inside range loops.

An anonymous function is a cute little relationship, which is complicated and doesn't have any name that has only love in arguments. You just want to hide it from anyone.

Okay, Let's come to the point. In Go language, we have the anonymous function. You will not normally do this, but you can use it if you want to check your code, routines, and launching goroutines.

Internal functions are anonymous functions, they don't have any function name and we also don't need to assign them to a variable, Just write and ROCK N ROLL.

Example :

package main
import "fmt"
func main (){
names:= []string{"Billu", "Killu", "Dillu"}
for _,value := range names{
// Declearing an anonymous function
func (arg string)  {
fmt.Println("Names are ", arg, " in anonymous function " )
}(value)
}
}

Definition of Example:

We made a slice of names, then wrote a For loop to get values from the slice, inside For loop we have declared an anonymous function without any name just a parameter and we have passed the argument in parenthesis.

Let me know if there is any missing information or mistake, I just moved to Go from Python.

Need Personal Support?

We provide custom solutions and support. Contact US at https://solution.omega.ac

Filed under Golang

← All posts

Keywords: golang, go, anonymous-function, goroutines