Skip to content

Instantly share code, notes, and snippets.

@gerep
Created February 28, 2016 13:15
Show Gist options
  • Save gerep/fcb579d42a3e81bcbccf to your computer and use it in GitHub Desktop.
Save gerep/fcb579d42a3e81bcbccf to your computer and use it in GitHub Desktop.
Using a channel to get a response from a goroutine
package main
import (
"fmt"
"net/http"
)
type work struct {
url string
resp chan *http.Response
}
func getter(w chan work) {
for {
do := <-w
resp, _ := http.Get(do.url)
do.resp <- resp
}
}
func main() {
w := make(chan work)
go getter(w)
resp := make(chan *http.Response)
w <- work{"http://cdnjs.cloudflare.com/jquery/1.9.1/jquery.min.js",
resp}
x := <-resp
fmt.Println(x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment