Created
September 9, 2016 20:52
-
-
Save arschles/53b3cccdc90675d43e97ed624c6f335f to your computer and use it in GitHub Desktop.
Sending a channel over a channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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