Once we run the program, the HTTP server will start locally listening on port 8080.
Next, executing a DELETE request from the command line as follows, will delete an employee with ID 1 and give us the updated list of employees:
$ curl -H "Content-Type: application/json" -X DELETE -d '{"Id":"1", "firstName": "Foo", "lastName": "Bar"}' http://localhost:8080/employee/delete
This can be seen in the following screenshot:
Let’s understand the change we introduced in this recipe:
- First, we added another route with the name deleteEmployee, which executes the deleteEmployee handler for every DELETE request for the URL pattern /employee/delete.
- Then, we defined a deleteEmployee handler, which basically decodes the employee data that comes as part of a DELETE request using the NewDecoder handler of the built-in encoding/json package of Go, gets the index of the requested employee using the GetIndex helper function, deletes the employee, and writes the updated array as JSON to an HTTP response stream.