亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Golang?HTTP編程的源碼解析詳解

 更新時間:2023年02月21日 08:47:37   作者:Amos01  
這篇文章主要為大家詳細(xì)介紹了Golang中的HTTP編程以及源碼解析,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,感興趣的可以了解一下

1、網(wǎng)絡(luò)基礎(chǔ)

基本TCP客戶-服務(wù)器程序Socket編程流程如如下圖所示。

TCP服務(wù)器綁定到特定端口并阻塞監(jiān)聽客戶端端連接,

TCP客戶端則通過IP+端口向服務(wù)器發(fā)起請求,客戶-服務(wù)器建立連接之后就能開始進(jìn)行數(shù)據(jù)傳輸。

Golang的TCP編程也是基于上述流程的。

2、Golang HTTP編程

2.1 代碼示例

func timeHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "%v", time.Now().Format(time.RFC3339))
}
 
func helloHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "%v", "hello world.")
}
 
func main() {
    // 1. 新建路由解碼器
    h := http.NewServeMux()
    // 2. 路由注冊
    h.HandleFunc("/hello", helloHandler)
    h.HandleFunc("/time", timeHandler)
    // 3. 服務(wù)啟動 阻塞監(jiān)聽
    http.ListenAndServe(":8000", h)
}

運(yùn)行上述程序,在瀏覽器地址欄分別輸入 http://localhost:8000/hello http://localhost:8000/time 結(jié)果分別如下圖所示。

2.2 源碼分析

分析從路由注冊到響應(yīng)用戶請求的流程。

2.2.1 新建解碼器 h := http.NewServeMux()

type ServeMux struct {
    mu    sync.RWMutex
    m     map[string]muxEntry
    es    []muxEntry // slice of entries sorted from longest to shortest.
    hosts bool       // whether any patterns contain hostnames
}
type muxEntry struct {
    h       Handler
    pattern string
}
// NewServeMux allocates and returns a new ServeMux.
func NewServeMux() *ServeMux { return new(ServeMux) }

Handler是interface,定義如下

type Handler interface {
    ServeHTTP(ResponseWriter, *Request)
}

ServeMux實(shí)現(xiàn)了Handler接口。

2.2.2 路由注冊 h.HandleFunc("/hello", helloHandler)

// HandleFunc registers the handler function for the given pattern.
func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
    ...
    mux.Handle(pattern, HandlerFunc(handler))
}
 
func (mux *ServeMux) Handle(pattern string, handler Handler) {
    ...
    e := muxEntry{h: handler, pattern: pattern}
    mux.m[pattern] = e
    if pattern[len(pattern)-1] == '/' {
        mux.es = appendSorted(mux.es, e)
    }
    ...
}

timeHandler和helloHandler函數(shù)被強(qiáng)制轉(zhuǎn)換為type HandlerFunc func(ResponseWriter, *Request)類型,且實(shí)現(xiàn)了Handler接口。

func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
    f(w, r)
}

mux.m建立了路由到處理函數(shù)timeHandler和helloHandler的映射。

2.2.3 服務(wù)啟動阻塞監(jiān)聽 http.ListenAndServe(":8000", h)

包裝Server結(jié)構(gòu)體,HTTP使用TCP協(xié)議。

func ListenAndServe(addr string, handler Handler) error {
    server := &Server{Addr: addr, Handler: handler}
    return server.ListenAndServe()
}
func (srv *Server) ListenAndServe() error {
    ...
    ln, err := net.Listen("tcp", addr)
    if err != nil {
        return err
    }
    return srv.Serve(ln)
}

net.Listen封裝了Socket編程的socket,bind,listen的調(diào)用,極大的方便了使用者。

阻塞監(jiān)聽請求,新建goroutine處理每個新請求。

func (srv *Server) Serve(l net.Listener) error {
    ...
    for {
        rw, err := l.Accept()
        ...
        c := srv.newConn(rw)
        c.setState(c.rwc, StateNew, runHooks) // before Serve can return
        go c.serve(connCtx)
    }
}
// Serve a new connection.
func (c *conn) serve(ctx context.Context) {
    ...
    serverHandler{c.server}.ServeHTTP(w, w.req)
    ...
}
func (sh serverHandler) ServeHTTP(rw ResponseWriter, req *Request) {
    handler := sh.srv.Handler
    ...
    handler.ServeHTTP(rw, req)
}

通過前面的流程推導(dǎo)可知,handler是http.ListenAndServe的第二個參數(shù)ServeMux

// ServeHTTP dispatches the request to the handler whose
// pattern most closely matches the request URL.
func (mux *ServeMux) ServeHTTP(w ResponseWriter, r *Request) {
    ...
    h, _ := mux.Handler(r) // 通過路由獲取處理函數(shù)
    h.ServeHTTP(w, r)
}

mux.Handler使用mux.m這個map通過請求URL找到對應(yīng)處理函數(shù)的。

h的實(shí)際類型為HandlerFunc,根據(jù)2.2.2會調(diào)用到具體函數(shù)timeHandler或者h(yuǎn)elloHandler。

3. 總結(jié)

golang對socket編程進(jìn)行了封裝,給HTTP編程帶來了極大的便利。

但是不支持以下特性

1. 路由分組 對路由進(jìn)行分組,可以方便分組鑒權(quán)

2. 動態(tài)路由 如動態(tài)路由/user/:username/post/:postid不支持

以上就是Golang HTTP編程的源碼解析詳解的詳細(xì)內(nèi)容,更多關(guān)于Golang HTTP編程的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Go語言編程中對文件讀寫的基本方法整理

    Go語言編程中對文件讀寫的基本方法整理

    這篇文章主要介紹了Go語言編程中對文件讀寫的基本方法整理,是Go語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-10-10
  • Golang高效解析和生成XML的示例詳解

    Golang高效解析和生成XML的示例詳解

    這篇文章將從Golang中處理XML的基本概念開始,詳細(xì)介紹如何讀取和解析XML文件,然后轉(zhuǎn)向如何創(chuàng)建和輸出XML數(shù)據(jù),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • Go映射的使用

    Go映射的使用

    Go提供了另一個重要的數(shù)據(jù)類型,稱為map,它將唯一鍵映射到值,本文主要介紹了Go映射的使用,包括聲明映射、初始化映射、操作映射等,感興趣的可以了解一下
    2023-11-11
  • Go網(wǎng)絡(luò)編程TCP抓包實(shí)操示例探究

    Go網(wǎng)絡(luò)編程TCP抓包實(shí)操示例探究

    作為一名軟件開發(fā)者,網(wǎng)絡(luò)編程是必備知識,本文通過?Go?語言實(shí)現(xiàn)?TCP?套接字編程,并結(jié)合?tcpdump?工具,展示它的三次握手、數(shù)據(jù)傳輸以及四次揮手的過程,幫助讀者更好地理解?TCP?協(xié)議與?Go?網(wǎng)絡(luò)編程
    2024-01-01
  • 詳解Golang中Channel的高級用法

    詳解Golang中Channel的高級用法

    在Go語言中,chan(通道)是一種用于在不同的goroutine之間進(jìn)行通信的機(jī)制,通道可以是無緩沖的(同步的)或有緩沖的(異步的),本文給大家詳細(xì)介紹了Golang中Channel的高級用法,需要的朋友可以參考下
    2024-05-05
  • go語言中的template使用示例詳解

    go語言中的template使用示例詳解

    在Go語言中,可以通過text/template和html/template包來處理模板,本文提供了一個使用Go模板的基本示例,包括導(dǎo)入包、創(chuàng)建數(shù)據(jù)結(jié)構(gòu)、定義模板、執(zhí)行模板及運(yùn)行程序,通過這些步驟,可以輸出一個格式化的YAML配置
    2024-10-10
  • Go語言實(shí)現(xiàn)JSON解析的方法詳解

    Go語言實(shí)現(xiàn)JSON解析的方法詳解

    在日常項(xiàng)目中,使用Json格式進(jìn)行數(shù)據(jù)封裝是比較常見的操作。本文將詳細(xì)講解如何利用Go語言實(shí)現(xiàn)JSON的解析,感興趣的小伙伴可以學(xué)習(xí)一下
    2022-04-04
  • 搭建Go語言的ORM框架Gorm的具體步驟(從Java到go)

    搭建Go語言的ORM框架Gorm的具體步驟(從Java到go)

    很多朋友不知道如何使用Goland軟件,搭建一個ORM框架GORM,今天小編給大家分享一篇教程關(guān)于搭建Go語言的ORM框架Gorm的具體步驟(從Java到go),感興趣的朋友跟隨小編一起學(xué)習(xí)下吧
    2022-09-09
  • GO使用Mutex確保并發(fā)程序正確性詳解

    GO使用Mutex確保并發(fā)程序正確性詳解

    這篇文章主要為大家介紹了GO使用Mutex確保并發(fā)程序正確性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • 解決golang post文件時Content-Type出現(xiàn)的問題

    解決golang post文件時Content-Type出現(xiàn)的問題

    這篇文章主要介紹了解決golang post文件時Content-Type出現(xiàn)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05

最新評論