<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                下邊的程序在端口8088上啟動了一個網頁服務器;`SimpleServer`會處理`/test1`url使它在瀏覽器輸出`hello world`。`FormServer`會處理'/test2`url:如果url最初由瀏覽器請求,那么它就是一個`GET`請求,并且返回一個`form`常量,包含了簡單的`input`表單,這個表單里有一個文本框和一個提交按鈕。當在文本框輸入一些東西并點擊提交按鈕的時候,會發起一個`POST`請求。`FormServer`中的代碼用到了`switch`來區分兩種情況。在`POST`情況下,使用`request.FormValue("inp")`通過文本框的`name`屬性`inp`來獲取內容,并寫回瀏覽器頁面。在控制臺啟動程序并在瀏覽器中打開url`http://localhost:8088/text2`來測試這個程序: 示例 15.10?[simple_webserver.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_15/simple_webserver.go) ~~~ package main import ( "io" "net/http" ) const form = ` <html><body> <form action="#" method="post" name="bar"> <input type="text" name="in" /> <input type="submit" value="submit"/> </form> </body></html> ` /* handle a simple get request */ func SimpleServer(w http.ResponseWriter, request *http.Request) { io.WriteString(w, "<h1>hello, world</h1>") } func FormServer(w http.ResponseWriter, request *http.Request) { w.Header().Set("Content-Type", "text/html") switch request.Method { case "GET": /* display the form to the user */ io.WriteString(w, form) case "POST": /* handle the form data, note that ParseForm must be called before we can extract form data */ //request.ParseForm(); //io.WriteString(w, request.Form["in"][0]) io.WriteString(w, request.FormValue("in")) } } func main() { http.HandleFunc("/test1", SimpleServer) http.HandleFunc("/test2", FormServer) if err := http.ListenAndServe(":8088", nil); err != nil { panic(err) } } ~~~ 注:當使用字符串常量表示html文本的時候,包含`<html><body></body></html>`對于讓瀏覽器識別它收到了一個html非常重要。 更安全的做法是在處理器中使用`w.Header().Set("Content-Type", "text/html")`在寫入返回之前將`header`的`content-type`設置為`text/html` `content-type`會讓瀏覽器認為它可以使用函數`http.DetectContentType([]byte(form))`來處理收到的數據 練習 15.6 [statistics.go] 編寫一個網頁程序,可以讓用戶輸入一連串的數字,然后將它們打印出來,計算出這些數字的均值和中值,就像下邊這張截圖一樣: [![](https://github.com/Unknwon/the-way-to-go_ZH_CN/raw/master/images/15.4_fig15.1.jpg?raw=true)](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/images/15.4_fig15.1.jpg?raw=true)
                  <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>

                              哎呀哎呀视频在线观看