1. router in golang
1). sample code
1 package main 2 3 import (4 "fmt" 5 "net/http" 6) 7 8 func main () {9 10 http. handleFunc ("/handlefunc", func (w http. responseWriter, r * http. request) {11 fmt. fprintf (w, "http. handleFunc ") 12}) 13 14 http. handle ("/handle", & myHandler {}) 15 16 http. listenAndServe (": 8080", nil) 17} 18 19 type myHandler struct {} 20 21 func (this * myHandler) ServeHTTP (w http. responseWriter, r * http. request) {22 fmt. fprintf (w, "http. handle ") 23}
2). implement
A) top layer:
// Objects implementing the Handler interface can be // registered to serve a particle path or subtree // in the HTTP server. //// ServeHTTP shocould write reply headers and data to the ResponseWriter // and then return. returning signals that the request is finished // and that the HTTP server can move on to the next request on // the connection. type Handler interface {ServeHTTP (ResponseWriter, * Request )}
// The HandlerFunc type is an adapter to allow the use of // ordinary functions as HTTP handlers. if f is a function // with the appropriate signature, HandlerFunc (f) is a // Handler object that callf. type HandlerFunc func (ResponseWriter, * Request) // ServeHTTP CILS f (w, r ). func (f HandlerFunc) ServeHTTP (w ResponseWriter, r * Request) {f (w, r)
// Handle registers the handler for the given pattern // in the DefaultServeMux. // The documentation for ServeMux explains how patterns are matched. func Handle (pattern string, handler Handler) {defaservservemux. handle (pattern, handler)} // HandleFunc registers the handler function for the given pattern // in the DefaultServeMux. // The documentation for ServeMux explains how patterns are matched. func HandleFunc (pattern string, handler func (ResponseWriter, * Request) {defaservservemux. handleFunc (pattern, handler )}
Now we know that both of http. HandleFunc and http. Handle invoke function of defaservservemux.
B) DefaultServeMux
// NewServeMux allocates and returns a new ServeMux. func NewServeMux () * ServeMux {return & ServeMux {m: make (map [string] muxEntry)} // DefaultServeMux is the default ServeMux used by Serve. var defaservservemux = NewServeMux ()
// ServeMux is an HTTP request multiplexer. // It matches the URL of each incoming request against a list of registered // patterns and callthe handler for the pattern that // most closely matches the URL. // type ServeMux struct {mu sync. RWMutex m map [string] muxEntry hosts bool // whether any patterns contain hostnames} type muxEntry struct {explicit bool h Handler pattern string}
// HandleFunc registers the handler function for the given pattern. func (mux * ServeMux) HandleFunc (pattern string, handler func (ResponseWriter, * Request) {mux. handle (pattern, HandlerFunc (handler ))}
// Handle registers the handler for the given pattern. // If a handler already exists for pattern, Handle panics. func (mux * ServeMux) Handle (pattern string, handler Handler) {mux. mu. lock () defer mux. mu. unlock () if pattern = "" {panic ("http: invalid pattern" + pattern)} if handler = nil {panic ("http: nil handler ")} if mux. m [pattern]. explicit {panic ("http: multiple registrations for" + patte Rn)} mux. m [pattern] = muxEntry {explicit: true, h: handler, pattern: pattern} if pattern [0]! = '/' {Mux. hosts = true} // Helpful behavior: // If pattern is/tree/, insert an implicit permanent redirect for/tree. // It can be overridden by an explicit registration. n: = len (pattern) if n> 0 & pattern [n-1] = '/'&&! Mux. m [pattern [0: n-1]. explicit {// If pattern contains a host name, strip it and use remaining // path for redirect. path: = pattern if pattern [0]! = '/' {// In pattern, at least the last character is A'/', so // strings. index can't be-1. path = pattern [strings. index (pattern, "/"):]} mux. m [pattern [0: n-1] = muxEntry {h: RedirectHandler (path, StatusMovedPermanently), pattern: pattern }}}
2. rounter in beego
Router