/MutliNWriter

This package provides an alternative to io.MultiWriter that enables dynamic addition and removal of io.Writers at run time.

Primary LanguageGo

MutliNWriter

This package provides an alternative to io.MultiWriter that enables dynamic addition and removal of io.Writers at run time.

Reasoning

io.Multiwriter is a great tool for when you have a fixed at compile time set of io.Writers you want to duplicate writes to such as writing exec.Cmd output to a file and stdout at the same time. What it can't do is allow for a dynamic set of Writers to be added during runtime.

This is where MultiNWriter comes in. It uses a mutex and a generic keyed map of io.Writers to enable N number of copied writes through a simple interface.

Getting started

multiWriter := MutliNWriter.NewMultiNWriter()
multiWriter.AddWriter("key", writer)
multiWriter.AddWriter("key2", writer2)

That's all you need to get up and running with dynamic duplicate writers at runtime!

Example

An example can be found here which shows a simulated long running task which can be monitored from N number of websockets in real time.