Good day! In this program, I make the menu for restaurant including Handle function.
Problem Statement:
I can't connect array: Name, Price
with functions getall
and get
.
package main
import (
"fmt"
"net/http"
"io"
"strconv"
"net/url"
)
type Menu struct {
Name string
Price int
description string
}
func (m Menu) String() string {
return fmt.Sprintf("%s: %s", m.Name, m.Price, )
}
func main() {
x :=[]Menu{
{Name:"Crispy", Price:31},
{Name:"Burger", Price:42},
{Name:"Superstar",Price:17},
/*{"Cola", 26},
{Name:"Superstar", Price:40},
{Name:"Nuggets", Price:19},*/
}
//getall(&x)
fmt.Println(x)
http.HandleFunc("/getall", getall)
http.HandleFunc("/get", get)
http.ListenAndServe(":8080", nil)
}
func getall(w http.ResponseWriter, r *http.Request) {
for _,num := range []Menu{}{
io.WriteString(w, "<p>"+m.Name+" Price "+strconv.Itoa(num)+"</p>")
}
}
func get(w http.ResponseWriter, r *http.Request) {
url := url.URL{}
println(url.String())
attr := r.URL.Query()
fmt.Println(attr)
id := attr["id"][0]
fmt.Println("id of request " + id)
v := id
d:= []Menu{}
if a, err := strconv.Atoi(v);
err == nil {
io.WriteString(w, "<p> "+Name[a-1]+" price "+strconv.Itoa(d[a-1])+"</p>")
}
}
Help me to implement array in getall
and get
function.