Go 很好的一天!在这个程序中,我为餐厅制作了包含把手功能的菜单;t connect array:Name、Price和函数getall和get
你好!在这个程序中,我制作了包含把手功能的餐厅菜单 问题陈述:Go 很好的一天!在这个程序中,我为餐厅制作了包含把手功能的菜单;t connect array:Name、Price和函数getall和get,go,Go,你好!在这个程序中,我制作了包含把手功能的餐厅菜单 问题陈述: 我无法将数组:Name,Price与函数getall和get连接起来 package main import ( "fmt" "net/http" "io" "strconv" "net/url" ) type Menu struct { Name string Price int description string } func (m Menu) Stri
我无法将数组:
Name,Price
与函数getall
和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>")
}
}
主程序包
进口(
“fmt”
“net/http”
“io”
“strconv”
“网络/网址”
)
类型菜单结构{
名称字符串
价格整数
描述字符串
}
func(m菜单)字符串()字符串{
返回fmt.Sprintf(“%s:%s”,m.Name,m.Price,)
}
func main(){
x:=[]菜单{
{名称:“脆皮”,价格:31},
{名称:“汉堡”,价格:42},
{名称:“超级明星”,价格:17},
/*{“可乐”,26},
{名称:“超级明星”,价格:40},
{名称:“金块”,价格:19}*/
}
//getall(&x)
格式打印LN(x)
http.HandleFunc(“/getall”,getall)
http.HandleFunc(“/get”,get)
http.listendServe(“:8080”,无)
}
func getall(w http.ResponseWriter,r*http.Request){
对于{,num:=range[]菜单{}{
io.WriteString(w,“”+m.Name+“Price”+strconv.Itoa(num)+“”)
}
}
func get(w http.ResponseWriter,r*http.Request){
url:=url.url{}
println(url.String())
attr:=r.URL.Query()
fmt.Println(属性)
id:=attr[“id”][0]
格式打印项次(“请求id”+id)
v:=id
d:=[]菜单{}
如果a,err:=strconv.Atoi(v);
err==nil{
io.WriteString(w,“”+名称[a-1]+“价格”+strconv.Itoa(d[a-1])+“”)
}
}
请帮助我实现
getall
和get
函数中的数组。我很乐意帮助您,因为您似乎真的在尝试学习围棋。我对你的代码做了一些更改。我已经移动了每个处理程序中的菜单列表。你可以保持它的全球性。在下面查找更改的代码
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>")
}
}
主程序包
进口(
“fmt”
“io”
“net/http”
“网络/网址”
“strconv”
)
类型菜单结构{
名称字符串
价格整数
描述字符串
}
func(m菜单)字符串()字符串{
返回fmt.Sprintf(“%s:%s”,m.名称,m.价格)
}
func main(){
格式打印LN(x)
http.HandleFunc(“/getall”,getall)
http.HandleFunc(“/get”,get)
http.listendServe(“:8080”,无)
}
func getall(w http.ResponseWriter,r*http.Request){
x:=[]菜单{
{名称:“脆皮”,价格:31},
{名称:“汉堡”,价格:42},
{名称:“超级明星”,价格:17},
}
对于u,m:=范围x{
io.WriteString(w,“”+m.Name+“Price”+strconv.Itoa(m.Price)+“”)
}
}
func get(w http.ResponseWriter,r*http.Request){
x:=[]菜单{
{名称:“脆皮”,价格:31},
{名称:“汉堡”,价格:42},
{名称:“超级明星”,价格:17},
}
url:=url.url{}
println(url.String())
attr:=r.URL.Query()
fmt.Println(属性)
id:=attr[“id”][0]
格式打印项次(“请求id”+id)
v:=id
如果a,err:=strconv.Atoi(v);err==nil{
io.WriteString(w,“”+x[a-1]。Name+“price”+strconv.Itoa(x[a-1]。price)+“”)
}
}
尝试访问:或用户
为m:=范围菜单{…}
实现getall(),为i使用,为范围菜单{if i==spect{…}
实现get()非常感谢您的帮助,但我仍然希望在主函数中使用菜单,然后在其他函数中使用它。还是不懂怎么做。无论如何,谢谢你