Last active
November 6, 2018 01:51
-
-
Save keith6014/6a0055125894ac7629a545014f22f765 to your computer and use it in GitHub Desktop.
goroutines
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
// http_goroutine project main.go | |
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"time" | |
) | |
func get(url string, c chan Result) { | |
res, err := http.Get(url) | |
st := time.Now().Format(time.RFC850) | |
if err != nil { | |
fmt.Println(err) | |
} | |
val, err := ioutil.ReadAll(res.Body) | |
res.Body.Close() | |
if err != nil { | |
fmt.Println(err) | |
} | |
c <- Result{Body: string(val), StartTime: st} | |
} | |
type Result struct { | |
Body string | |
StartTime string | |
} | |
func main() { | |
var endpoints []string = []string{"/sleep", "/sleep", "/sleep", "/sleep", "/sleep"} | |
var url string = "http://localhost:8080" | |
c := make(chan Result, 1) | |
for _, endpoint := range endpoints { | |
go get(url+endpoint, c) | |
} | |
var res []Result | |
for _, i := range endpoints { | |
x := <-c | |
res = append(res, x) | |
fmt.Println(i) | |
} | |
fmt.Println(res) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The python server looks like this