精品专区-精品自拍9-精品自拍三级乱伦-精品自拍视频-精品自拍视频曝光-精品自拍小视频

網站建設資訊

NEWS

網站建設資訊

Golangnet/http知識的詳細介紹

本篇內容介紹了“Golang net/http知識的詳細介紹”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

10年積累的成都網站建設、做網站經驗,可以快速應對客戶對網站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網絡服務。我雖然不認識你,你也不認識我。但先網站設計后付款的網站建設流程,更有高臺免費網站建設讓你可以放心的選擇與我們合作。

以下是對net/http的學習。

Go 語言中處理 HTTP 請求主要跟兩個東西相關:ServeMux 和 Handler。 ServrMux 本質上是一個 HTTP 請求路由器(或者叫多路復用器,Multiplexor)。它把收到的請求與一組預先定義的 URL 路徑列表做對比,然后在匹配到路徑的時候調用關聯的處理器(Handler)。

處理器(Handler)負責輸出HTTP響應的頭和正文。任何滿足了http.Handler接口的對象都可作為一個處理器。通俗的說,對象只要有個如下簽名的ServeHTTP方法即可:

ServeHTTP(http.ResponseWriter, *http.Request)

Go 語言的 HTTP 包自帶了幾個函數用作常用處理器,比如FileServer,NotFoundHandler 和 RedirectHandler。我們從一個簡單具體的例子開始:

package main

import (
	"log"
	"net/http"
)

func main() {
	mux := http.NewServeMux()

	rh := http.RedirectHandler("http://www.baidu.com", 307)
	mux.Handle("/foo", rh)

	log.Println("Listening...")
	http.ListenAndServe(":3000", mux)
}

快速地過一下代碼:

在 main 函數中我們只用了 http.NewServeMux 函數來創建一個空的 ServeMux。 然后我們使用 http.RedirectHandler 函數創建了一個新的處理器,這個處理器會對收到的所有請求,都執行307重定向操作到 http://www.baidu.com。 接下來我們使用 ServeMux.Handle 函數將處理器注冊到新創建的 ServeMux,所以它在 URL 路徑/foo 上收到所有的請求都交給這個處理器。 最后我們創建了一個新的服務器,并通過 http.ListenAndServe 函數監聽所有進入的請求,通過傳遞剛才創建的 ServeMux來為請求去匹配對應處理器。

然后在瀏覽器中訪問 http://localhost:3000/foo 你應該能發現請求已經成功的重定向了。

明察秋毫的你應該能注意到一些有意思的事情:ListenAndServer 的函數簽名是 ListenAndServe(addr string, handler Handler) ,但是第二個參數我們傳遞的是個 ServeMux。

我們之所以能這么做,是因為 ServeMux 也有 ServeHTTP 方法,因此它也是個合法的 Handler。

對我來說,將 ServerMux 用作一個特殊的Handler是一種簡化。它不是自己輸出響應而是將請求傳遞給注冊到它的其他 Handler。這乍一聽起來不是什么明顯的飛躍 - 但在 Go 中將 Handler 鏈在一起是非常普遍的用法。

自定義處理器(Custom Handlers)

真正的重點是我們有一個對象(本例中就是個timerHandler結構體,但是也可以是一個字符串、一個函數或者任意的東西),我們在這個對象上實現了一個 ServeHTTP(http.ResponseWriter, *http.Request) 簽名的方法,這就是我們創建一個處理器所需的全部東西。

package main

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

type timeHandler struct {
	format string
}

func (th *timeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	tm := time.Now().Format(th.format)
	w.Write([]byte("The time is : " + tm))
}

func main() {
	mux := http.NewServeMux()

	rh := http.RedirectHandler("http://www.baidu.com", 307)
	th := &timeHandler{format: time.RFC1123}

	mux.Handle("/a", rh)
	mux.Handle("/time", th)

	log.Println("Listening.....")
	http.ListenAndServe(":3000", mux)

}

將函數作為處理器

http.HandlerFunc()能夠讓一個常規函數作為一個Handler接口條件

func timeHandler(w http.ResponseWriter, r *http.Request) {
	tm := time.Now().Format(time.RFC1123)
	w.Write([]byte("the time is : " + tm))
}

func main() {
	mux := http.NewServeMux()

	rh := http.RedirectHandler("http://www.baidu.com", 307)
	th := http.HandlerFunc(timeHandler)

	mux.Handle("/a", rh)
	mux.Handle("/time", th)

	log.Println("Listening.....")
	http.ListenAndServe(":3000", mux)

}

更為便捷的方式:ServerMux.HandlerFunc 方法

func main() {
	mux := http.NewServeMux()

	mux.HandleFunc("/time", timeHandler)

	log.Println("Listening...")
	http.ListenAndServe(":3000", mux)
}

如果方法中需要參數,則需要使用閉包,將變量帶入:

//demo1
 func timeHandler(format string) http.Handler{
	fn:=func(w http.ResponseWriter,r *http.Request){
		tm:=time.Now().Format(format)
		w.Write([]byte("The time is : "+tm))
	}

	 return http.HandlerFunc(fn)
 }
 //demo2
 func timeHandler(format string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		tm := time.Now().Format(format)
		w.Write([]byte("The time is: " + tm))
	})
}
//demo3
func timeHandler(format string) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		tm := time.Now().Format(format)
		w.Write([]byte("The time is: " + tm))
	}
}

更為便利的DefaultServeMux

net/http 包提供了一組快捷方式來配合 DefaultServeMux:http.Handle 和 http.HandleFunc。這些函數與我們之前看過的類似的名稱的函數功能一樣,唯一的不同是他們將處理器注冊到 DefaultServerMux ,而之前我們是注冊到自己創建的 ServeMux。

此外,ListenAndServe在沒有提供其他的處理器的情況下(也就是第二個參數設成了 nil),內部會使用 DefaultServeMux。

因此,作為最后一個步驟,我們使用 DefaultServeMux 來改寫我們的 timeHandler應用:

func timeHandler(format string) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		tm := time.Now().Format(format)
		w.Write([]byte("The time is : " + tm))
	}

	return http.HandlerFunc(fn)
}

func main() {
	/* mux := http.NewServeMux()

	rh := http.RedirectHandler("http://www.baidu.com", 307)
	th := timeHandler(time.RFC1123)

	mux.Handle("/a", rh)
	mux.Handle("/time", th)

	log.Println("Listening.....")
	http.ListenAndServe(":3000", mux)
	*/

	var format string = time.RFC1123

	th := timeHandler(format)

	http.Handle("/time", th)

	log.Println("listening....")

	http.ListenAndServe(":3000", nil)
}

“Golang net/http知識的詳細介紹”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注創新互聯網站,小編將為大家輸出更多高質量的實用文章!


當前文章:Golangnet/http知識的詳細介紹
標題網址:http://m.jcarcd.cn/article/gjjpdg.html
主站蜘蛛池模板: 国产精品都市激情 | 91丝袜国产欧美 | 欧美一级毛B片 | 国产高清| 欧美在线免费观看 | 日本xxxx色视 | 日韩BD | 九九国产| 91神马高| 国内精品在线观看看 | 日本不卡在线观看 | 国产一区二区精品 | 日韩特黄大片日 | 三区在线观看不卡 | 国产女主播一区 | 99久热精 | 国产在线九色 | 日韩插穴 | 欧美午夜片在线观看 | 精品蜜桃臀1区2区 | 国产大片免费观看 | 国产二区自拍 | 国产在线综合网站 | 日韩gv国产gv欧 | 国产高清午夜自 | 国产精品一区二区三 | 国产福利在线小视频 | 国产播放隔着超 | 国产在在线免 | 91精品91| 国产在线精 | 日韩精品欧美一区喷 | 国产精品美脚玉 | 三级网站国产 | 欧美日韩产 | 日韩一区国产一级 | 国产簧片在线观看 | 日韩a午| 日本三级观看 | 三区在线观看 | 国产精品九九 |