Search⌘ K
AI Features

Completing and Deleting Items

Explore how to extend a Go command-line application by implementing complete and delete features for to-do items using REST API calls. Learn to build commands with Cobra, send HTTP PATCH and DELETE requests, and write tests to ensure proper functionality.

Let’s complete the application functionality by adding the two missing features: the complete command to mark an item as done and the del command to delete an item from the list.

According to the to-do REST API requirements, to complete an item we must send an HTTP PATCH request to the endpoint /todo/id?complete, where id is an integer number representing the item in the list. To delete an item from the list, we send an HTTP DELETE request to the endpoint /todo/id, where id is again a number that represents the item.

To send those requests, we’ll reuse the function sendRequest(). We open and edit the file cmd/client.go. We define a new function completeItem() that takes two parameters, the apiRoot as string and id as int. It returns an error. This function uses those two parameters to compose the final URL for the request and then uses the sendRequest() function to send the request to the server: ...