-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyncfetch.go
40 lines (35 loc) · 868 Bytes
/
syncfetch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//Execution Type: synchromous / linear / blocking
//Goal: Fetch JSON API response for every movie in titles var
package main
import (
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
)
var titles = []string{
"Star Wars", "The Matrix",
"Inception", "Hulk",
"The Departed", "Blade Runner",
"Alien", "Metropolis",
"Brazil", "Gattaca",
}
func fetcher(api_url string) string {
resp, err := http.Get(api_url)
if err != nil {
panic(err)
}
defer resp.Body.Close() //this'll be executed before func return
body, _ := ioutil.ReadAll(resp.Body)
return string(body)
}
func main() {
start := time.Now()
for _, v := range titles {
//log.Print(fetcher("http://www.omdbapi.com/?r=JSON&s=" + url.QueryEscape(v)))
fetcher("http://www.omdbapi.com/?r=JSON&s=" + url.QueryEscape(v))
}
elapsed := time.Since(start)
log.Print("Time elapsed: ", elapsed)
}