Skip to content

Instantly share code, notes, and snippets.

@arschles
Created September 9, 2016 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arschles/53b3cccdc90675d43e97ed624c6f335f to your computer and use it in GitHub Desktop.
Save arschles/53b3cccdc90675d43e97ed624c6f335f to your computer and use it in GitHub Desktop.
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