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

报告相同问题?

悬赏问题

  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗