亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Google的“執行”和范圍/功能

Google的“執行”和范圍/功能

Go
aluckdog 2021-03-30 13:12:49
在golang.org提供的示例服務器之一中:package mainimport (    "flag"    "http"    "io"    "log"    "template")var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18var 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=70name=s value="" title="Text to QR Encode"><input type=submitvalue="Show QR" name=qr></form></body></html>`  為什么template.HTMLEscape(w, []byte(http.URLEscape(v.(string))))包含在其中UrlHtmlFormatter?為什么不能直接鏈接到它"url+html"?另外,如何更改func QR以接受參數值?我想要它做的是接受一個命令行標志代替req *http.Request...預先感謝...
查看完整描述

2 回答

?
森林海

TA貢獻2011條經驗 獲得超2個贊

您編輯了原始問題以添加第二個問題。


另外,如何更改func QR以接受參數值?我想要它做的是接受一個命令行標志來代替req * http.Request。


如果您閱讀《 Go編程語言規范》,§Types(包括§Function類型),您將發現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)

}

也許不是!誰知道?


查看完整回答
反對 回復 2021-04-26
  • 2 回答
  • 0 關注
  • 265 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號