我的第一个stackoverflow问题,所以请轻松我的天真有关stackoverflow和问题,初学者在golang。
我想知道两个调用之间的区别,以及对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") }http.Handle(“/ profile”,Logger(profilefunc)) http.HandleFunc(“/”,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") }http.Handle("/profile", Logger(profilefunc)) http.HandleFunc("/", HomeFunc)
最满意答案
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 。我想...简单理解Handle,Handler,HandleFunc,HandlerFunc。
http.Handle("/profile", Logger(profilefunc)) http.HandleFunc("/", HomeFunc)我想知道这两个电话之间的区别
Logger是一个中间件的例子,它是一个带有http.Handler的函数,它返回另一个包装原始处理程序的http.Handler 。 在调用时,此处理程序可能(或可能不)在执行某些操作之前和/或之后调用嵌套的http.Handler 。 所以第一行是说使用模式“/ profile”注册包含在Logger中间件中的profileFunc Handler 。 第二行是说使用“/”模式注册HomeFunc函数。
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 like ... simple understanding of the Handle, Handler, HandleFunc, HandlerFunc.
http.Handle("/profile", Logger(profilefunc)) http.HandleFunc("/", HomeFunc)I would to know the difference between the two calls
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.
更多推荐
发布评论