I've created a RESTFul API via golang.
The Issue is that when I send a /product
request I will be given the result in json and when I repeat this request the result will append prevoius.
I want to clear the REST data buffer and whenever I send a request, API send me fresh data, not with prevoius. What should I do?
Route Handler
func main() {
router := mux.NewRouter()
router.HandleFunc("/product", GetProductInfo).Methods("GET")
log.Printf("Listenning on Port %s ...
", PORT)
log.Fatal(http.ListenAndServe(PORT, router))
}
Request Handler
type ProductOut struct {
ID int `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Price string `json:"price,omitempty"`
Location string `json:"location,omitempty"`
Created_at string `json:"created_at,omitempty"`
Images []string `json:"images,omitempty"`
}
var product_out []ProductOut
func GetProductInfo(w http.ResponseWriter, r *http.Request) {
db_select_products := db.SelectProducts() // Gets data from database
var out ProductOut
for _, ele := range db_select_products {
out.ID = contentGetFieldInteger(ele, "out_id") // A method for getting integer from struct field
out.Title = contentGetFieldString(ele, "out_title") // A method for getting string from struct field
out.Description = contentGetFieldString(ele, "out_description")
out.Price = contentGetFieldString(ele, "out_price")
out.Location = contentGetFieldString(ele, "out_location")
out.Created_at = contentGetFieldString(ele, "out_created_at")
db_select_image := db.SelectImages(out.ID) // Gets another data from database
for _, imele := range db_select_image {
out.Images = append(out.Images, imageGetFieldString(imele, "out_path"))
fmt.Println(out.Images)
}
product_out = append(product_out, out)
out.Images = nil
}
json.NewEncoder(w).Encode(product_out)
}