Skip to content
/ fanout Public

Fan Out design pattern (aka broadcasting channel using generics)

License

Notifications You must be signed in to change notification settings

badu/fanout

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fan Out

Fan Out design pattern, using channels with generics.

What problem does it solve?

Fan Out pattern is a software architecture pattern that involves distributing data (messages / payloads) from a single sender (publisher) to multiple receivers (listeners).

Each recipient receives a copy of the message - if your payload is a pointer, that would violate the pattern.

The Fan Out pattern is useful in situations where multiple components of a system need to react to the same event or receive the same data.

This is a naive implementation of the Fan Out pattern, using channels and generics.

In its shortest form, it looks like this:

package mypack

func FanOut[T any](from <-chan T, to ...chan<- T) {
	for v := range from {
		for _, ch := range to {
			ch <- v
		}
	}
}

Usage

go get github.com/badu/fanout

T. B. D.

About

Fan Out design pattern (aka broadcasting channel using generics)

Topics

Resources

License

Stars

Watchers

Forks

Languages