<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智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                先來看一個表單遞交的例子,我們有如下的表單內容,命名成文件login.gtpl(放入當前新建項目的目錄里面) ~~~ <html> <head> <title></title> </head> <body> <form action="/login" method="post"> 用戶名:<input type="text" name="username"> 密碼:<input type="password" name="password"> <input type="submit" value="登陸"> </form> </body> </html> ~~~ 上面遞交表單到服務器的`/login`,當用戶輸入信息點擊登陸之后,會跳轉到服務器的路由`login`里面,我們首先要判斷這個是什么方式傳遞過來,POST還是GET呢? http包里面有一個很簡單的方式就可以獲取,我們在前面web的例子的基礎上來看看怎么處理login頁面的form數據 ~~~ package main import ( "fmt" "html/template" "log" "net/http" "strings" ) func sayhelloName(w http.ResponseWriter, r *http.Request) { r.ParseForm() //解析url傳遞的參數,對于POST則解析響應包的主體(request body) //注意:如果沒有調用ParseForm方法,下面無法獲取表單的數據 fmt.Println(r.Form) //這些信息是輸出到服務器端的打印信息 fmt.Println("path", r.URL.Path) fmt.Println("scheme", r.URL.Scheme) fmt.Println(r.Form["url_long"]) for k, v := range r.Form { fmt.Println("key:", k) fmt.Println("val:", strings.Join(v, "")) } fmt.Fprintf(w, "Hello astaxie!") //這個寫入到w的是輸出到客戶端的 } func login(w http.ResponseWriter, r *http.Request) { fmt.Println("method:", r.Method) //獲取請求的方法 if r.Method == "GET" { t, _ := template.ParseFiles("login.gtpl") t.Execute(w, nil) } else { //請求的是登陸數據,那么執行登陸的邏輯判斷 fmt.Println("username:", r.Form["username"]) fmt.Println("password:", r.Form["password"]) } } func main() { http.HandleFunc("/", sayhelloName) //設置訪問的路由 http.HandleFunc("/login", login) //設置訪問的路由 err := http.ListenAndServe(":9090", nil) //設置監聽的端口 if err != nil { log.Fatal("ListenAndServe: ", err) } } ~~~ 通過上面的代碼我們可以看出獲取請求方法是通過`r.Method`來完成的,這是個字符串類型的變量,返回GET, POST, PUT等method信息。 login函數中我們根據`r.Method`來判斷是顯示登錄界面還是處理登錄邏輯。當GET方式請求時顯示登錄界面,其他方式請求時則處理登錄邏輯,如查詢數據庫、驗證登錄信息等。 當我們在瀏覽器里面打開`http://127.0.0.1:9090/login`的時候,出現如下界面 [![](https://github.com/astaxie/build-web-application-with-golang/raw/master/zh/images/4.1.login.png?raw=true)](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/images/4.1.login.png?raw=true) 圖4.1 用戶登錄界面 我們輸入用戶名和密碼之后發現在服務器端是不會打印出來任何輸出的,為什么呢?默認情況下,Handler里面是不會自動解析form的,必須顯式的調用`r.ParseForm()`后,你才能對這個表單數據進行操作。我們修改一下代碼,在`fmt.Println("username:", r.Form["username"])`之前加一行`r.ParseForm()`,重新編譯,再次測試輸入遞交,現在是不是在服務器端有輸出你的輸入的用戶名和密碼了。 `r.Form`里面包含了所有請求的參數,比如URL中query-string、POST的數據、PUT的數據,所有當你在URL的query-string字段和POST沖突時,會保存成一個slice,里面存儲了多個值,Go官方文檔中說在接下來的版本里面將會把POST、GET這些數據分離開來。 現在我們修改一下login.gtpl里面form的action值`http://127.0.0.1:9090/login`修改為`http://127.0.0.1:9090/login?username=astaxie`,再次測試,服務器的輸出username是不是一個slice。服務器端的輸出如下: [![](https://github.com/astaxie/build-web-application-with-golang/raw/master/zh/images/4.1.slice.png?raw=true)](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/images/4.1.slice.png?raw=true) 圖4.2 服務器端打印接受到的信息 `request.Form`是一個url.Values類型,里面存儲的是對應的類似`key=value`的信息,下面展示了可以對form數據進行的一些操作: ~~~ v := url.Values{} v.Set("name", "Ava") v.Add("friend", "Jess") v.Add("friend", "Sarah") v.Add("friend", "Zoe") // v.Encode() == "name=Ava&friend=Jess&friend=Sarah&friend=Zoe" fmt.Println(v.Get("name")) fmt.Println(v.Get("friend")) fmt.Println(v["friend"]) ~~~ > **Tips**: Request本身也提供了FormValue()函數來獲取用戶提交的參數。如r.Form["username"]也可寫成r.FormValue("username")。調用r.FormValue時會自動調用r.ParseForm,所以不必提前調用。r.FormValue只會返回同名參數中的第一個,若參數不存在則返回空字符串。
                  <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>

                              哎呀哎呀视频在线观看