Use DELETE Method in GoLang Rest API

This project will demonstrate how to Use DELETE Method in GoLang Rest API

  • GoLangRESTfulAPI
    • src
      • apis
        • productapi
          • product.api.go
      • entities
        • product.entity.go
      • main.go
package entities

type Product struct {
    Id       string  `json:"id"`
    Name     string  `json:"name"`
    Price    float64 `json:"price"`
    Quantity int     `json:"quantity"`
    Status   bool    `json:"status"`
}
package productapi

import (
    "encoding/json"
    "net/http"

    "github.com/gorilla/mux"
)

type ResponseResult struct {
    Id string `json:"id"`
}

func Delete(response http.ResponseWriter, request *http.Request) {
    vars := mux.Vars(request)
    id := vars["id"]
    respondWithJSON(response, http.StatusOK, ResponseResult{Id: id})
}

func respondWithJSON(response http.ResponseWriter, statusCode int, data interface{}) {
    result, _ := json.Marshal(data)
    response.Header().Set("Content-Type", "application/json")
    response.WriteHeader(statusCode)
    response.Write(result)
}
package main

import (
    "apis/productapi"
    "fmt"
    "net/http"

    "github.com/gorilla/mux"
)

func main() {
    router := mux.NewRouter()

    router.HandleFunc("/api/product/delete/{id}", productapi.Delete).Methods("DELETE")

    err := http.ListenAndServe(":3000", router)
    if err != nil {
        fmt.Println(err)
    }
}

Screenshots