Skip to content

Instantly share code, notes, and snippets.

@yetanotherchris
Last active November 27, 2020 17:44
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 yetanotherchris/83cea998583863a05b6f7861756aa54d to your computer and use it in GitHub Desktop.
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)
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, &quoteData)
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