Last active
November 27, 2020 17:44
-
-
Save yetanotherchris/83cea998583863a05b6f7861756aa54d to your computer and use it in GitHub Desktop.
Quote of the day in Go Lang (playing with Go for the first time)
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" | |
"log" | |
"net/http" | |
) | |
// go run main.go | |
func main() { | |
url := "http://api.forismatic.com/api/1.0/?method=getQuote&format=json&lang=en" | |
var response, err = http.DefaultClient.Get(url) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer response.Body.Close() | |
bodyBytes, err := ioutil.ReadAll(response.Body) | |
if err != nil { | |
log.Fatal(err) | |
} | |
//bodyString := string(bodyBytes) | |
//fmt.Print(bodyString) | |
var quoteData QuoteData | |
json.Unmarshal(bodyBytes, "eData) | |
fmt.Println(quoteData.QuoteText) | |
author := quoteData.QuoteAuthor | |
if len(author) < 1 { | |
author = "unknown" | |
} | |
fmt.Println(" - " + author) | |
} | |
type QuoteData struct { | |
QuoteAuthor string | |
QuoteText string | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment