美好的一天!在这个程序中,我为餐厅制作了包含 Handle 功能的菜单。
问题陈述:
我无法连接数组: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>")
}
}
帮我在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>")
}
}
尝试访问:http://localhost:8080/getall或者 http://localhost:8080/get?id=1
关于再会!在这个程序中,我制作了包括 Handle 函数的餐厅菜单。我无法将数组 : Name, Price 与函数 getall 和 get 连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45029816/
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我正在使用这个:4.times{|i|assert_not_equal("content#{i+2}".constantize,object.first_content)}我之前声明过局部变量content1content2content3content4content5我得到的错误NameError:wrongconstantnamecontent2这个错误是什么意思?我很确定我想要content2=\ 最佳答案 你必须用一个大字母来调用ruby常量:Content2而不是content2。Aconstantnamestart
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
“输出”是一个序列化的OpenStruct。定义标题try(:output).try(:data).try(:title)结束什么会更好?:) 最佳答案 或者只是这样:deftitleoutput.data.titlerescuenilend 关于ruby-on-rails-更好的替代方法try(:output).try(:data).try(:name)?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.c
如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只
Unity自动旋转动画1.开门需要门把手先动,门再动2.关门需要门先动,门把手再动3.中途播放过程中不可以再次进行操作觉得太复杂?查看我的文章开关门简易进阶版效果:如果这个门可以直接打开的话,就不需要放置"门把手"如果门把手还有钥匙需要旋转,那就可以把钥匙放在门把手的"门把手",理论上是可以无限套娃的可调整参数有:角度,反向,轴向,速度运行时点击Test进行测试自己写的代码比较垃圾,命名与结构比较拉,高手轻点喷,新手有类似的需求可以拿去做参考上代码usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;u