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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                前面小節已經介紹了Web是基于http協議的一個服務,Go語言里面提供了一個完善的net/http包,通過http包可以很方便的就搭建起來一個可以運行的Web服務。同時使用這個包能很簡單地對Web的路由,靜態文件,模版,cookie等數據進行設置和操作。 ## [](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/03.2.md#http包建立web服務器)http包建立Web服務器 ~~~ package main import ( "fmt" "net/http" "strings" "log" ) func sayhelloName(w http.ResponseWriter, r *http.Request) { r.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 main() { http.HandleFunc("/", sayhelloName) //設置訪問的路由 err := http.ListenAndServe(":9090", nil) //設置監聽的端口 if err != nil { log.Fatal("ListenAndServe: ", err) } } ~~~ 上面這個代碼,我們build之后,然后執行web.exe,這個時候其實已經在9090端口監聽http鏈接請求了。 在瀏覽器輸入`http://localhost:9090` 可以看到瀏覽器頁面輸出了`Hello astaxie!` 可以換一個地址試試:`http://localhost:9090/?url_long=111&url_long=222` 看看瀏覽器輸出的是什么,服務器輸出的是什么? 在服務器端輸出的信息如下: [![](https://github.com/astaxie/build-web-application-with-golang/raw/master/zh/images/3.2.goweb.png?raw=true)](https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/images/3.2.goweb.png?raw=true) 圖3.8 用戶訪問Web之后服務器端打印的信息 我們看到上面的代碼,要編寫一個Web服務器很簡單,只要調用http包的兩個函數就可以了。 > 如果你以前是PHP程序員,那你也許就會問,我們的nginx、apache服務器不需要嗎?Go就是不需要這些,因為他直接就監聽tcp端口了,做了nginx做的事情,然后sayhelloName這個其實就是我們寫的邏輯函數了,跟php里面的控制層(controller)函數類似。 > > 如果你以前是Python程序員,那么你一定聽說過tornado,這個代碼和他是不是很像,對,沒錯,Go就是擁有類似Python這樣動態語言的特性,寫Web應用很方便。 > > 如果你以前是Ruby程序員,會發現和ROR的/script/server啟動有點類似。 我們看到Go通過簡單的幾行代碼就已經運行起來一個Web服務了,而且這個Web服務內部有支持高并發的特性,我將會在接下來的兩個小節里面詳細的講解一下Go是如何實現Web高并發的。
                  <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>

                              哎呀哎呀视频在线观看