本文介绍了只有标准库的Golang中间件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个stackoverflow问题,所以请容易对我的天真关于stackoverflow和问题,初学者在golang。



我想知道区别两个调用并简单理解 Handle Handler HandleFunc HandlerFunc



  fmt.Println(Starting the server。)
$ b $ profilefunc:= http.HandlerFunc(ProfileFunc)

http。 Handle(/ profile,Logger(profilefunc))
http.HandleFunc(/,HomeFunc)

http.ListenAndServe(0.0.0.0:8081,零)


func Logger(h http.Handler)http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter,r * http.Request){
log.Println(送达请求之前)
h.Se. rveHTTP(w,r)
log.Println(服务请求后)
})
}

func ProfileFunc(w http.ResponseWriter,r * http .Request){
fmt.Fprint(w,你在配置文件页面上。)
}

func HomeFunc(w http.ResponseWriter,r * http.Request ){
fmt.Fprintf(w,Hello Imran Pochi)
}


我想...简单理解Handle,Handler,HandleFunc,HandlerFunc。





  • Handler 是一个接口,可以响应HTTP请求并具有 ServeHTTP(ResponseWriter,* Request)方法。
  • http.Handle 寄存器 Handler 来处理匹配给定的模式

  • http的HTTP请求。 HandleFunc 注册一个处理函数来处理匹配给定模式的HTTP请求。处理函数应该是 func(ResponseWriter,* Request)

  • HandlerFunc 是一个形式为 func(ResponseWriter,* Request)的显式函数类型。 HandlerFunc 有一个调用自己的方法 ServeHTTP 。这允许您将一个函数转换为 HandlerFunc ,并将它用作 Handler





  •   http.Handle(/ profile,Logger(profilefunc))
    http.HandleFunc(/,HomeFunc)

    Logger 是,它是一个函数,它需要一个 http.Handler ,并返回包含原始处理程序的另一个 http.Handler 。当调用这个处理程序可能(或不可能)在执行某些操作之前和/或之后调用嵌套的 http.Handler 。因此,第一行是说注册包含在 Logger profileFunc Handler $ c>具有模式/ profile的中间件。第二行是使用/模式注册 HomeFunc 函数。


    My first stackoverflow question, so please go easy on my naivety about stackoverflow and the question asked, beginner in golang.

    I would like to know the difference between the two calls and also simple understanding of the Handle, Handler, HandleFunc, HandlerFunc.

    func main() {            
       fmt.Println("Starting the server.")
    
       profilefunc := http.HandlerFunc(ProfileFunc)
    
       http.Handle("/profile", Logger(profilefunc))
       http.HandleFunc("/", HomeFunc)
    
       http.ListenAndServe("0.0.0.0:8081", nil)
    }
    
    func Logger(h http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
            log.Println("Before serving request")
            h.ServeHTTP(w, r)
            log.Println("After serving request")
        })
    }
    
    func ProfileFunc(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "You are on the profile page.")
    }
    
    func HomeFunc(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello Imran Pochi")
    }
    
    解决方案

    • Handler is an interface that can respond to an HTTP request and has a ServeHTTP(ResponseWriter, *Request) method.
    • http.Handle registers a Handler to handle HTTP requests matching a given pattern.
    • http.HandleFunc registers a handler function to handle HTTP requests matching a given pattern. The handler function should be of the form func(ResponseWriter, *Request).
    • HandlerFunc is an explicit function type of the form func(ResponseWriter, *Request). HandlerFunc has a method ServeHTTP that calls itself . This allows you to cast a function to a HandlerFunc and use it as a Handler.

    http.Handle("/profile", Logger(profilefunc))
    http.HandleFunc("/", HomeFunc)
    

    Logger is an example of a middleware, which is a function that takes an http.Handler and it returns another http.Handler that wraps the original handler. When called this handler may (or may not) call the nested http.Handler before and/or after performing some operation. So the first line is saying register the profileFunc Handler wrapped in the Logger middleware with the pattern "/profile". The second line is saying register the HomeFunc function with the "/" pattern.

    这篇关于只有标准库的Golang中间件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    10-21 03:06