Skip to content

Instantly share code, notes, and snippets.

@jonathan-fielding
Last active January 11, 2021 22:13
Show Gist options
  • Save jonathan-fielding/74e2949c3b90ef46a92c00317cec1267 to your computer and use it in GitHub Desktop.
Save jonathan-fielding/74e2949c3b90ef46a92c00317cec1267 to your computer and use it in GitHub Desktop.
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