【发布时间】:2014-09-24 01:57:10
【问题描述】:
我有一个使用 Go 构建的网络表单。用户输入一个数字,然后我需要对该数字进行一些数学运算。似乎所有使用 http 包的方法都使用字符串作为输出。
如何对用户输入进行简单的数学运算?
这是我的基本代码:
func init() {
http.HandleFunc("/", root)
http.HandleFunc("/result", result)
}
// handle the root url
func root(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, inputForm)
}
// root url html
const inputForm = `<html>
<body>
<form action="/result" method="post">
<div>Number between 0 and 1
<input type="text" name="anar">
</div>
</form>
</body>
</html>
`
func result(w http.ResponseWriter, r *http.Request) {
input := r.FormValue("anar") // FormValue is always string
// add number to input
newNum := input + 0.25
// stuff to output result
}
【问题讨论】: