Skip to content

Instantly share code, notes, and snippets.

@thechelsuk
Last active June 9, 2022 08:08
Show Gist options
  • Select an option

  • Save thechelsuk/ff6b3fa419d525d61a6014258445c403 to your computer and use it in GitHub Desktop.

Select an option

Save thechelsuk/ff6b3fa419d525d61a6014258445c403 to your computer and use it in GitHub Desktop.
# read api end point
func doRequest(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
# parse api response as json
func parseResponse(body []byte) ([]byte, error) {
var response map[string]interface{}
if err := json.Unmarshal(body, &response); err != nil {
return nil, err
}
return json.Marshal(response)
}
# print json response to stdout for debugging
func printResponse(body []byte) {
fmt.Println(string(body))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment