Go-restful 是一个优秀且易于使用的 Go RESTful 风格框架,但我想知道的是(这只是一段代码):
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)
}
这段代码可以很好地工作。请注意最后一行 http.ListenAndServe(":8080", nil)
,它没有将任何处理程序传递给 ListenAndServe 方法(而是传递了一个 nil 值),只是端口字符串。有谁知道 go-restful 是如何实现处理程序绑定(bind)的?
最佳答案
将 nil
作为处理程序传递是 Go 中的常见做法。作为specs say ,
Handler is typically nil, in which case the DefaultServeMux is used.
DefaultServeMux
只是 http
中的一个方便的全局变量。所以,好好休息吧uses it默认情况下。
关于rest - 如何在 go-restful 中绑定(bind)处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43039131/