91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Go編程基礎4-Http服務器

發布時間:2020-07-26 04:56:22 來源:網絡 閱讀:660 作者:1350368559 欄目:開發技術

1、http服務器

package main
import(
    "net/http"
    "log"
)
func main(){//注冊某個函數專門響應某個路由"/",函數簽名符合
    http.HandleFunc("/",func(w http.ResponseWriter,r *http.Request){ 
        w.Write([]byte("Hello,this is version 1!"))
    })
    http.HandleFunc("/bye",sayBye)
    log.Println("Starting server ... v1")
    log.Fatal(http.ListenAndServe(":4000",nil))
}
func sayBye( w http.ResponseWriter,r *http.Request){
    w.Write([]byte("Bye bye,this is version 1!"))
}
go run test.go
2018/07/09 14:32:08 Starting server ... v1

瀏覽器打開訪問:http://localhost:4000/
Go編程基礎4-Http服務器
瀏覽器打開訪問:http://localhost:4000/bye
Go編程基礎4-Http服務器

如何使用mux自定義ServeHTTP,實現http server呢?

package main
import(
    "net/http"
    "log"
)
func main(){
    mux := http.NewServeMux()
    mux.Handle("/",&myHandler{})
    mux.HandleFunc("bye",sayBye)

    log.Println("Starting server ... v2")
    log.Fatal(http.ListenAndServe(":4000",mux))
}
type myHandler struct {}
func (*myHandler) ServeHTTP(w http.ResponseWriter,r *http.Request){
    w.Write([]byte("Hello,this is version 2!"))
}
func sayBye(w http.ResponseWriter,r *http.Request){
    w.Write([]byte("Bye bye,this is version 2!"))
}
go run test.go
2018/07/09 15:37:35 Starting server ... v2

瀏覽器打開訪問:http://localhost:4000/
Go編程基礎4-Http服務器
瀏覽器打開訪問:http://localhost:4000/bye

瀏覽器打開訪問:http://localhost:4000/abcdef 默認也匹配到這里
Go編程基礎4-Http服務器

所有未匹配到的路由都會匹配到跟 "/" 路由

package main
import(
    "net/http"
    "log"
)
func main(){
    mux := http.NewServeMux()
    mux.Handle("/",&myHandler{}) //所有未匹配到的路由都會匹配到跟路由
    mux.HandleFunc("bye",sayBye)

    log.Println("Starting server ... v2")
    log.Fatal(http.ListenAndServe(":4000",mux))
}
type myHandler struct {}
func (*myHandler) ServeHTTP(w http.ResponseWriter,r *http.Request){
    w.Write([]byte("Hello v2,the request url is:"+ r.URL.String()))
}
func sayBye(w http.ResponseWriter,r *http.Request){
    w.Write([]byte("Bye bye,this is version 2!"))
}
go run test.go
2018/07/09 15:50:23 Starting server ... v2

瀏覽器打開訪問:http://localhost:4000/nihao 默認也匹配到/這里
Go編程基礎4-Http服務器

如何自定義server信息呢?

import(
    "net/http"
    "log"
    "time"
)
func main(){
    server :=&http.Server{
        Addr: ":4000",
        WriteTimeout:2 * time.Second,
    }
    mux := http.NewServeMux()
    mux.Handle("/",&myHandler{}) //所有未匹配到的路由都會匹配到跟路由
    mux.HandleFunc("bye",sayBye)
    server.Handler=mux

    log.Println("Starting server ... v2")
    log.Fatal(server.ListenAndServe)
}
type myHandler struct {}
func (*myHandler) ServeHTTP(w http.ResponseWriter,r *http.Request){
    w.Write([]byte("Hello v2,the request url is:"+ r.URL.String()))
}
func sayBye(w http.ResponseWriter,r *http.Request){
    w.Write([]byte("Bye bye,this is version 2!"))
}
go run test.go
2018/07/09 15:57:46 Starting server ... v2
2018/07/09 15:57:46 0x120ef70
瀏覽器打開訪問:http://localhost:4000 訪問正常

客戶端可以主動斷開

package main
import(
    "net/http"
    "log"
    "time"
    "os"
    "os/signal"
)
func main(){
    server :=&http.Server{
        Addr: ":4000",
        WriteTimeout:4 * time.Second,
    }

    quit := make(chan os.Signal)
    signal.Notify(quit,os.Interrupt)

    mux := http.NewServeMux()
    mux.Handle("/",&myHandler{}) //所有未匹配到的路由都會匹配到跟路由
    mux.HandleFunc("bye",sayBye)
    server.Handler=mux

    go func(){
        <-quit
        if err:=server.Close(); err != nil {
            log.Fatal("Close server:",err)
        }
    }()

    log.Println("Starting server ... v3")
    err := server.ListenAndServe()
    if err !=nil{
        if err == http.ErrServerClosed{
            log.Print("Server closed under request")
        }else{
            log.Fatal("Server closed unexpected")
        }
    }
    log.Println("Server exit")
}
type myHandler struct {}
func (*myHandler) ServeHTTP(w http.ResponseWriter,r *http.Request){
    w.Write([]byte("Hello v2,the request url is:"+ r.URL.String()))
}
func sayBye(w http.ResponseWriter,r *http.Request){
    time.Sleep(3*time.Second)
    w.Write([]byte("Bye bye,this is version 2!"))
}
2018/07/09 20:53:41 Starting server ... v3 
此時暫停程序,ctrl+c
2018/07/09 20:54:01 Server closed under request
2018/07/09 20:54:01 Server exit

如何實現不同的路由轉發呢?

package main

import (
    "log"
    "net/http"
    "time"
)

//創建一個map,進行路由的轉發
var mux map[string]func(http.ResponseWriter, *http.Request)

func main() {
    server := http.Server{
        Addr:        ":8090",
        Handler:     &myHandler{},
        ReadTimeout: 5 * time.Second,
    }
    //通過路由注冊,/hello 和 /bye 是不同的錄用,調用不同的func
    mux = make(map[string]func(http.ResponseWriter, *http.Request)) //分配點內存空間
    mux["/hello"] = sayHello
    mux["/bye"] = sayBye
    err := server.ListenAndServe()
    if err != nil {
        log.Fatal(err)
    }
}

type myHandler struct{}

func (*myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    if h, ok := mux[r.URL.String()]; ok { //map的妙用,判斷有沒有注冊路由,如果有,找到,ok=true
        h(w, r)
        return
    }
}
func sayHello(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello,this is version 3!"))
}
func sayBye(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Bye,this is version 4!"))
}

go run test.go
瀏覽器訪問:http://localhost:8090/hello
返回:Hello,this is version 3!
瀏覽器訪問:http://localhost:8090/bye
返回:Bye,this is version 4!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

双城市| 安图县| 揭东县| 红原县| 嵩明县| 汶上县| 林口县| 陈巴尔虎旗| 耿马| 米易县| 宁阳县| 安康市| 平邑县| 吕梁市| 揭西县| 得荣县| 左云县| 武宁县| 黄龙县| 香河县| 仙游县| 惠来县| 文昌市| 湟中县| 元谋县| 嘉峪关市| 乌恰县| 鹤庆县| 聂拉木县| 安龙县| 安仁县| 嘉定区| 容城县| 龙门县| 阿鲁科尔沁旗| 龙泉市| 右玉县| 蓬安县| 洪泽县| 长武县| 乌审旗|