Getting a full list of available products

First, let's create a method called GetProducts that takes the *gin.Context type as an argument:

func (h *Handler) GetProducts(c *gin.Context) {}

Next, we need to ensure that our database interface is initialized and not nil, then we use the database layer interface in order to obtain the list of products:

func (h *Handler) GetProducts(c *gin.Context) {  if h.db == nil {    return  }  products, err := h.db.GetAllProducts()}

Now, what happens if the call returns an error? We need to return a JSON document to the client with the error. The response to the client also needs to include an HTTP status code that indicates that the request failed. An HTTP status code is a way to report that an error happened in ...

Get Hands-On Full Stack Development with Go now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.