Created
October 17, 2022 02:04
-
-
Save jdavidteki/409b88c031dfacd28e29bbce8d4f7569 to your computer and use it in GitHub Desktop.
go-callback-warmup
This file contains hidden or 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 ( | |
"io" | |
"log" | |
"net/http" | |
"strings" | |
"time" | |
) | |
// 1. 'httpGet' is provided for you. | |
// 2. You have to implement 'httpGetParallel' and 'httpGetSerial' | |
// using 'httpGet'. See signatures below. | |
// 3. 'testGetFunctions' will try running your 'httpGetParallel' and | |
// 'httpGetSerial' with some test URLs. The expected output | |
// is shown at the very bottom of this file. | |
// | |
func main() { | |
testGetFunctions() | |
} | |
func httpGet(url string) (string, error) { | |
log.Printf(`GET %s`, url) | |
resp, err := http.Get(url) | |
if err != nil { | |
return "", err | |
} | |
defer resp.Body.Close() | |
body, err := io.ReadAll(resp.Body) | |
if err != nil { | |
return "", err | |
} | |
return string(body), nil | |
} | |
// This function should initiate parallel HTTP GET requests for all URLs in | |
// the 'urls' array. After all requests are complete, | |
// the array of response bodies should match the order of the `urls` array. | |
func httpGetParallel(urls []string) ([]string, error) { | |
// You have to write this code. | |
// You can use 'httpGet', but don't use promises. | |
var reponses = []string{} | |
for _, url := range urls { | |
reponse, err := httpGet(url) | |
if err != nil { | |
return []string{}, err | |
} | |
reponses = append(reponses, reponse) | |
} | |
return reponses, nil | |
} | |
// This function should make HTTP GET requests for the URLs in the 'urls' | |
// array one at a time. After all requests are complete, the array of | |
// response bodies should match the order of the `urls` array. | |
func httpGetSerial(urls []string) (string, error) { | |
// You have to write this code. | |
if len(urls) == 0 { | |
return "", nil | |
} | |
httpGetResponse, err := httpGet(urls[0]) | |
if err != nil { | |
return "", err | |
} | |
logMessage(httpGetResponse) | |
_, err = httpGetSerial(urls[1:]) | |
if err != nil { | |
return "", err | |
} | |
return httpGetResponse, nil | |
} | |
func logMessage(message string) { | |
currentTime := time.Now().String()[:16] | |
log.Printf("[%s] %s", currentTime, message) | |
} | |
func testGetFunctions() { | |
// httpbin.org provides several convenient test endpoints. We're using | |
// httpbin.org/delay/N, which just waits N seconds before responding. | |
var urls = []string{ | |
"https://httpbin.org/delay/1", | |
"https://httpbin.org/delay/2", | |
"https://httpbin.org/delay/1", | |
} | |
logMessage("Trying httpGetParallel...") | |
parallelReponse, err := httpGetParallel(urls) | |
if err != nil { | |
logMessage(err.Error()) | |
} | |
logMessage(strings.Join(parallelReponse, ",")) | |
logMessage("Trying httpGetSerial...") | |
serialReponse, err := httpGetSerial(urls) | |
if err != nil { | |
logMessage(err.Error()) | |
} | |
logMessage(serialReponse) | |
} | |
/* | |
Expected output: | |
[20:07:12.4] Trying httpGetParallel... | |
[20:07:12.4] GET "https://httpbin.org/delay/1"... | |
[20:07:12.4] GET "https://httpbin.org/delay/2"... | |
[20:07:12.4] GET "https://httpbin.org/delay/1"... | |
[20:07:14.8] Got responses: [ | |
"{\n \"args\": {}, \n \"", | |
"{\n \"args\": {}, \n \"", | |
"{\n \"args\": {}, \n \"" | |
] | |
[20:07:14.8] Trying httpGetSerial... | |
[20:07:14.8] GET "https://httpbin.org/delay/1"... | |
[20:07:15.9] GET "https://httpbin.org/delay/2"... | |
[20:07:18.0] GET "https://httpbin.org/delay/1"... | |
[20:07:19.1] Got responses: [ | |
"{\n \"args\": {}, \n \"", | |
"{\n \"args\": {}, \n \"", | |
"{\n \"args\": {}, \n \"" | |
] | |
(Your timestamps might be different.) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment