Skip to content

Instantly share code, notes, and snippets.

@inhzus
Created July 11, 2019 05:51
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 inhzus/d7ca93a0f9d605d41e75008621728814 to your computer and use it in GitHub Desktop.
Save inhzus/d7ca93a0f9d605d41e75008621728814 to your computer and use it in GitHub Desktop.
Passing channel over channel to do request & get response
package main
import "fmt"
var queue = make(chan int)
var c = make(chan chan error)
func request() {
for i := 0; i < 4; i++ {
queue <- i
}
rsp := make(chan error)
c <- rsp
fmt.Printf("request: %v", <-rsp)
}
func response() {
for {
select {
case val := <-queue:
fmt.Printf("response: %v\n", val)
case rsp := <-c:
rsp <- fmt.Errorf("stop signal recieved")
return
}
}
}
func main() {
go response()
request()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment