<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 5.15\. Web服務器 現在讓我們來實現一個完整的程序:一個簡單的web服務器。這其實是一個轉發服務器。 google的[http://chart.apis.google.com](http://chart.apis.google.com/) 提供了一個將數據轉換為圖表的服務。不過那個圖表的轉換程序使用比較復雜,因為需要用戶 自己設置各種參數。不過我們這里的程序界面要稍微友好一點:因為我們只需要獲取一小段數據, 然后調用google的圖表轉換程序生存QR碼(Quick Response縮寫,二維條碼),對于文本 信息下編碼。二維條碼圖像可以用手機上的攝像機采集,然后解析得到解碼后的信息。 下面是完整的程序: ``` package main import ( "flag" "http" "io" "log" "strings" "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, strings.Bytes(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=&##34;Show QR" name=qr> </form> </body> </html> ` ``` main函數的開始部分比較簡單。有一個flag選項用于指定HTTP服務器的監聽端口。 還有一個模板變量templ,主要用于保存HTML頁面的生成模板,我們稍后會討論。 main首先分析命令行選項,然后幫定QR函數為服務器的根目錄 處理函數。最后http.ListenAndServe啟動服務器,并在服務器運行期間一直 阻塞。 QR接收客戶端請求,然后用表單中的s變量的值替換到模板。 template包實現了json-template。 我們的程序的簡潔正是得益于template包的強大功能。本質上,在執行templ.Execute的 時候,根據需要替換調模板中的某些區域。這里的原始模板文本保存在templateStr中, 其中花括弧部分對應模板的動作。在{.section @}和{.end}之間的 以@開頭的元素,在處理模板的時候會被替換。 標記{@|url+html}的意思是在格式化模板的時候,用格式化字典(fmap) 中"url+html" 關鍵字對應的函數的處理標簽的替代文本。這里的UrlHtmlFormatter 函數,只是為了安全啟見過濾包含的不合法信息。 這里的模板只是用于顯式的html頁面。如果覺得上面的解釋比較簡略的話,可以看到template包的 documentation。 我們僅僅用很少的代碼加一些HTML文本就實現了一個有意思的webserver。使用go,往往用很少的 代碼就能實現強大的功能。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看