`http`和`default servemux`之间的区别是什么?

huangapple go评论76阅读模式
英文:

Difference between http and default servemux?

问题

这两段代码的区别在于使用的路由器不同。

第一段代码使用了默认的路由器 http.HandleFunc,它会将请求的路径与注册的处理函数进行匹配,并调用匹配到的处理函数来处理请求。

第二段代码使用了自定义的路由器 http.NewServeMux(),它允许我们手动创建一个路由器对象,并使用 mux.HandleFunc 来注册处理函数。然后,我们将这个自定义的路由器对象传递给 http.ListenAndServe 方法,以便使用该路由器来处理请求。

总的来说,第二段代码使用了更灵活的路由器,可以更好地控制请求的处理方式。

英文:

What's the difference between this:

func main() {

  http.HandleFunc("/page2", Page2)
  http.HandleFunc("/", Index)
  http.ListenAndServe(":3000", nil)
}

And using the golang serve mux

func main() {
  mux := http.NewServeMux()

  mux.HandleFunc("/page2", Page2)
  mux.HandleFunc("/", Index)
  http.ListenAndServe(":3000", mux)
}

答案1

得分: 51

第一个程序使用了默认的服务多路复用器。它与更冗长的代码完全相同:

func main() {
  http.DefaultServeMux.HandleFunc("/page2", Page2)
  http.DefaultServeMux.HandleFunc("/", Index)
  http.ListenAndServe(":3000", http.DefaultServeMux)
}

这两个程序之间有一个重要的区别:第一个程序对程序中使用的处理器没有完全的控制权。有一些包会在init()函数中自动向默认的服务多路复用器注册(示例)。如果程序直接或间接地导入了其中一个这样的包,这些处理器注册的处理器将在第一个程序中生效。

第二个程序对服务器使用的处理器具有完全的控制权。任何注册到默认的服务多路复用器的处理器都会被忽略。

英文:

The first program uses the default serve mux. It's identical to the more verbose:

func main() {
  http.DefaultServeMux.HandleFunc("/page2", Page2)
  http.DefaultServeMux.HandleFunc("/", Index)
  http.ListenAndServe(":3000", http.DefaultServeMux)
}

There's one important difference between the two programs: The first program does not have complete control over the handlers used in the program. There are packages that automatically register with the default serve mux from init() functions (example). If the program imports one of these packages directly or indirectly, the handlers registered by these handlers will be active in the first program.

The second program has complete control over the handlers used with the server. Any handlers registered with the default serve mux are ignored.

答案2

得分: 5

默认的 mux(多路复用器)的定义如下:

var DefaultServeMux = NewServeMux()

所以实际上没有什么主要的区别,除非你想进一步自定义并需要一个显式的 mux(例如出于某种原因将它们链接在一起)。

但是由于默认的 mux 已经被分配了,所以没有必要无缘无故地创建另一个。

英文:

Default mux is defined like:

var DefaultServeMux = NewServeMux()

So there's actually no major difference, unless you want to customize further and need an explicit mux for that (for example chain them for some reason).

But since the default is already allocated there's no need to create another one for no reason.

答案3

得分: 4

ServerMux是一种实现了Handler接口的类型,所有的服务器都有一个。在你的第一个例子中,服务器只是使用了默认的处理程序。我认为这里没有什么区别,因为NewServeMux返回的mux与默认的mux是相同的。它的存在是为了让你可以进一步自定义请求处理。

英文:

ServerMux is a type which implements the Handler interface, all servers have one. In your first example the server just uses the default handler. I don't think there are differences here because the mux returned by NewServeMux is going to be the same as the default. It is made available so that you can further customize request handling.

huangapple
  • 本文由 发表于 2016年4月29日 01:07:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/36921190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定