Go Tutorial: Retrieve a random entry from the ZenQuotes API

If you’re a new developer looking to learn how to work with web APIs in Go, the ZenQuotes API is a great place to start. This API provides a collection of inspiring quotes that you can use to test your skills in retrieving data over the internet.

In this walkthrough, we will guide you through the process of writing a simple program in Go that will retrieve a random quote from the ZenQuotes API. We will break down the steps required to send a GET request to the API, decode the JSON response, and print the quote to the console.

By the end of this tutorial, you will have a solid understanding of how to work with web APIs in Go, and will be able to use these skills in your own projects.

Get a Random Inspirational Quote in GO

This simple program sends a GET request to the ZenQuotes API at https://zenquotes.io/api/random and reads the response body. It then unmarshals the JSON response into a Quote struct and prints the quote and author to the console. Let’s get started:

package main

This line indicates that the following code is part of the main package, which is the default package in Go that includes the main function.

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

These lines import the necessary packages.

  • Encoding/json is used to decode the JSON response returned by the API
  • fmt is used to print output to the console
  • io/ioutil is used to read the response body
  • net/http is used to send HTTP requests
type Quote struct {
	Quote string `json:"q"`
	Author string `json:"a"`
}

This defines a struct called Quote that has two fields: Quote and Author. The json struct tags are used to map the fields to the corresponding keys in the JSON response returned by the API.

func main() {

	//Sends GET request to the ZenQuotes API using http.Get(), and check for errors using the err variable 
	//If there is an error, the program prints an error message and return
	resp, err := http.Get("https://zenquotes.io/api/random")
	if err != nil {
		fmt.Println("Failed to fetch quote:", err)
		return
	}
	defer resp.Body.Close()

	// If there is no error, read the response body using ioutil.ReadAll()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Failed to read response body:", err)
		return
	}

	// Create an empty Quote struct and unmarshal the JSON response into the struct using json.Unmarshal() 
	// If there is an error in parsing the JSON response, print an error message and return
	var quote Quote
	err = json.Unmarshal(body, &quote)
	if err != nil {
		fmt.Println("Failed to parse response body:", err)
		return
	}

	//Prints the quote and author to the console using fmt.Println().
	fmt.Println(quote.Quote)
	fmt.Println("  - ", quote.Author)
}

This is the main() function, which is the entry point of the program. It sends a GET request to the ZenQuotes API using http.Get(), and checks for errors using the err variable. If there is an error, the program prints an error message and returns.

If there is no error, the program reads the response body using ioutil.ReadAll(). It then creates an empty Quote struct and unmarshals the JSON response into the struct using json.Unmarshal(). If there is an error in parsing the JSON response, the program prints an error message and returns.

Finally, the program prints the quote and author to the console using fmt.Println().

The completed code is below:

package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

type Quote struct {
	Quote string `json:"q"`
	Author string `json:"a"`
}

func main() {
	resp, err := http.Get("https://zenquotes.io/api/random")
	if err != nil {
		fmt.Println("Failed to fetch quote:", err)
		return
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Failed to read response body:", err)
		return
	}

	var quote Quote
	err = json.Unmarshal(body, &quote)
	if err != nil {
		fmt.Println("Failed to parse response body:", err)
		return
	}

	fmt.Println(quote.Quote)
	fmt.Println("  - ", quote.Author)
}

Subscribe