From a9539f7e8b83dfcf6f219288566b945c6f14fc21 Mon Sep 17 00:00:00 2001
From: Martin Schmollinger <martin.schmollinger@gmail.com>
Date: Fri, 14 May 2021 17:13:30 +0200
Subject: [PATCH] Initial solution to exercise gojoke

---
 src/gojoke/go.mod   |  3 +++
 src/gojoke/main.go  | 27 +++++++++++++++++++++++++++
 src/gojoke/types.go | 12 ++++++++++++
 3 files changed, 42 insertions(+)
 create mode 100644 src/gojoke/go.mod
 create mode 100644 src/gojoke/main.go
 create mode 100644 src/gojoke/types.go

diff --git a/src/gojoke/go.mod b/src/gojoke/go.mod
new file mode 100644
index 0000000..c2acb83
--- /dev/null
+++ b/src/gojoke/go.mod
@@ -0,0 +1,3 @@
+module github.com/turngeek/examples-go/src/gojoke
+
+go 1.16
diff --git a/src/gojoke/main.go b/src/gojoke/main.go
new file mode 100644
index 0000000..a7cc87e
--- /dev/null
+++ b/src/gojoke/main.go
@@ -0,0 +1,27 @@
+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)
+}
diff --git a/src/gojoke/types.go b/src/gojoke/types.go
new file mode 100644
index 0000000..01a79d4
--- /dev/null
+++ b/src/gojoke/types.go
@@ -0,0 +1,12 @@
+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"`
+}
-- 
GitLab