Golang Goroutine: Concurrency in Go Read it later

5/5 - (1 vote)

Goroutines are one of the most useful features of Go programming language. They are lightweight threads of execution that enable us to execute multiple functions concurrently. With Golang Goroutine, developers can create highly concurrent, scalable, and efficient programs. In this blog, we’ll dive deep into the Goroutine syntax, types, advantages, and different ways of creating Goroutines.

What is a Goroutine in Go?

Goroutine is a lightweight thread of execution that allows concurrent execution of functions. Goroutines are managed by the Go runtime, which schedules them on different processor threads. Goroutines are incredibly efficient and can be created and destroyed quickly. They also consume very little memory, making them ideal for creating highly concurrent applications.

Golang Goroutine Syntax

Creating a Goroutine is very simple in Go. All you need to do is add the keyword ‘go’ in front of a function call. Here’s the syntax:

go function_name(arguments)

Here, function_name is the name of the function you want to execute as a Goroutine. arguments are the input arguments for the function.

Let’s look at an example:

func main() {
    go printMessage("Hello, World!")
    fmt.Println("Main function")
}

func printMessage(message string) {
    fmt.Println(message)
}

In the above example, we’ve created a Goroutine to execute the printMessage function. The printMessage function prints a message to the console. We’ve passed the message “Hello, World!” as an argument to the function. When we execute the program, we’ll see the following output:

Main function
Hello, World!

Notice that the printMessage function is executed concurrently with the main function.

Types of Goroutines in Go

There are two types of Goroutines in Go: Anonymous Goroutines and Named Goroutines.

  1. Anonymous Goroutines

An Anonymous Goroutine is created by using a function literal. A function literal is an anonymous function that is not declared with a name. Here’s an example:

func main() {
    go func(message string) {
        fmt.Println(message)
    }("Hello, World!")
    fmt.Println("Main function")
}

In the above example, we’ve created an Anonymous Goroutine to execute a function that prints a message to the console. We’ve passed the message “Hello, World!” as an argument to the function.

  1. Named Goroutines

A Named Goroutine is created by defining a function and giving it a name. Here’s an example:

func main() {
    go printMessage("Hello, World!")
    fmt.Println("Main function")
}

func printMessage(message string) {
    fmt.Println(message)
}

In the above example, we’ve created a Named Goroutine to execute the printMessage function. The printMessage function prints a message to the console. We’ve passed the message “Hello, World!” as an argument to the function.

Advantages of Golang Goroutine

  1. Lightweight: Goroutines are lightweight threads of execution that require very little memory.
  2. Efficient: Goroutines are incredibly efficient, and they can be created and destroyed quickly.
  3. Concurrent: Goroutines enable concurrent execution of functions, making it easy to create highly concurrent applications.
  4. Scalable: Goroutines can be used to create highly scalable applications that can handle a large number of concurrent requests.
  5. Simplified code: Goroutines simplify code by eliminating the need for complex synchronization mechanisms such as locks and semaphores.

Different Ways of Creating Golang Goroutine

  1. Using the ‘go’ keyword

The most common way of creating a Goroutine is by using the ‘go’ keyword, as we saw in the syntax section. This method is straightforward and easy to implement.

  1. Using function literals

We can also create Goroutines using function literals, which are anonymous functions that can be executed concurrently. Here’s an example:

func main() {
    go func() {
        fmt.Println("Hello, World!")
    }()
    fmt.Println("Main function")
}

In the above example, we’ve created a Goroutine using a function literal that prints a message to the console.

  1. Using closures

Closures are functions that reference variables outside of their own scope. We can use closures to create Goroutines that share data. Here’s an example:

func main() {
    message := "Hello, World!"
    go func() {
        fmt.Println(message)
    }()
    fmt.Println("Main function")
}

In the above example, we’ve created a Goroutine using a closure that shares the message variable defined in the main function.

  1. Using methods

We can also create Goroutines using methods defined on a struct. Here’s an example:

type MyStruct struct {
    message string
}

func (s *MyStruct) printMessage() {
    fmt.Println(s.message)
}

func main() {
    myStruct := MyStruct{message: "Hello, World!"}
    go myStruct.printMessage()
    fmt.Println("Main function")
}

In the above example, we’ve defined a method called printMessage on a struct called MyStruct. We’ve created an instance of the struct and passed it to a Goroutine to execute the printMessage method.

Was This Article Helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *