Skip to content
Snippets Groups Projects
Commit a9539f7e authored by Martin Schmollinger's avatar Martin Schmollinger
Browse files

Initial solution to exercise gojoke

parent b649ad1d
No related branches found
No related tags found
No related merge requests found
module github.com/turngeek/examples-go/src/gojoke
go 1.16
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
const (
url = "http://api.icndb.com/jokes/random"
)
func main() {
fmt.Printf("Calling joke service %s ...\n", url)
resp, err := http.Get(url)
if err != nil {
log.Fatalf("Can't access %s\n#%v", url, err)
}
jokeResponse := JokeResponse{}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&jokeResponse)
if err != nil {
log.Fatalf("Bad response\n:#%v", err)
}
log.Printf("Joke of the Day:\n%s", jokeResponse.Value.Joke)
}
package main
type JokeResponse struct {
Type string `json:"type"`
Value Value `json:"value"`
}
type Value struct {
Id int `json:"id"`
Joke string `json:"joke"`
Categories []string `json:"categories"`
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment