Delete

The delete handler is also defined for the same paths as read and update. The code is pretty straightforward:

func deleteHotel(c *gin.Context) { 
   hotelId:= c.Param("id") 
   _, found:= repository[hotelId] 
   if !found { 
         c.JSON(http.StatusNotFound, gin.H{"status": "hotel with id not found"}) 
   } else { 
         delete(repository, hotelId) 
         //return OK 
         c.JSON(http.StatusOK, gin.H{"status": "ok"}) 
   } 
} 

If a hotel with the id is not found, then we return 404 (http.StatusNotFound), or else we delete it from the map and return HTTP status code 200 (http.StatusOK).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset