Go語(yǔ)言單控制器和多控制器使用詳解
本文實(shí)例為大家分享了Go語(yǔ)言單控制器和多控制器使用的具體代碼,供大家參考,具體內(nèi)容如下
一. 單控制器
- 在Golang的net/http包下有ServeMux實(shí)現(xiàn)了Front設(shè)計(jì)模式的Front窗口,ServeMux負(fù)責(zé)接收請(qǐng)求并把請(qǐng)求分發(fā)給處理器(Handler)
- http.ServeMux實(shí)現(xiàn)了Handler接口
type Handler interface { ?? ?ServeHTTP(ResponseWriter, *Request) } type ServeMux struct { ?? ?mu ? ?sync.RWMutex ?? ?m ? ? map[string]muxEntry ?? ?hosts bool // whether any patterns contain hostnames } func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) { ?? ?if r.RequestURI == "*" { ?? ??? ?if r.ProtoAtLeast(1, 1) { ?? ??? ??? ?w.Header().Set("Connection", "close") ?? ??? ?} ?? ??? ?w.WriteHeader(StatusBadRequest) ?? ??? ?return ?? ?} ?? ?h, _ := mux.Handler(r) ?? ?h.ServeHTTP(w, r) }
自定義結(jié)構(gòu)體,實(shí)現(xiàn)Handler接口后,這個(gè)結(jié)構(gòu)體就屬于一個(gè)處理器,可以處理全部請(qǐng)求
- 無(wú)論在瀏覽器中輸入的資源地址是什么,都可以訪問(wèn)ServeHTTP
package main import "fmt" import "net/http" type MyHandler struct { } func (mh *MyHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) { ? ?fmt.Fprintln(res,"輸出內(nèi)容") } func main() { ? ?myhandler := MyHandler{} ? ?server := http.Server{ ? ? ? Addr: ? ?"127.0.0.1:8090", ? ? ? Handler: &myhandler, ? ?} ? ?server.ListenAndServe() }
二.多控制器
在實(shí)際開(kāi)發(fā)中大部分情況是不應(yīng)該只有一個(gè)控制器的,不同的請(qǐng)求應(yīng)該交給不同的處理單元.在Golang中支持兩種多處理方式
- 多個(gè)處理器(Handler)
- 多個(gè)處理函數(shù)(HandleFunc)
使用多處理器
- 使用http.Handle把不同的URL綁定到不同的處理器
- 在瀏覽器中輸入http://localhost:8090/myhandler或http://localhost:8090/myother可以訪問(wèn)兩個(gè)處理器方法.但是其他URl會(huì)出現(xiàn)404(資源未找到)頁(yè)面
package main import "fmt" import "net/http" type MyHandler struct{} type MyOtherHandler struct{} func (mh *MyHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) { ? ?fmt.Fprintln(res, "第一個(gè)") } func (mh *MyOtherHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) { ? ?fmt.Fprintln(res, "第二個(gè)") } func main() { ? ?myhandler := MyHandler{} ? ?myother := MyOtherHandler{} ? ?server := http.Server{ ? ? ? Addr: "localhost:8090", ? ?} ? ?http.Handle("/myhandler", &myhandler) ? ?http.Handle("/myother", &myother) ? ?server.ListenAndServe() }
多函數(shù)方式要比多處理器方式簡(jiǎn)便.直接把資源路徑與函數(shù)綁定
package main import "fmt" import "net/http" //不需要定義結(jié)構(gòu)體 //函數(shù)的參數(shù)需要按照ServeHTTP函數(shù)參數(shù)列表進(jìn)行定義 func first(res http.ResponseWriter, req *http.Request) { ? ?fmt.Fprintln(res, "第一個(gè)") } func second(res http.ResponseWriter, req *http.Request) { ? ?fmt.Fprintln(res, "第二個(gè)") } func main() { ? ?server := http.Server{ ? ? ? Addr: "localhost:8090", ? ?} ? ?//注意此處使用HandleFunc函數(shù) ? ?http.HandleFunc("/first", first) ? ?http.HandleFunc("/second", second) ? ?server.ListenAndServe() }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解go語(yǔ)言判斷管道是否關(guān)閉的常見(jiàn)誤區(qū)
這篇文章主要想和大家一起探討一下在Go語(yǔ)言中,我們是否可以使用讀取管道時(shí)的第二個(gè)返回值來(lái)判斷管道是否關(guān)閉,文中的示例代碼講解詳細(xì),有興趣的可以了解下2023-10-10golang調(diào)用藍(lán)兔支付實(shí)現(xiàn)網(wǎng)上支付功能
支付寶、微信的網(wǎng)上支付需要營(yíng)業(yè)執(zhí)照個(gè)人無(wú)法直接使用,如果個(gè)人需要實(shí)現(xiàn)網(wǎng)上支付功能,目前大部分應(yīng)該是都是依賴第三方聚合支付來(lái)實(shí)現(xiàn),本文就來(lái)介紹一下如何調(diào)用藍(lán)兔支付實(shí)現(xiàn)網(wǎng)上支付功能,有需要的可以參考下2023-09-09golang抓取網(wǎng)頁(yè)并分析頁(yè)面包含的鏈接方法
今天小編就為大家分享一篇golang抓取網(wǎng)頁(yè)并分析頁(yè)面包含的鏈接方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08Go中defer使用場(chǎng)景及注意事項(xiàng)
defer 會(huì)在當(dāng)前函數(shù)返回前執(zhí)行傳入的函數(shù),它會(huì)經(jīng)常被用于關(guān)閉文件描述符、關(guān)閉數(shù)據(jù)庫(kù)連接以及解鎖資源。這篇文章主要介紹了Go中defer使用注意事項(xiàng),需要的朋友可以參考下2021-12-12