本文整理匯總了Golang中http.Handler類的典型用法代碼示例。如果您正苦於以下問題:Golang Handler類的具體用法?Golang Handler怎麽用?Golang Handler使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Handler類的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Serve
// Serve executes the provided Handler on the currently active CGI
// request, if any. If there's no current CGI environment
// an error is returned. The provided handler may be nil to use
// http.DefaultServeMux.
func Serve(handler http.Handler) os.Error {
req, err := Request()
if err != nil {
return err
}
if handler == nil {
handler = http.DefaultServeMux
}
rw := &response{
req: req,
header: make(http.Header),
bufw: bufio.NewWriter(os.Stdout),
}
handler.ServeHTTP(rw, req)
if err = rw.bufw.Flush(); err != nil {
return err
}
return nil
}
示例2: ServeHTTP
// ServeHTTP dispatches the handler registered in the matched route.
//
// When there is a match, the route variables can be retrieved calling
// mux.Vars(request).
func (r *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
// Clean path to canonical form and redirect.
// (this comes from the http package)
if p := cleanPath(request.URL.Path); p != request.URL.Path {
writer.Header().Set("Location", p)
writer.WriteHeader(http.StatusMovedPermanently)
return
}
var handler http.Handler
if match, ok := r.Match(request); ok {
handler = match.Handler
}
if handler == nil {
if r.NotFoundHandler == nil {
r.NotFoundHandler = http.NotFoundHandler()
}
handler = r.NotFoundHandler
}
defer context.DefaultContext.Clear(request)
handler.ServeHTTP(writer, request)
}
示例3: loggingHandler
func loggingHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
log.Printf("%s\t%s", req.RemoteAddr, req.URL)
h.ServeHTTP(w, req)
})
}
示例4: loggingHandler
func loggingHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(c *http.Conn, req *http.Request) {
log.Stderrf("%s\t%s", c.RemoteAddr, req.Url);
h.ServeHTTP(c, req);
});
}