Function 谷歌';s';go';和范围/职能
在golang.org上给出的一个示例服务器中:Function 谷歌';s';go';和范围/职能,function,scope,go,Function,Scope,Go,在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.HTMLFormat
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>
`
主程序包
进口(
“旗帜”
“http”
“io”
“日志”
“模板”
)
var addr=flag.String(“addr”,“:1718”,“http服务地址”)//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))
错误:=http.ListenAndServe(*addr,nil)
如果错误!=零{
log.Exit(“ListendServe:”,错误)
}
}
func QR(c*http.Conn,req*http.Request){
模板执行(请求FormValue(“s”),c)
}
func UrlHtmlFormatter(w io.Writer,v接口{},fmt字符串){
HTMLEscape(w,[]字节(http.URLEscape(v.(string)))
}
const templateStr=`
QR链路发生器
{.section@}
{@| html}
{.end}
`
为什么template.HTMLEscape(w,[]字节(http.URLEscape(v.(string)))
包含在UrlHtmlFormatter
中?为什么它不能直接链接到“url+html”
另外,如何更改func QR以接受参数值?我想让它做的是接受一个命令行标志来代替
req*http.Request
。。。提前感谢…函数模板的签名。HTMLEscape
是:
func(w io.Writer, s []byte)
type FormatterMap map[string]func(io.Writer, interface{}, string)
template.FormatterMap
的类型声明为:
func(w io.Writer, s []byte)
type FormatterMap map[string]func(io.Writer, interface{}, string)
因此,FormatterMap
map元素值函数的签名为:
func(io.Writer, interface{}, string)
UrlHtmlFormatter
函数是一个包装器,用于为HtmlCape
函数提供FormatterMap
映射元素值函数签名
func UrlHtmlFormatter(w io.Writer, v interface{}, fmt string) {
template.HTMLEscape(w, []byte(http.URLEscape(v.(string))))
}
函数
模板.HTMLEscape
的签名为:
func(w io.Writer, s []byte)
type FormatterMap map[string]func(io.Writer, interface{}, string)
template.FormatterMap
的类型声明为:
func(w io.Writer, s []byte)
type FormatterMap map[string]func(io.Writer, interface{}, string)
因此,FormatterMap
map元素值函数的签名为:
func(io.Writer, interface{}, string)
UrlHtmlFormatter
函数是一个包装器,用于为HtmlCape
函数提供FormatterMap
映射元素值函数签名
func UrlHtmlFormatter(w io.Writer, v interface{}, fmt string) {
template.HTMLEscape(w, []byte(http.URLEscape(v.(string))))
}
您编辑了原始问题以添加第二个问题 另外,如何将func QR更改为 是否接受参数值?我想要什么 它要做的就是接受命令行 替换req*http.Request的标志 如果您阅读,包括,您将看到Go具有强大的静态类型,包括函数类型。虽然这不能保证捕获所有错误,但它通常捕获使用无效的、不匹配的函数签名的尝试 您没有告诉我们为什么要更改
QR
的函数签名,这似乎是一种任意和反复无常的方式,因此它不再是有效的HandlerFunc
类型,从而保证程序甚至无法编译。我们只能猜测你想要完成什么。也许就这么简单:您希望根据运行时参数修改http.Request
。也许是这样的:
// Note: flag.Parse() in func main() {...}
var qrFlag = flag.String("qr", "", "function QR parameter")
func QR(c *http.Conn, req *http.Request) {
if len(*qrFlag) > 0 {
// insert code here to use the qr parameter (qrFlag)
// to modify the http.Request (req)
}
templ.Execute(req.FormValue("s"), c)
}
也许不是!谁知道呢?您编辑了原始问题以添加第二个问题 另外,如何将func QR更改为 是否接受参数值?我想要什么 它要做的就是接受命令行 替换req*http.Request的标志 如果您阅读,包括,您将看到Go具有强大的静态类型,包括函数类型。虽然这不能保证捕获所有错误,但它通常捕获使用无效的、不匹配的函数签名的尝试 您没有告诉我们为什么要更改
QR
的函数签名,这似乎是一种任意和反复无常的方式,因此它不再是有效的HandlerFunc
类型,从而保证程序甚至无法编译。我们只能猜测你想要完成什么。也许就这么简单:您希望根据运行时参数修改http.Request
。也许是这样的:
// Note: flag.Parse() in func main() {...}
var qrFlag = flag.String("qr", "", "function QR parameter")
func QR(c *http.Conn, req *http.Request) {
if len(*qrFlag) > 0 {
// insert code here to use the qr parameter (qrFlag)
// to modify the http.Request (req)
}
templ.Execute(req.FormValue("s"), c)
}
也许不是!谁知道呢?对不起。更改函数QR的目的是将变量作为参数传递给它。对不起。更改函数QR的目的是将一个变量作为参数传递给它。我很感激我对我提出的问题的所有回答…我很感激我对我提出的问题的所有回答。。。