This is a creation in Article, where the information may have evolved or changed.
Package Mainimport ("io" "net/http" "Log")//Hello World, the Web serverFunc HelloServer (w http. Responsewriter, req *http. Request) {io. WriteString (W,"Hello, world!\n.")}func Main () {http. Handlefunc ("/hello", HelloServer) Err:= http. Listenandserve (": 12345", nil)ifErr! =Nil {log. Fatal ("Listenandserve:", Err)}}
Above is a simple go HTTP server-side program. Affected by this code, when looking at the Martini code, has been trying to find the call HTTP. Handlefunc code, has not been found.
http. Listenandserve's prototype:
Func listenandserve (addr string, handler handler) error
In the example above, handler passes nil. Look at the handler prototype again:
Type Handler Interface { servehttp (Responsewriter, *request)}
And Martini is the realization of the handler Object!
// Servehttp is the HTTP Entry point for a Martini instance. Useful if you want to control your own HTTP server. Func (M *martini) servehttp (res http. Responsewriter, req *http. Request) { M.createcontext (res, req). Run ()}
Look again in the Go language programming about Listenandserve Introduction:
This method is used to listen on the specified TCP network address addr and then call the server-side handler to process the incoming connection
The request is received. The method has two parameters: the first parameter addr is the listening address, the second parameter represents the service-side handler,
Usually empty, which means that the server invokes HTTP. Defaultservemux, and the business logic written by the server
Handler http. Handle () or HTTP. Handlefunc () injects HTTP by default. In Defaultservemux,
The specific code is as follows:
http. Handle ("/foo", Foohandler) http. Handlefunc ("/bar", func (w http). Responsewriter, R *"Hello,%q", HTML. Escapestring (R.url. Path)}) log. Fatal (http. Listenandserve (": 8080"
If you want more control over the behavior of the server, you can customize HTTP. Server, the code is as follows:
S: = &http. server{ ": 8080", handler:myhandler, Ten * Time . Second, Ten * Time . Second, 1,} log. Fatal (S.listenandserve ())
Type Serverstruct{Addrstring //TCP address to listen on, ": http" if emptyHandler Handler//handler to invoke, HTTP. Defaultservemux if nilReadTimeout time. Duration//maximum duration before timing out read of the requestWriteTimeout time. Duration//maximum duration before timing out write of the responseMaxheaderbytesint //maximum size of request headers, defaultmaxheaderbytes if 0Tlsconfig *tls. Config//Optional TLS config, used by Listenandservetls//Tlsnextproto Optionally specifies a function to take over//ownership of the provided TLS connection when an NPN//protocol upgrade has occurred. The map key is the protocol//name negotiated. The Handler argument should is used to//handle HTTP requests and would initialize the Request ' s TLS//and remoteaddr if not already set. The connection is//automatically closed when the function returns.Tlsnextproto map[string]func (*server, *TLS. Conn, Handler)//connstate Specifies an optional callback function//called when a client connection changes state. See the//connstate type and associated constants for details.connstate func (net. Conn, Connstate)//errorlog Specifies an optional logger for errors accepting//connections and unexpected behavior from handlers. //If Nil, logging goes to OS. Stderr via the log package ' s//Standard logger.Errorlog *log. Logger//contains filtered or unexported fields}
Listenandserve starts an HTTP server with a given address and handler. The handler isusually nil, which means to use Defaultservemux. Handle and Handlefunc add handlers to defaultservemux:http. Handle ("/foo", Foohandler) http. Handlefunc ("/bar", func (w http. Responsewriter, R *http. Request) {fmt. fprintf (W,"Hello,%q.", HTML. Escapestring (R.url. Path)}) log. Fatal (http. Listenandserve (": 8080", nil))
Func Handle
Func Handle (pattern string, handler handler)
Handle registers the handler for the given pattern in the Defaultservemux. The documentation for Servemux explains how patterns is matched.
Func Handlefunc
Func Handlefunc (Pattern string, handler func (Responsewriter, *request))
Handlefunc registers the handler function for the given pattern in the Defaultservemux. The documentation for Servemux explains how patterns is matched.
Func Listenandserve
Func listenandserve (addr string, handler handler) error
Listenandserve listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming CO Nnections. Handler is typically nil, in which case the Defaultservemux is used.