Created
February 28, 2016 13:15
-
-
Save gerep/fcb579d42a3e81bcbccf to your computer and use it in GitHub Desktop.
Using a channel to get a response from a goroutine
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 ( | |
"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