Created
February 24, 2022 09:24
-
-
Save chico/ffc0d06373485ba3f8de210caa817333 to your computer and use it in GitHub Desktop.
Joke go program to fetch & print a random Joke
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" | |
"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