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条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致