DEV Community

Romulo Gatto
Romulo Gatto

Posted on

Concurrency in Go: Goroutines and Channels

Concurrency in Go: Goroutines and Channels

Go (Golang) is a powerful programming language that offers built-in support for concurrent programming. Its concurrency model relies on goroutines and channels, making it easy to write efficient and scalable concurrent code.

In this guide, we will explore how goroutines and channels work in Go, and how you can leverage them to build your own concurrent applications.

What are Goroutines?

In Go, a goroutine is a lightweight thread of execution that allows us to execute functions concurrently. Unlike traditional threads or processes, creating a goroutine is cheap, with minimal memory overhead. This makes it possible to have thousands of concurrently executing goroutines within one application without sacrificing performance.

To create a new goroutine, simply prefix the function call with the go keyword:

func main() {
    go myFunction()
}
Enter fullscreen mode Exit fullscreen mode

By utilizing go, our myFunction() runs in its own separate flow of control alongside the main program's flow without blocking other operations from happening simultaneously.

Synchronization with Channels

Channels provide a way for communication and synchronization between different goroutines. A channel represents a conduit through which values can be sent between multiple goroutines – essentially acting as communication pipes.

Creating a channel is straightforward:

ch := make(chan int)
Enter fullscreen mode Exit fullscreen mode

This creates an unbuffered channel capable of transporting integers. Values can be sent into the channel using the <- operator:

ch <- 42
Enter fullscreen mode Exit fullscreen mode

Similarly, values can be received from the channel using <- operator as well:

result := <- ch
Enter fullscreen mode Exit fullscreen mode

By default, sending or receiving from/to channels blocks until there is both sender and receiver ready on either end. This behavior helps synchronize different parts of your program that need coordination during execution.

Applying Concurrency: An Example

Let's dive into an example where we utilize goroutines and channels to build a simple concurrent program. Consider the following scenario where we want to concurrently fetch data from multiple APIs:

package main

import (
    "fmt"
    "net/http"
)

func fetchData(url string, ch chan<- string) {
    resp, err := http.Get(url)
    if err != nil {
        ch <- fmt.Sprint(err)
        return
    }

    defer resp.Body.Close()

    // Process response

    ch <- "Data Fetched Successfully!"
}

func main() {
    urls := []string{
        "https://api1.example.com",
        "https://api2.example.com",
        // Add more URLs
    }

    ch := make(chan string)

    for _, url := range urls {
        go fetchData(url, ch)
    }

    for range urls {
        fmt.Println(<-ch)
    }
}
Enter fullscreen mode Exit fullscreen mode

In the example above, we define a fetchData function that makes an HTTP request to a given URL and communicates the status of the operation through a channel. By launching multiple goroutines with different URLs, we can process their responses independently.

Finally, by iterating through all the URLs in our main function and receiving messages from the channel using <-, we ensure that all responses are received before exiting.

Conclusion

Goroutines and channels are essential features of Go's concurrency model. They provide an elegant way to write concurrent programs that scale well across multiple cores or machines. Remember to keep in mind Go's motto: "Do not communicate by sharing memory; instead, share memory by communicating." Utilize goroutines and channels effectively in your programs for efficient parallel execution.

Top comments (0)