Golang Async: 10 Examples to Get Started

1 Min Read

Asynchronous programming is a critical aspect of modern software development, enabling developers to perform multiple tasks concurrently and improve the overall performance of their applications. In this article, we’ll explore Golang async programming and provide 10 examples to help you master the art of concurrent execution in Go.

1. Introduction to Goroutines

Goroutines are lightweight threads managed by the Go runtime. They are the foundation of asynchronous programming in Go. To run a function concurrently, simply prepend the go keyword before the function call. Here’s a basic example:


package main

import (
	"fmt"
	"time"
)

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

func main() {
	go printNumbers()

	time.Sleep(6 * time.Second)
}

In this example, the printNumbers function is executed concurrently as a goroutine, without blocking the main function.

2. Using Channels for Communication

Channels are the primary means of communication between goroutines. They provide a way to send and receive data between concurrently executing functions. Here’s an example of using channels:


package main

import (
	"fmt"
)

func printNumbers(ch chan int) {
	for i := 1; i <= 5;
i++ {
ch <- i
}
close(ch)
}

func main() {
ch := make(chan int)
go printNumbers(ch)

for num := range ch {
	fmt.Println(num)
}
}

In this example, the printNumbers function sends integers to the channel ch. The main function reads from the channel and prints the numbers.

3. Select Statement for Channel Operations

The select statement in Go allows you to work with multiple channels concurrently. It enables you to wait for any channel operation (send or receive) to complete. Here’s an example:


package main

import (
	"fmt"
	"time"
)

func server1(ch chan string) {
	time.Sleep(2 * time.Second)
	ch <- "Server 1 response"
}

func server2(ch chan string) {
	time.Sleep(1 * time.Second)
	ch <- "Server 2 response"
}

func main() {
	output1 := make(chan string)
	output2 := make(chan string)

	go server1(output1)
	go server2(output2)

	select {
	case res := <-output1:
		fmt.Println(res)
	case res := <-output2:
		fmt.Println(res)
	}
}

In this example, we simulate two server responses with different wait times. The select statement in the main function waits for the first channel operation to complete and prints the response.

4. Buffered Channels

Buffered channels can hold multiple values before blocking. Here’s an example:


package main

import (
	"fmt"
)

func main() {
ch := make(chan int, 2)

ch <- 1
ch <- 2

fmt.Println(<-ch)
fmt.Println(<-ch)
}

In this example, we create a buffered channel with a capacity of 2. We can send two integers to the channel without blocking. Then, we read and print the integers from the channel.

5. Using WaitGroups for Synchronization

sync.WaitGroup is a useful tool for synchronizing multiple goroutines. It allows you to wait for a group of goroutines to finish before proceeding. Here’s an example:


package main

import (
	"fmt"
	"sync"
)

func worker(id int, wg *sync.WaitGroup) {
	defer wg.Done()
	fmt.Printf("Worker %d started\n", id)
}

func main() {
	var wg sync.WaitGroup

	for i := 1; i <= 5; i++ {
		wg.Add(1)
		go worker(i, &wg)
	}

	wg.Wait()
	fmt.Println("All workers finished")
}

In this example, we create 5 worker goroutines. Each worker calls wg.Done() when finished. The main function uses wg.Wait() to wait for all workers to complete before proceeding.

6. Mutex for Managing Concurrent Access to Shared Data

sync.Mutex can be used to manage concurrent access to shared data. Here’s an example:


package main

import (
"fmt"
"sync"
)

var (
counter int
mutex sync.Mutex
)

func increment(id int) {
for i := 0; i < 5; i++ {
mutex.Lock()
counter++
fmt.Printf("Worker %d incremented counter to %d\n", id, counter)
mutex.Unlock()
}
}

func main() {
var wg sync.WaitGroup
for i := 1; i <= 3; i++ {
	wg.Add(1)
	go func(id int) {
		defer wg.Done()
		increment(id)
	}(i)
}

wg.Wait()
fmt.Println("Final counter value:", counter)
}

In this example, we use a mutex to ensure that only one goroutine can access the shared counter variable at a time, preventing race conditions.

7. Context for Canceling Goroutines

The context package provides a way to cancel goroutines, which is helpful for managing resources and time-consuming tasks. Here’s an example:


package main

import (
	"context"
	"fmt"
	"time"
)

func longRunningTask(ctx context.Context, id int) {
	for {
		select {
		case <-ctx.Done():
			fmt.Printf("Task %d canceled\n", id)
			return
		default:
			time.Sleep(1 * time.Second)
			fmt.Printf("Task %d is running\n", id)
		}
	}
}

func main() {
	ctx, cancel := context.WithCancel(context.Background())

	go longRunningTask(ctx, 1)

	time.Sleep(5 * time.Second)
	cancel()
	time.Sleep(1 * time.Second)
}

In this example, we create a long-running task that is canceled after 5 seconds using the cancel() function.

8. Error Handling with Channels

Channels can also be used for error handling in asynchronous tasks. Here’s an example:


package main

import (
	"errors"
	"fmt"
)

func divide(a, b float64, ch chan float64, errCh chan error) {
	if b == 0 {
		errCh <- errors.New("division by zero")
		return
	}

	ch <- a / b
}

func main() {
	resultCh := make(chan float64)
	errorCh := make(chan error)

	go divide(4, 2, resultCh, errorCh)

	select {
	case res := <-resultCh:
		fmt.Println("Result:", res)
	case err := <-errorCh:
		fmt.Println("Error:", err)
	}
}

In this example, the divide function sends the result or an error to separate channels. The main function uses a select statement to handle the result or the error.

9. Using Atomic Package for Concurrent Access to Shared Data

The sync/atomic package provides atomic operations for concurrent access to shared data. Here’s an example:


package main

import (
	"fmt"
	"sync"
	"sync/atomic"
)

var counter int64

func increment(id int) {
	for i := 0; i < 5; i++ {
		atomic.AddInt64(&counter, 1)
		fmt.Printf("Worker %d incremented counter to %d\n", id, counter)
	}
}

func main() {
	var wg sync.WaitGroup

	for i := 1; i <= 3; i++ {
		wg.Add(1)
		go func(id int) {
			defer wg.Done()
			increment(id)
		}(i)
	}

	wg.Wait()
fmt.Println("Final counter value:", counter)
}

In this example, we use the sync/atomic package’s AddInt64 function to atomically increment the shared counter variable. This allows concurrent access to the variable without the need for a mutex.

10. Timeouts with Context

The context package can also be used to set timeouts for goroutines. Here’s an example:


package main

import (
	"context"
	"fmt"
	"time"
)

func longRunningTask(ctx context.Context, id int) {
	for {
		select {
		case <-ctx.Done():
			fmt.Printf("Task %d timed out\n", id)
			return
		default:
			time.Sleep(1 * time.Second)
			fmt.Printf("Task %d is running\n", id)
		}
	}
}

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	go longRunningTask(ctx, 1)

	time.Sleep(6 * time.Second)
}

In this example, we create a long-running task with a timeout of 5 seconds. The task is automatically canceled if it takes longer than the specified timeout.

These ten examples should provide you with a solid foundation for working with Golang async and concurrent programming. By leveraging goroutines, channels, and synchronization primitives, you can build efficient, high-performance applications in Go.

FAQ

What is Golang async?

Golang async refers to the asynchronous programming approach used in the Go language, which allows for concurrent execution of multiple tasks. Goroutines, channels, and synchronization primitives are some of the key features that enable async programming in Go.

What is a goroutine in Golang?

A goroutine is a lightweight, concurrent function execution in Go. Goroutines are managed by the Go runtime and run on a single operating system thread, allowing for efficient concurrent processing of multiple tasks within a Go application.

What are channels in Golang?

Channels in Golang are used for communication between goroutines, providing a way to send and receive values between concurrently executing tasks. Channels help to synchronize the execution of goroutines and prevent race conditions, allowing for safe and efficient concurrent programming.

What is a wait group in Golang?

A wait group in Golang is a synchronization primitive from the `sync` package that allows you to wait for a collection of goroutines to finish executing. Wait groups are useful for coordinating the completion of multiple tasks in a concurrent program.

What is a mutex in Golang?

A mutex (short for “mutual exclusion”) in Golang is a synchronization primitive from the `sync` package that ensures exclusive access to shared resources by multiple goroutines. Mutexes help prevent race conditions by locking and unlocking access to shared variables or resources.

How do you create a goroutine in Golang?

To create a goroutine in Golang, you simply use the `go` keyword followed by the function you want to execute concurrently. For example:


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

In this example, printNumbers() and printLetters() will be executed concurrently as goroutines.

How do you handle errors in asynchronous Golang code?

In Golang, handling errors in asynchronous code typically involves using channels to pass error values between goroutines. Here’s an example of how to handle errors in a goroutine:


func asyncFunction() (int, error) {
    // Perform some operation and return the result and/or an error
}

func main() {
    resultChan := make(chan int)
    errChan := make(chan error)

    go func() {
        result, err := asyncFunction()
        if err != nil {
            errChan <- err
            return
        }
        resultChan <- result
    }()

    select {
    case result := <-resultChan:
        fmt.Println("Result:", result)
    case err := <-errChan:
        fmt.Println("Error:", err)
    }
}

In this example, the asyncFunction() returns a result and an error. The anonymous goroutine sends the result or error to their respective channels, and the select statement waits for either a result or an error to be received.

How do you cancel a running goroutine?

In Golang, you can use a `context.Context` to cancel a running goroutine. The `context` package provides functions to create and manage context objects, which can carry cancellation signals, deadlines, and other request-scoped values. Here’s an example of how to cancel a running goroutine using a context:


func main() {
    ctx, cancel := context.WithCancel(context.Background())
    go func() {
        for {
            select {
            case <-ctx.Done():
                fmt.Println("Goroutine canceled")
                return
            default:
                // Perform some operation
            }
        }
    }()

    // Simulate some work
    time.Sleep(2 * time.Second)

    // Cancel the goroutine
    cancel()
}

In this example, the context.WithCancel() function creates a new context with a cancellation function. The cancel() function can be called to signal the cancellation of the goroutine. The goroutine checks for the cancellation signal using the ctx.Done() channel within the select statement and exits when the signal is received.

How do you synchronize multiple goroutines?

To synchronize multiple goroutines in Golang, you can use channels or synchronization primitives from the `sync` package, such as `WaitGroup` and `Mutex`. Here’s an example of synchronizing multiple goroutines using a `sync.WaitGroup`:


func worker(wg *sync.WaitGroup) {
    // Perform some operation

    // Signal that the worker is done
    wg.Done()
}

func main() {
    var wg sync.WaitGroup

    // Start multiple goroutines
    for i := 0; i < 5; i++ {
        wg.Add(1)
        go worker(&wg)
    }

    // Wait for all goroutines to finish
    wg.Wait()
}

In this example, a sync.WaitGroup is used to wait for multiple goroutines to finish executing. The wg.Add(1) function increments the WaitGroup counter, and wg.Done() decrements it. The wg.Wait() function blocks until the counter reaches zero, indicating that all goroutines have finished executing.

How do you limit the number of concurrent goroutines?

To limit the number of concurrent goroutines in Golang, you can use a buffered channel as a semaphore. Here’s an example of limiting the number of concurrent goroutines:


func worker(sem chan struct{}, wg *sync.WaitGroup) {
    // Acquire the semaphore
    sem <- struct{}{}

    // Perform some operation

    // Release the semaphore
    <-sem

    // Signal that the worker is done
    wg.Done()
}

func main() {
    var wg sync.WaitGroup
    sem := make(chan struct{}, 3) // Limit to 3 concurrent goroutines

    // Start multiple goroutines
    for i := 0; i < 10; i++ {
        wg.Add(1)
        go worker(sem, &wg)
    }

    // Wait for all goroutines to finish
    wg.Wait()
}

In this example, a buffered channel sem is used as a semaphore to limit the number of concurrent goroutines. The channel has a capacity of 3, allowing only 3 concurrent goroutines at a time. The worker() function acquires and releases the semaphore by sending and receiving from the channel.

What is the difference between synchronous and asynchronous programming in Golang?

Synchronous programming is a sequential execution model where operations are performed one after another. In Golang, synchronous programming is the default mode, and functions are executed in the order they are called.
Asynchronous programming, on the other hand, allows multiple operations to run concurrently, without waiting for one to finish before starting the next. In Golang, asynchronous programming can be achieved using goroutines, which are lightweight concurrent threads managed by the Go runtime.

The primary difference between synchronous and asynchronous programming in Golang lies in the way operations are executed. Synchronous programming blocks the execution until the current operation is completed, whereas asynchronous programming allows multiple operations to run concurrently, improving the overall performance and responsiveness of the program.

Share this Article
Leave a comment