What is the actual purpose of fan out pattern

if i am not mistaken fan out pattern is the pattern when several go routins are reading from the same channel, so i guess that the purpose of that is to recive values from channel much faster but i am not sure, also can u tell me about fun in and its purpose and use cases

First of all, I hope someone else will correct me here if I’m wrong as I’ve not delved that deep into channels or patterns associated with them yet.

From what I understand one potential purpose is to be able to set up a broadcaster system of sorts - ie if you want to broadcast a message to multiple listeners vs just one. There can be multiple use cases for this, distributing work being one.

For example, in my case of a simulation experiment I want multiple “Aggregate” instances to receive a message when an “Agent” contained within them dies (and then do some things, like stop listening for further messages from that dead agent, check if the aggregate also needs to be destroyed, etc etc).

Another use case may be a chat system, where a message can be broadcast to multiple clients.

Hi

Yes fan out is a pattern to send a message to several clients. If you want to model this in go you have to use one channel for each client otherwise will the first “free” one read the message and it is removed from the channel. Like this:

package main

import (
	"fmt"
	"time"
)

func client(name string, c <-chan string) {
	for {
		message := <-c
		fmt.Println(name, "got", message)
	}
}

func main() {
	n := 10
	c := make(chan string, 1)

	for i := 1; i <= n; i++ {
		name := fmt.Sprintf("Client %d", i)
		go client(name, c)
	}

	c <- "Message 1"
	c <- "Message 2"

	for {
		// Just sleep otherwise will program end
		// before go-routines manage to run
		time.Sleep(time.Second)
	}
}

Result:

$ go run main2.go
Client 5 got Message 2
Client 2 got Message 1

but keeping all the channels in a slice and ranging over them make all clients get the message

package main

import (
	"fmt"
	"time"
)

func client(name string, c <-chan string) {
	for {
		message := <-c
		fmt.Println(name, "got", message)
	}
}

func main() {
	n := 10
	clientChannels := []chan string{}
	for i := 1; i <= n; i++ {
		c := make(chan string, 1)
		clientChannels = append(clientChannels, c)
		name := fmt.Sprintf("Client %d", i)
		go client(name, c)
	}
	for _, c := range clientChannels {
		c <- "Message 1"
		c <- "Message 2"
	}

	for {
		// Just sleep otherwise will program end
		// before go-routines manage to run
		time.Sleep(time.Second)
	}
}

Result:

 go run main.go
Client 3 got Message 1
Client 3 got Message 2
Client 4 got Message 1
Client 4 got Message 2
Client 7 got Message 1
Client 7 got Message 2
Client 5 got Message 1
Client 5 got Message 2
Client 6 got Message 1
Client 6 got Message 2
Client 2 got Message 1
Client 2 got Message 2
Client 9 got Message 1
Client 9 got Message 2
Client 10 got Message 1
Client 10 got Message 2
Client 1 got Message 1
Client 8 got Message 1
Client 8 got Message 2
Client 1 got Message 2

its purpose

To distribute load between CPU cores.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.