I am accessing my Go server from my react app. Here is my code snippet of Go below about which I am concerned:
func getSales(w http.ResponseWriter, r *http.Request) {
item := []Sales{}
fmt.Println(r.URL)
id := chi.URLParam(r, "id") // want to access id variable
rows, err := db.Query(`SELECT customer_id, user_id, cash,balance from sales WHERE id=?`)
// use "id" VARIABLE here as the value of id
if err != nil {
log.Fatal(err)
}
for rows.Next() {
var r Sales
err = rows.Scan(&r.Code, &r.Customer_id, &r.User_id, &r.Cash, &r.Balance)
if err != nil {
log.Fatal(err)
}
item = append(item, r)
}
defer rows.Close()
json.NewEncoder(w).Encode(item)
}
I want to access the id variable in db.Query but I was not able to find any solution.