dongrong1856 2017-07-11 08:55
浏览 31
已采纳

美好的一天! 在此程序中,我制作了包含Handle函数的餐厅菜单。我无法连接数组:Name,Price和函数getall和get

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.

  • 写回答

2条回答 默认 最新

  • doudihuang7642 2017-07-11 10:32
    关注

    I would like to help you as it seems you are genuinely trying to learn go. I've made few changes in your code. I've moved the menu listing in each handler. You may keep it global. Find the changed code below.

    package main
    
    import (
        "fmt"
        "io"
        "net/http"
        "net/url"
        "strconv"
    )
    
    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() {
        fmt.Println(x)
    
        http.HandleFunc("/getall", getall)
        http.HandleFunc("/get", get)
        http.ListenAndServe(":8080", nil)
    
    }
    
    func getall(w http.ResponseWriter, r *http.Request) {
    
        x := []Menu{
            {Name: "Crispy", Price: 31},
            {Name: "Burger", Price: 42},
            {Name: "Superstar", Price: 17},
        }
    
        for _, m := range x {
    
            io.WriteString(w, "<p>"+m.Name+" Price "+strconv.Itoa(m.Price)+"</p>")
        }
    
    }
    
    func get(w http.ResponseWriter, r *http.Request) {
    
        x := []Menu{
            {Name: "Crispy", Price: 31},
            {Name: "Burger", Price: 42},
            {Name: "Superstar", Price: 17},
        }
    
        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
        if a, err := strconv.Atoi(v); err == nil {
            io.WriteString(w, "<p> "+x[a-1].Name+" price "+strconv.Itoa(x[a-1].Price)+"</p>")
        }
    
    }
    

    Try visiting: http://localhost:8080/getall OR http://localhost:8080/get?id=1

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?