Go Fan-Out/Fan-In Pattern 🎯

beginner
22 min

Go Fan-Out/Fan-In Pattern 🎯

Welcome to the world of Go (Golang)! Today, we're going to dive into the Fan-Out/Fan-In pattern, a powerful technique used in concurrent programming. This pattern is crucial for handling multiple tasks efficiently, which is essential in modern application development.

What is the Fan-Out/Fan-In Pattern? 📝

The Fan-Out/Fan-In pattern is a design technique used to manage concurrent tasks in a program. It consists of two main steps:

  1. Fan-Out: This step involves splitting a task into multiple smaller tasks (or sub-tasks). Each sub-task is then executed concurrently.

  2. Fan-In: Once all the sub-tasks are completed, the results are collected and combined to produce the final output.

In other words, the Fan-Out/Fan-In pattern is like delegating a complex task to multiple assistants (sub-tasks) and then collecting their results to complete the task.

Why use the Fan-Out/Fan-In Pattern? 💡

By using the Fan-Out/Fan-In pattern, we can take advantage of multiple CPU cores to execute tasks concurrently, reducing the overall execution time. This is particularly useful when dealing with I/O-bound or CPU-bound tasks.

Practical Example 🎯

Let's consider a real-world example: a web scraper that fetches data from multiple websites simultaneously.

go
package main import ( "fmt" "net/http" "sync" ) func fetchData(url string, wg *sync.WaitGroup, results chan<- string) { defer wg.Done() resp, err := http.Get(url) if err != nil { fmt.Println(err) return } // Assuming the website returns the data in its body results <- resp.Body.String() } func main() { urls := []string{ "https://example.com/data1", "https://example.com/data2", "https://example.com/data3", // Add more URLs as needed } var wg sync.WaitGroup results := make(chan string) for _, url := range urls { wg.Add(1) go fetchData(url, &wg, results) } go func() { wg.Wait() close(results) }() for result := range results { fmt.Println(result) } }

In this example, we're fetching data from multiple URLs concurrently using the fetchData function. The main function initializes the wait group, channels, and URLs. It then loops through the URLs, creating a goroutine for each one to fetch data. Once all the goroutines are done, the main function waits for them to finish using the wait group and then closes the results channel. The main function then reads data from the results channel until it's closed.

Quiz 📝

Quick Quiz
Question 1 of 1

In the Fan-Out/Fan-In pattern, what does Fan-Out represent?

Quick Quiz
Question 1 of 1

In the Go example provided, what is the purpose of the wait group?