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

                路由進階與目錄架構 === 1.今天課程第一部分就是先把,上節課路由沒有將完的將完 2.第二部分是就目錄結構將來 后面兩節課程的計劃是golang操作mysql 然后在做一個登錄登出的小案例,自己實現session ***** 1.路由序章 - get 接收數據 - post 接收數據 get數據接收 ``` // url: http:127.0.0.1:8085/name/:name // p httprouter.Params 可以通過 p.ByName("name") 來獲取值 // 現在我們來編譯測試一下吧 func Name(w http.ResponseWriter,r *http.Request,p httprouter.Params) { name := p.ByName("name") w.Write([]byte("you name:"+name)) } // url: get http:127.0.0.1:8085/registeredusers // 演示get請求獲取數據 func RegisteredUsersGet(w http.ResponseWriter,r *http.Request,p httprouter.Params) { r.ParseForm() // 解析url傳遞的參數,對于POST則解析響應包的主體(request body) // 注意:如果沒有調用ParseForm方法,下面無法獲取表單的數據 name := r.Form["name"] password := r.Form["password"] // 這里獲取到的是[]string if len(name)==0 || len(password)==0 { // 如果沒有傳入 就訪問這個url 處理 // 這里我先不封裝,后面的課程會講分離 // RESTful知道吧 參數異常返回400 w.WriteHeader(http.StatusBadRequest) //設置返回狀態碼 w.Write([]byte("參數沒有傳,你干撒呢!")) return } w.Write([]byte("name: "+name[0]+" password: "+password[0])) // 此處小朋友會疑惑嗎name[0] 接收的參數為什么是[]string /** 我來解釋 用postman測試,提交http://localhost:8080/?uid=111 服務端輸出 :[111] 提交: http://localhost:8080/?uid=111&uid=222 服務端輸出:[111 222] */ } ``` post 數據接收 ``` // url: post http:127.0.0.1:8085/registeredusers // 演示post請求獲取數據 func RegisteredUsersPOST(w http.ResponseWriter,r *http.Request,p httprouter.Params) { /** 注意.Form r.Form包含了GET、POST參數 POST:為 application/x-www-form-urlencoded 提交的蛤 */ r.ParseForm() name := r.Form["name"] password := r.Form["password"] if len(name) == 0 || len(password) == 0 { w.WriteHeader(http.StatusBadRequest) w.Write([]byte("參數!")) return } w.Write([]byte("name: "+name[0]+" password: "+password[0])) } ``` 這里要注意一下json提交的上一中post是接收不了的 ### 現在這個把他整個目錄都重新架構一遍 ``` . ├── auth 鑒權層 │?? └── auth.go ├── dbops 數據庫操作 ├── defs 數據體定義 ├── go.mod ├── go.sum ├── LICENSE ├── main.go ├── response 處理返回 │?? └── response.go ├── router 路由層 │?? ├── handlers.go │?? └── router.go ├── session session處理 └── utils 工具類 ``` 現在吧那個返回封裝一下 在defs下創建err.go err相關返回定義 ``` package defs import "net/http" type Err struct { Error string `json:"error"` //這是打tag ErrorCode string `json:"error_code"` } type ErrResponse struct { HttpSc int Error Err } // 定義常用返回信息 var ( // 定義參數錯誤返回類型 ErrorRequestBodyParseFailed = ErrResponse{Error:Err{Error:"Request body is not correct",ErrorCode:"001"},HttpSc:http.StatusBadRequest} ) ``` 定義request下的request.go ``` package response import ( "GolangWebCourseware/defs" "encoding/json" "net/http" ) // 當返回錯誤信息時 func sendErrorResponse(w http.ResponseWriter,errResp defs.ErrResponse) { w.WriteHeader(errResp.HttpSc) bytes, _ := json.Marshal(errResp.Error) w.Write(bytes) } ``` 然后直接就可以這樣寫了 ``` // url: post http:127.0.0.1:8085/registeredusers // 演示post請求獲取數據 func RegisteredUsersPOST(w http.ResponseWriter,r *http.Request,p httprouter.Params) { /** 注意.Form r.Form包含了GET、POST參數 POST:為 application/x-www-form-urlencoded 提交的蛤 */ r.ParseForm() name := r.Form["name"] password := r.Form["password"] if len(name) == 0 || len(password) == 0 { response.SendErrorResponse(w,defs.ErrorRequestBodyParseFailed) return } w.Write([]byte("name: "+name[0]+" password: "+password[0])) } ``` 好了現在處理最后一步,接收json數據 1.第一步建立當前接收json數據的結構體 ``` package defs type user struct { name string `json:"name"` password string `json:"password"` } ``` 2.讀body然后序列化 ``` // url: post http:127.0.0.1:8085/rpjson func RegisteredUsersPOSTByJSON(w http.ResponseWriter,r *http.Request,p httprouter.Params) { //讀取body 這里就用ioutil包吧,這個沒有包含文件不會太大 bytes, _ := ioutil.ReadAll(r.Body) // 創建一個用戶接收數據的空對象 userinfo := &defs.User{} //序列化 err := json.Unmarshal(bytes, userinfo) if err != nil { response.SendErrorResponse(w,defs.ErrorRequestBodyParseFailed) return } fmt.Println(userinfo) w.Write(bytes) } ``` 本次課程代碼: [https://github.com/dollarkillerx/GolangWebCourseware/tree/%E8%B7%AF%E7%94%B1%E8%BF%9B%E9%98%B6%E4%B8%8E%E7%9B%AE%E5%BD%95%E6%9E%B6%E6%9E%84](https://github.com/dollarkillerx/GolangWebCourseware/tree/%E8%B7%AF%E7%94%B1%E8%BF%9B%E9%98%B6%E4%B8%8E%E7%9B%AE%E5%BD%95%E6%9E%B6%E6%9E%84) OK,golang是不是非常的爽啊!,如此優雅! 下一刻 golang操作mysql ***** ### 接受數據推薦這個: - post ``` func RegisteredUsersPOST(w http.ResponseWriter,r *http.Request,p httprouter.Params) { r.ParseForm() r.PostForm.Get("key") } ``` - get ``` query := r.URL.Query() uid := query["uid"][0] fmt.Println(uid) ```
                  <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>

                              哎呀哎呀视频在线观看