Published on

GoLang Concurrency

Authors
  • avatar
    Name
    Avinash
    Twitter

Building Efficient Programs with concurrency

Welcome back to our GoLang series! In this blog post, we'll explore the powerful concurrency features of GoLang that enable you to build efficient and scalable programs. Let's get started!

Goroutines

GoLang introduces the concept of Goroutines, which are lightweight threads of execution. Goroutines enable concurrent programming by allowing functions to be executed concurrently. You can think of Goroutines as functions running independently, yet concurrently, with other parts of your program.

Here's an example of launching Goroutines:

package main

import (
    "fmt"
    "time"
)

func printNumbers() {
    for i := 1; i <= 5; i++ {
        fmt.Println(i)
        time.Sleep(1 * time.Second)
    }
}

func printLetters() {
    for i := 'a'; i <= 'e'; i++ {
        fmt.Printf("%c ", i)
        time.Sleep(1 * time.Second)
    }
}

func main() {
    go printNumbers()
    go printLetters()

    // Wait for Goroutines to complete
    time.Sleep(6 * time.Second)
}

In this example, we have two functions, printNumbers and printLetters, which are executed concurrently using Goroutines. The go keyword is used to launch Goroutines. By running the program, you'll see the numbers and letters printed concurrently.

Channels

GoLang provides channels as a means of communication between Goroutines. Channels allow Goroutines to send and receive values synchronously. They ensure safe and synchronized communication, preventing data races and enabling effective coordination between Goroutines.

Here's an example of using channels:

package main

import (
    "fmt"
    "time"
)

func writeToChannel(c chan<- int) {
    for i := 1; i <= 5; i++ {
        c <- i
        time.Sleep(1 * time.Second)
    }
    close(c)
}

func readFromChannel(c <-chan int) {
    for num := range c {
        fmt.Println(num)
    }
}

func main() {
    c := make(chan int)

    go writeToChannel(c)
    readFromChannel(c)
}

In this example, we have two Goroutines: writeToChannel and readFromChannel. The writeToChannel Goroutine writes numbers to the channel, while the readFromChannel Goroutine reads and prints those numbers. By using channels, the Goroutines are synchronized, and the program will output the numbers in the correct order.

Select Statement

The select statement in GoLang allows you to work with multiple channels concurrently. It enables you to receive or send data on multiple channels, whichever is ready first. The select statement is useful for handling multiple Goroutines and coordinating their communication.

Here's an example of using the select statement:

package main

import (
    "fmt"
    "time"
)

func sendPing(c chan<- string) {
    for {
        time.Sleep(1 * time.Second)
        c <- "ping"
    }
}

func sendPong(c chan<- string) {
    for {
        time.Sleep(2 * time.Second)
        c <- "pong"
    }
}

func main() {
    pingChannel := make(chan string)
    pongChannel := make(chan string)

    go sendPing(pingChannel)
    go sendPong(pongChannel)

    for {
        select {
        case msg := <-pingChannel:
            fmt.Println("Received message:", msg)
        case msg := <-pongChannel:
            fmt.Println("Received message:", msg)
        }
    }
}

In this example, we have two Goroutines: sendPing and sendPong. Each Goroutine sends a message to its respective channel at different intervals. The main Goroutine uses the select statement to receive messages from both channels and prints them as they arrive.

Conclusion

Congratulations on learning about GoLang's powerful concurrency features! In this blog post, we explored Goroutines, channels, and the select statement. By utilizing these features, you can build efficient and concurrent programs in GoLang.


In the next blog post, we'll delve into package management in GoLang and explore how to manage dependencies and create reusable packages. Stay tuned!

Thank you for reading our GoLang Concurrency: Building Efficient Programs blog post. We hope you found it informative and inspiring to utilize GoLang's concurrency features in your programs. If you have any questions or suggestions, feel free to leave a comment below. Happy coding!