如何在go-restful中绑定处理程序?

如何在go-restful中绑定处理程序?

问题描述:

Go-restful is a good and easy to use Go RESTful style framework, but here is something i am wondering about (this is just a piece of code):


func main() {
    service := new(restful.WebService)

    service.Route(
    service.GET("/{user-id}").To(FindUser).
    Returns(200, "hello", noMessageValue).
    Returns(500, "internal error", noMessageValue))
    restful.Add(service)
    http.ListenAndServe(":8080", nil)
} 

This code can work well. Notice the last line http.ListenAndServe(":8080", nil), it does not pass any handler to the ListenAndServe method (it passes a nil value instead), just the port string. Does anyone know how go-restful implements handler binding ?

Go-restful是一个很好且易于使用的Go RESTful样式框架,但这是我想知道的( 这只是一段代码): p>


  func main(){
 service:= new(restful.WebService)
 
  service.Route(
 service.GET(“ / {user-id}”)。To(FindUser)。
返回(200,“ hello”,noMessageValue)。
返回(500,“内部错误”,noMessageValue)  ))
 restful.Add(service)
 http.ListenAndServe(“:8080”,nil)
} 
  code>  pre> 
 
 

此代码可以正常工作。 注意最后一行 http.ListenAndServe(“:8080”,nil) code>,它没有将任何处理程序传递给ListenAndServe方法(而是传递了nil值),而只是传递了端口字符串。 有人知道go-restful如何实现处理程序绑定吗? p> div>

Passing nil as a handler is a common practice in Go. As the specs say,

Handler is typically nil, in which case the DefaultServeMux is used.

DefaultServeMux is just a convenience global variable in http. So, go-restful just uses it by default.