網(wǎng)站首頁(yè) 編程語(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í)際開發(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() }
原文鏈接:https://blog.csdn.net/qq_27870421/article/details/118494202
相關(guān)推薦
- 2023-11-25 優(yōu)化計(jì)算屬性mapState、mapGetters和methods的mapActions、mapMu
- 2022-10-18 Qt基于TCP實(shí)現(xiàn)客戶端與服務(wù)端的連接_C 語(yǔ)言
- 2022-08-10 pandas.DataFrame.from_dict直接從字典構(gòu)建DataFrame的方法_pyth
- 2022-06-06 webpack5.6.0解決報(bào)The ‘mode‘ option has not been set,
- 2022-11-16 kali添加開機(jī)自啟的方法_相關(guān)技巧
- 2022-08-03 C#?代碼大小寫規(guī)范說(shuō)明_C#教程
- 2022-06-02 Python學(xué)習(xí)之隨機(jī)模塊random詳解_python
- 2022-06-21 C語(yǔ)言程序的編譯與預(yù)處理基礎(chǔ)定義講解_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支