Handler Interface
A Handler responds to an HTTP request.
1 | type Handler interface { |
ServeHTTP should write reply headers and data to the ResponseWriter and then return. Returning signals that the request is finished; it is not valid to use the ResponseWriter or read from the Request.Body after or concurrently with the completion of the ServeHTTP call.
Cautious handlers should read the Request.Body first, and then reply.
Server
1 | func ListenAndServe(add string, handler Handler) error |
ListenAndServe listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.
The handler is typically nil, in which case the DefaultServeMux is used.
ListenAndServe always returns a non-nil error.
(TLS is similiar to SSL)
1 | func ListenAndServeTLS(addr, certFile, keyFile string, handler Handler) error |
ListenAndServeTLS acts identically to ListenAndServe, except that it expects HTTPS connections. Additionally, files containing a certificate and matching private key for the server must be provided.
Request
1 | type Request struct { |
Play with request
- Retrieve URL & Form data
- See HTTP Method
- See URL values
- Work with the HTTP header
1 | type URL struct { |
1 | type Header map[string][]string |
Response
A ResponseWriter interface is used by an HTTP handler to construct an HTTP response.
A ResponseWriter may not be used after the Handler.ServeHTTP method has returned.
1 | type ResponseWriter interface { |
Http.header functions
1 | func (h Header) Add(key, value string) |
Set the response header
1 | res.Header().Set("Content-Type", "text/html; charset=utf-8") |