Amin Shojaei
Amin Shojaei

Reputation: 6548

How to observe (monitor) a channel

Is it possible to get copy of channel messages? (instead of receive and delete message from channel)

The idea is to log a channel's messages.

Upvotes: 1

Views: 619

Answers (2)

Jonathan Hall
Jonathan Hall

Reputation: 79744

No, this is not possible, but you can use two channels, and pass between them observing the data in transit:

func observe(ch interface{}) ch interface{} {
    newCh := make(chan interface{})
    go func() {
        for item := range ch {
            fmt.Println(item)
            newCh <- item
        }
        close(newCh)
    }()
    return newCh
}

Upvotes: 3

Volker
Volker

Reputation: 42468

Is it possible to get copy of channel messages?

No.

Upvotes: 3

Related Questions