Last active
January 11, 2021 22:13
-
-
Save jonathan-fielding/74e2949c3b90ef46a92c00317cec1267 to your computer and use it in GitHub Desktop.
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 ( | |
"encoding/json" | |
"fmt" | |
"net/http" | |
"time" | |
) | |
type CatFacts struct { | |
Status struct { | |
Verified interface{} `json:"verified"` | |
SentCount int `json:"sentCount"` | |
} `json:"status"` | |
Type string `json:"type"` | |
Deleted bool `json:"deleted"` | |
ID string `json:"_id"` | |
User string `json:"user"` | |
Text string `json:"text"` | |
CreatedAt time.Time `json:"createdAt"` | |
UpdatedAt time.Time `json:"updatedAt"` | |
V int `json:"__v"` | |
} | |
func response(w http.ResponseWriter, req *http.Request) { | |
resp, err := http.Get("https://cat-fact.herokuapp.com/facts/random?amount=1") | |
if err != nil { | |
panic(err) | |
} | |
defer resp.Body.Close() | |
var catfacts CatFacts | |
json.NewDecoder(resp.Body).Decode(&catfacts) | |
fmt.Fprintf(w, catfacts.Text) | |
} | |
func main() { | |
http.HandleFunc("/", response) | |
http.ListenAndServe(":3000", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment