<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國際加速解決方案。 廣告
                [TOC] # 簡介 http WEB服務器: ~~~ 1. 注冊回調函數:http.HandleFunc("/", myHandler) func myHandler(w http.ResponseWriter, r *http.Request) w:給客戶端回發數據 r:從客戶端讀到的數據 type ResponseWriter interface { Header() Header Write([]byte) (int, error) WriteHeader(int) } type Request struct { Method string // 瀏覽器請求方法 GET、POST? URL *url.URL // 瀏覽器請求的訪問路徑 ?? Header Header // 請求頭部 Body io.ReadCloser // 請求包體 RemoteAddr string // 瀏覽器地址 ?? ctx context.Context } 2. 綁定服務器地址結構:http.ListenAndServe("127.0.0.1:8000", nil) 參2:通常傳 ni 。 表示 讓服務端 調用默認的 http.DefaultServeMux 進行處理 ~~~ # 使用 ~~~ func myHandler(w http.ResponseWriter, r *http.Request) { fmt.Println("r.Method = ", r.Method) fmt.Println("r.URL = ", r.URL) fmt.Println("r.Header = ", r.Header) fmt.Println("r.Body = ", r.Body) //回復 io.WriteString(w, "hello world\n") } func main() { //第一個參數是url的 http.HandleFunc("/", myHandler) //用于指定的tcp網絡地址監聽 //第一個參數是監聽地址,第二個參數是服務端處理程序,通常為空,為空表示服務端調用http.DefaultServeMux處理 err := http.ListenAndServe("127.0.0.1:8183", nil) if err != nil { fmt.Println("有錯誤: ", err) } } ~~~ ## 默認 Handler * NotFountHandler ~~~ http.ListenAndServe(":8081", http.NotFoundHandler()) ~~~ * RedirectHandler ~~~ http.ListenAndServe(":8081", http.RedirectHandler("/test", 302)) ~~~ * FileServer ~~~ http.ListenAndServe(":8081", http.FileServer(http.Dir("./"))) ~~~ # get ~~~ func myHandler(w http.ResponseWriter, r *http.Request) { //解析參數,默認是不會解析的 r.ParseForm() fmt.Fprintf(w, "%v\n", r.Form) fmt.Fprintf(w, "path:%s\n", r.URL.Path) fmt.Fprintf(w, "schema:%s\n", r.URL.Scheme) //get查詢字符串 fmt.Fprintf(w, "form:%s\n", r.Form) //控制臺打印 for k, v := range r.Form { fmt.Println("key: ", k) fmt.Println("value: ", strings.Join(v, "")) } fmt.Fprintf(w, "hello world\n") } func main() { //第一個參數是url的 http.HandleFunc("/", myHandler) //用于指定的tcp網絡地址監聽 //第一個參數是監聽地址,第二個參數是服務端處理程序,通常為空,為空表示服務端調用http.DefaultServeMux處理 err := http.ListenAndServe("127.0.0.1:8183", nil) if err != nil { fmt.Println("有錯誤: ", err) } } ~~~ # post ~~~ func myHandler(w http.ResponseWriter, r *http.Request) { method := r.Method if method == "GET" { t, err := template.ParseFiles("./index.html") if err != nil { fmt.Fprintf(w, "載入頁面錯誤") return } t.Execute(w, nil) } else if method == "POST" { r.ParseForm() fmt.Printf("username: %s\n", r.FormValue("username")) fmt.Printf("password: %s\n", r.FormValue("password")) fmt.Fprintf(w, "username: %s, password: %s\n", r.FormValue("username"), r.FormValue("password")) } } func main() { //第一個參數是url的 http.HandleFunc("/", myHandler) //用于指定的tcp網絡地址監聽 //第一個參數是監聽地址,第二個參數是服務端處理程序,通常為空,為空表示服務端調用http.DefaultServeMux處理 err := http.ListenAndServe("127.0.0.1:8183", nil) if err != nil { fmt.Println("有錯誤: ", err) } } ~~~ # 模板 ~~~ "html/template" ~~~ ~~~ func myHandler(w http.ResponseWriter, r *http.Request) { t, err := template.ParseFiles("./index.html") if err != nil { fmt.Fprintf(w, "載入頁面錯誤") return } //第二個參數就是模板那的 t.Execute(w, "mary") } func main() { //第一個參數是url的 http.HandleFunc("/", myHandler) //用于指定的tcp網絡地址監聽 //第一個參數是監聽地址,第二個參數是服務端處理程序,通常為空,為空表示服務端調用http.DefaultServeMux處理 err := http.ListenAndServe("127.0.0.1:8183", nil) if err != nil { fmt.Println("有錯誤: ", err) } } ~~~ 頁面上 ~~~ <h1>{{.}}</h1> ~~~ ## 模板中的結構體 ~~~ type UserInfo struct { Name string Sex string Age int } func myHandler(w http.ResponseWriter, r *http.Request) { t, err := template.ParseFiles("./index.html") if err != nil { fmt.Fprintf(w, "載入頁面錯誤") return } user := UserInfo{ Name: "Mary", Sex: "男", Age: 18, } //第二個參數就是模板那的 t.Execute(w, user) } ~~~ html ~~~ <h1>{{.}}</h1> <h1>{{.Name}}</h1> <h1>{{.Sex}}</h1> <h1>{{.Age}}</h1> ~~~ ## 模板中的map ~~~ t, err := template.ParseFiles("./index.html") if err != nil { fmt.Fprintf(w, "載入頁面錯誤") return } m := make(map[string]interface{}) m["name"] = "mary" m["sex"] = "男" m["age"] = 18 //第二個參數就是模板那的 t.Execute(w, m) ~~~ html ~~~ <h1>{{.}}</h1> <h1>{{.name}}</h1> <h1>{{.sex}}</h1> <h1>{{.age}}</h1> ~~~ ## 判斷 ~~~ {{if gt .age 18}} <p>hello, old man, {{.name}}</p> {{else}} <p>hello, young man, {{.name}}</p> {{end}} ~~~
                  <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>

                              哎呀哎呀视频在线观看