。在golang.org給出的示例中的一臺服務器:谷歌的「走出去」和範圍/功能
package main
import (
"flag"
"http"
"io"
"log"
"template"
)
var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18
var fmap = template.FormatterMap{
"html": template.HTMLFormatter,
"url+html": UrlHtmlFormatter,
}
var templ = template.MustParse(templateStr, fmap)
func main() {
flag.Parse()
http.Handle("/", http.HandlerFunc(QR))
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Exit("ListenAndServe:", err)
}
}
func QR(c *http.Conn, req *http.Request) {
templ.Execute(req.FormValue("s"), c)
}
func UrlHtmlFormatter(w io.Writer, v interface{}, fmt string) {
template.HTMLEscape(w, []byte(http.URLEscape(v.(string))))
}
const templateStr = `
<html>
<head>
<title>QR Link Generator</title>
</head>
<body>
{.section @}
<img src="http://chart.apis.google.com/chart?chs=300x300&cht=qr&choe=UTF- 8&chl={@|url+html}"
/>
<br>
{@|html}
<br>
<br>
{.end}
<form action="/" name=f method="GET"><input maxLength=1024 size=70
name=s value="" title="Text to QR Encode"><input type=submit
value="Show QR" name=qr>
</form>
</body>
</html>
`
爲什麼是包含在UrlHtmlFormatter
template.HTMLEscape(w, []byte(http.URLEscape(v.(string))))
?爲什麼不能直接鏈接到"url+html"
?
另外,我怎麼能改變func QR接受參數值?我想讓它做的是接受命令行標誌代替req *http.Request
...在此先感謝...
我很感激所有的迴應,我得到的問題,我問... – danwoods 2010-03-22 02:10:29