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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] ### 核心api ***** 請求格式和處理函數綁定 ``` func HandleFunc( pattern string,//請求對格式,如http://localhost/user/login handler func(ResponseWriter, *Request)//處理函數 ) ``` 啟動服務器 ``` func ListenAndServe( addr string,//地址如:8080 handler Handler // ) ``` 創建返回數據的結構體 ``` type H struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{}`json:"data, omitempty"` } `json:"code"`的意思是把json字符串里的key(Code)變為小寫 `json:"data, omitempty"`的意思是如果該字段值為空,則不顯示 ``` ### 正式代碼 ***** ``` package main import ( "encoding/json" "fmt" "io" "log" "net/http" ) func userLogin(writer http.ResponseWriter, request * http.Request) { request.ParseForm() mobile := request.PostForm.Get("mobile") passwd := request.PostForm.Get("passwd") fmt.Println("mobile=",mobile,";passwd=", passwd) loginok := false if mobile == "12" && passwd == "123" { loginok = true } //str := `{"code":0, "data": {"id","1", "token":"test"}` if loginok { data := make(map[string]interface{ }) data["id"] = 1 data["token"] = "test" Resp(writer, 0, data, "") }else { Resp(writer, -1, nil, "密碼不正確") } _, _ = io.WriteString(writer, "hello world") } type H struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{}`json:"data, omitempty"` } func Resp(w http.ResponseWriter, code int, data interface{}, msg string) { w.Header().Set("Content-Type", "application") w.WriteHeader(http.StatusOK) h := H{ Code: code, Msg: msg, Data: data, } //MARK:- 將結構體轉化為輸出 ret,err := json.Marshal(h) if err != nil { log.Println(err.Error()) } _, _ = w.Write(ret) //_, _ = w.Write([]byte(str)) } func main() { http.HandleFunc("/usr/login", userLogin) _ = http.ListenAndServe(":9090", nil) fmt.Println("hello world") } ```
                  <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>

                              哎呀哎呀视频在线观看