我正在尝试使用 Fprintf 打印用户键入表单的用户名:
GO代码:
const logPage = `
<html>
<form action="/login" method="POST">
<label for="name">Username</label>
<input type="text" id="Username" name="name"></input>
...
</form>
</html>
`
const homePage = `
<html>
<h1>hi %s</h1>
</html>
`
func homehandler(w http.ResponseWriter, r *http.Request) {
a = r.FormValue("name")
fmt.Fprintf(w, homePage, a) ---> how do I insert the a value in the required interface{} form?
}
func main() {
http.HandleFunc("/home", homehandler)
...
}
根据这个:http://golang.org/pkg/net/http/#Request.FormValue , FormValue返回一个字符串,但是Fprintf好像需要一个接口(interface)类型:http://golang.org/pkg/fmt/#Fprintf .如何在上面的代码中插入正确的“a”值/类型?或者,是否有更好的方法来做到这一点?
最佳答案
我实际上不确定您的代码是否可以按您预期的方式工作。
这是一个稍微修改过的工作示例:
const logPage = `
<html>
<form action="/login" method="POST">
<label for="name">Username</label>
<input type="text" id="Username" name="name"></input>
...
</form>
</html>
`
const homePage = `
<html>
<h1>hi %s</h1>
</html>
`
func loginhandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
fmt.Fprint(w, homePage)
} else if r.Method == "POST" {
// Here you can check the credentials
// or print the page you wanted to
// BUT please DON'T DO THIS
a = r.FormValue("name")
fmt.Fprint(w, logpage, a)
// DON'T DO THIS FOR THE SAKE OF YOUR USERS
}
}
func main() {
// http.HandleFunc("/home", homehandler) not needed
http.HandleFunc("/login", loginhandler)
...
}
事情是这样的:
/login 页面发出GET 请求,并调用loginHandler。loginhandler 中,您将登录页面 html 返回给用户的浏览器。POST请求。但是,您应该始终清理用户数据。 template/html 会为您做这些,所以请看一看。如果您对该软件包的使用有其他疑问,请留言!
关于GO:作为 Fprintf 参数传递的表单值类型不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25266538/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano
question的一些答案关于redirect_to让我想到了其他一些问题。基本上,我正在使用Rails2.1编写博客应用程序。我一直在尝试自己完成大部分工作(因为我对Rails有所了解),但在需要时会引用Internet上的教程和引用资料。我设法让一个简单的博客正常运行,然后我尝试添加评论。靠我自己,我设法让它进入了可以从script/console添加评论的阶段,但我无法让表单正常工作。我遵循的其中一个教程建议在帖子Controller中创建一个“评论”操作,以添加评论。我的问题是:这是“标准”方式吗?我的另一个问题的答案之一似乎暗示应该有一个CommentsController参