Skip to content

Boilerplate code and helper functions for commonly used Go patterns.

Notifications You must be signed in to change notification settings

hankcheah/go-snippets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-snippets

This repository contains boilerplate code and helper functions for some of the more commonly repeated Go patterns.

channel package

func Clone[T any](inCh chan T, size int) <-chan <-chan T

Clone is a generic function that duplicates a channel. The number of duplicates it creates depends on the size parameter. The type and the buffer size of the cloned channels are the same as the source channel.

[Example]

Say this is our current pipeline. intStream is consumed by a function call consumeOne. This is working fine, but what if we have another function consumeTwo that also needs to access the entirety of intStream?

intStream := make(chan int, 10)
consumeOne(intStream)	// This is fine
consumeTwo(intStream)   // This is not

To solve this, we can create two clones of the channel, one for each of the consumers of the input channel.

intStream := make(chan int, 10)
intStreamChs := Clone(intStream, 2)
consumeOne(<-intStreamChs)
consumeTwo(<-intStreamChs)

About

Boilerplate code and helper functions for commonly used Go patterns.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages