Skip to content

Instantly share code, notes, and snippets.

@chico
Created February 24, 2022 09:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chico/ffc0d06373485ba3f8de210caa817333 to your computer and use it in GitHub Desktop.
Save chico/ffc0d06373485ba3f8de210caa817333 to your computer and use it in GitHub Desktop.
Joke go program to fetch & print a random Joke
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
type jokeBody struct {
Joke string `json:"joke"`
}
func main() {
url := "https://sv443.net/jokeapi/v2/joke/Programming?type=single"
c := http.Client{ Timeout: time.Second * 10 }
req, _ := http.NewRequest(http.MethodGet, url, nil)
res, _ := c.Do(req)
if res.Body != nil { defer res.Body.Close() }
body, _ := ioutil.ReadAll(res.Body)
j := jokeBody{}
json.Unmarshal(body, &j)
fmt.Printf(j.Joke)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment