DEV Community

Hamamd Tahir
Hamamd Tahir

Posted on • Edited on

Real-Time Example: Using Goroutines and Channels

In Go, goroutines and channels play a key role in making concurrent programming smooth and efficient. Goroutines are like lightweight threads that let you perform tasks simultaneously, while channels help these goroutines communicate seamlessly. Together, they bring a powerful way to write concurrent programs in Go that feels intuitive and straightforward.

Imagine you're building a web scraper—a fantastic real-world application! Web scraping is all about gathering data from various websites, which can sometimes take a lot of time. But with goroutines, you can simultaneously fetch data from multiple sites, making the whole process much faster. And with channels, you can easily collect and process all that data from the different websites you’re scraping.

Let’s dive into a simple example of a web scraper in Go that utilizes goroutines and channels to make this task efficient and fun! channels:

package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func fetch(url string, ch chan<- string) {
    resp, err := http.Get(url)
    if err != nil {
        fmt.Println("Error fetching URL:", err)
        ch <- ""
        return
    }
    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("Error reading response body:", err)
        ch <- ""
        return
    }

    ch <- string(body)
}

func main() {
    urls := []string{"https://example.com", "https://google.com", "https://github.com"}
    ch := make(chan string)

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

    for range urls {
        fmt.Println(<-ch)
    }
}

Enter fullscreen mode Exit fullscreen mode

In this example, the fetch function retrieves the content of a URL using an HTTP GET request and shares it with a channel. We set up a goroutine for each URL in the urls slice, allowing the fetching of each URL to happen at the same time. Finally, we read from the channel to see the fetched content and display it.

Top comments (0)