Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sending a channel over a channel
package main
import (
"log"
"time"
)
type acker struct {
ch chan time.Duration
}
func doStuff(t time.Duration, ch <-chan acker) {
ac := <-ch
time.Sleep(t)
ac.ch <- t
}
func main() {
ch := make(chan acker)
for i := 0; i < 10; i++ {
go doStuff(time.Duration(i+1)*time.Second, ch)
}
thinger := acker{ch: make(chan time.Duration)}
for i := 0; i < 10; i++ {
ch <- thinger
}
for i := 0; i < 10; i++ {
dur := <-thinger.ch
log.Printf("thinger slept for %s", dur)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.