Skip to content

Go Channel Tee Pattern — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Go Channel Tee Pattern. We cover key concepts, practical examples, and best practices.

Channel tee pattern -- Split a single channel into two identical output channels, each receiving every value.

The Problem

Tee pattern duplicates each value from a source channel to two output channels. Each output receives every value sent to the source.

Wrong

source := make(chan int)
// Want to send each value to both out1 and out2

Output:

// Need tee pattern for duplication
func tee(ctx context.Context, in <-chan int) (<-chan int, <-chan int) {
    out1 := make(chan int)
    out2 := make(chan int)
    go func() {
        defer close(out1)
        defer close(out2)
        for val := range in {
            out1 := out1
            out2 := out2
            for i := 0; i < 2; i++ {
                select {
                case <-ctx.Done():
                    return
                case out1 <- val:
                    out1 = nil // Disable this case
                case out2 <- val:
                    out2 = nil
                }
            }
        }
    }()
    return out1, out2
}

Output:

// Each output receives all values from source

Prevention

  • Tee creates N copies of each value
  • Use nil channel trick to alternate sends
  • Needs buffering or goroutines to avoid deadlock
  • Each output gets every value (not split)
  • Tee is different from fan-out (which splits)

Common Mistakes with channel tee

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world GO code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

**What is the difference between tee and fan-out?**

Tee sends each value to ALL outputs. Fan-out sends each value to ONE worker.

Can I tee to more than 2 outputs?

Yes. Loop over outputs array.

Does tee preserve order?

Each output receives values in source order.


Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. DodaTech tutorials help Go developers build production-ready software used by millions.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro