Creating your first HTTP DELETE method
Whenever we want to remove a record that is no longer required then we go with the HTTP DELETE method implementation, which we will cover in this recipe.
How to do it…
- Install the
github.com/gorilla/muxpackage, using thego getcommand, as follows:
$ go get github.com/gorilla/mux- Create
http-rest-delete.gowhere we will define a route that supports the HTTPDELETEmethod and a handler that deletes the employee details for the provided ID from the static array of employees, marshals the array to JSON, and writes it to an HTTP response stream, as follows:
package main
import
(
"encoding/json"
"log"
"net/http"
"github.com/gorilla/mux"
)
const
(
CONN_HOST = "localhost"
CONN_PORT = "8080"
)
type Route struct
{
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
type Routes []Route
var routes = Routes
{
Route
{
"getEmployees",
"GET",
"/employees",
getEmployees,
},
Route
{
"addEmployee",...