英文:
What is an http request multiplexer?
问题
我一直在学习Golang,我注意到很多人使用http.NewServeMux()
函数来创建服务器,但我不太理解它的作用。
我读到这样的解释:
在Go中,ServeMux是一个HTTP请求多路复用器。它将每个传入请求的URL与注册的模式列表进行匹配,并调用与URL最匹配的模式的处理程序。
那么,这与只做以下操作有什么不同呢?
http.ListenAndServe(addr, nil)
http.Handle("/home", home)
http.Handle("/login", login)
使用多路复用器的目的是什么?
英文:
I've been studying golang and I noticed a lot of people create servers by using the http.NewServeMux()
function and I don't really understand what it does.
I read this:
> In go ServeMux is an HTTP request multiplexer. It matches the URL of
> each incoming request against a list of registered patterns and calls
> the handler for the pattern that most closely matches the URL.
How is that different than just doing something like:
http.ListenAndServe(addr, nil)
http.Handle("/home", home)
http.Handle("/login", login)
What is the purpose of using multiplexing?
答案1
得分: 27
从net/http
的GoDoc和源代码中。
ListenAndServe
函数用于启动一个带有指定地址和处理程序的HTTP服务器。处理程序通常为nil,这意味着使用DefaultServeMux。Handle和HandleFunc函数用于向DefaultServeMux添加处理程序。
DefaultServeMux
只是预定义的http.ServeMux
。
var DefaultServeMux = &defaultServeMux
var defaultServeMux ServeMux
正如你所看到的,http.Handle
在内部调用了DefaultServeMux
。
func Handle(pattern string, handler Handler) { DefaultServeMux.Handle(pattern, handler) }
http.NewServeMux()
的目的是创建自己的http.ServeMux
实例,用于在需要两个不同端口和不同路由的http.ListenAndServe
函数实例时使用。
英文:
From net/http
GoDoc and Source.
ListenAndServe starts an HTTP server with a given address and handler. The handler is usually nil, which means to use DefaultServeMux. Handle and HandleFunc add handlers to DefaultServeMux
DefaultServeMux
is just a predefined http.ServeMux
var DefaultServeMux = &defaultServeMux
var defaultServeMux ServeMux
As you can see http.Handle
calls DefaultServeMux
internally.
func Handle(pattern string, handler Handler) { DefaultServeMux.Handle(pattern, handler) }
The purpose of http.NewServeMux()
is to have your own instance of http.ServerMux
for instances like when you require two http.ListenAndServe
functions listening to different ports with different routes.
答案2
得分: 7
在Golang中,多路复用器(multiplexer)类似于硬件中的多路复用器,它将多个输入转发到多个输出。
我给你一个简单的例子:
type CustomMultiplexer struct {
}
给定的多路复用器必须实现ServeHTTP
方法,以便在HTTP服务器中注册输入。
func (mux CustomMultiplexer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
SimpleRequestHandler(w, r)
return
}
http.NotFound(w, r)
return
}
我的SimpleRequestHandler
是一个如下所示的方法:
func SimpleRequestHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
mySimpleGetRequestHandler(w, r)
break
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
break
}
}
现在我可以使用我的CustomMultiplexer
在传入请求之间进行多路复用。
func main() {
customServer := CustomServer{}
err := http.ListenAndServe(":9001", &customServer)
if err != nil {
panic(err)
}
}
http.HandleFunc
方法可以作为我给出的简单多路复用器的工作方式。
英文:
the multiplexer in Golang is some things like multiplexer in hardware which multiply some inputs into some outputs
i gave you a simple exampe
type CustomMultiplexer struct {
}
the given multiplexer have to implement the ServeHTTP
method to be registered int http to server inputs
func (mux CustomMultiplexer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
SimpleRequestHandler(w, r)
return
}
http.NotFound(w, r)
return
}
my SimpleRequestHandler
is a method as follow
func SimpleRequestHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
mySimpleGetRequestHandler(w, r)
break
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
break
}
}
now i can use my CustomMultiplxere
to do multiplexing between incoming requested
func main() {
customServer := CustomServer{}
err := http.ListenAndServe(":9001", &customServer)
if err != nil {
panic(err)
}
}
the http.HandleFunc
method works as my given simple multiplexer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论