Anonymous goroutines in Golang

Go ProgrammingServer Side ProgrammingProgramming

In order to be able to understand the anonymous goroutines, we must be aware of the existence of anonymous functions and goroutines. We will first explore the anonymous functions that are the real reason behind the motivation of anonymous goroutines and then we will learn a little about what goroutines are, before finally checking a few examples of anonymous goroutines.

Anonymous functions

In Golang, anonymous functions are those functions that don't have any name. Simply put, anonymous functions don't use any variables as a name when they are declared.

We know that we declare a function with a similar syntax as shown below.

func Sample(){
// some code
}

While we do have a name for the above function (Sample), in the case of anonymous functions, we don't have one.

Example 1

A very simple example of an anonymous function is shown below.

package main
import (
"fmt"
)
func main() {
func(name string) {
fmt.Println("Welcome to", name)
}("TutorialsPoint")
}

In the above code, we have an anonymous function inside the main function of our Go program. This anonymous function takes one parameter and the argument is passed as soon as the code block of the function ends.

Output

If we run this code, with the command go run main.go, we will see the following output printed in the terminal.

Welcome to TutorialsPoint

goroutines in Golang

A goroutine is an extremely lightweight thread, managed by the Go runtime. Using goroutines, we can write asynchronous parallel programs that can execute some tasks far more quickly than sequential programs would.

Goroutines help us in making the programs asynchronous in nature and let us accomplish more in less time and with fewer resources.

So, the idea behind an anonymous goroutine is that it is simply an anonymous function that runs on a separate goroutine from which it was invoked.

Example 2

Let's take a very basic example of an anonymous goroutine to understand it better.

package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("before the anonymous goroutine")
go func(name string) {
fmt.Println("Welcome to", name)
}("TutorialsPoint")
time.Sleep(time.Second)
fmt.Println("after the anonymous goroutine")
}

In the above example, we are using an anonymous goroutine and to make sure the main function doesn't finish before the goroutine does its job, we also wrote a time.Sleep() function which will delay the second fmt.Println() by 1 second.

Output

If we run the above code with the help of the go run main.go command, then we will see the following output.

before the anonymous goroutine
Welcome to TutorialsPoint
after the anonymous goroutine

Instead of writing a time.Sleep() function, we can also use any function that will block the main goroutine which will give enough time to the anonymous goroutine inside the main function to run properly.

Example 3

Consider the code shown below.

package main
import (
"fmt"
)
func main() {
func(name string) {
fmt.Println("Welcome to", name)
}("TutorialsPoint")
fmt.Scanln() // blocking call
}

Output

If we run the above code with the help of the go run main.go command, then we will see the following output.

Welcome to TutorialsPoint
...
Updated on 22-Feb-2022 05:22:37