Golang中间件只有标准库(Golang middleware with just the standard library)

编程入门 行业动态 更新时间:2024-10-26 08:27:32
Golang中间件只有标准库(Golang middleware with just the standard library)

我的第一个stackoverflow问题,所以请轻松我的天真有关stackoverflow和问题,初学者在golang。

我想知道两个调用之间的区别,以及对Handle , Handler , HandleFunc , HandlerFunc简单理解。

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

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") }

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.

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

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") }

最满意答案

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

Handler是一个可以响应HTTP请求并具有ServeHTTP(ResponseWriter, *Request)方法的接口。 http.Handle注册一个Handler来处理与给定pattern匹配的HTTP请求。 http.HandleFunc注册一个处理函数来处理与给定pattern匹配的HTTP请求。 处理函数应该是func(ResponseWriter, *Request) 。 HandlerFunc是func(ResponseWriter, *Request)形式的显式函数类型func(ResponseWriter, *Request) 。 HandlerFunc有一个调用自身的方法ServeHTTP 。 这允许您将函数HandlerFunc转换为HandlerFunc并将其用作Handler 。

我想知道这两个电话之间的区别

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

Logger是一个中间件的例子,它是一个带有http.Handler的函数,它返回另一个包装原始处理程序的http.Handler 。 在调用时,此处理程序可能(或可能不)在执行某些操作之前和/或之后调用嵌套的http.Handler 。 所以第一行是说使用模式“/ profile”注册包含在Logger中间件中的profileFunc Handler 。 第二行是说使用“/”模式注册HomeFunc函数。

I would like ... simple understanding of the Handle, Handler, HandleFunc, HandlerFunc.

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.

I would to know the difference between the two calls

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.

更多推荐

本文发布于:2023-04-29 05:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335263.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中间件   标准   Golang   library   standard

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!