12. FileServer例子
// deliver files from the directory /var/www
fileServer := http.FileServer("/var/www", "/")
// register the handler and deliver requests to it
err := http.ListenAndServe(":8000", fileServer)
if err != nil {
log.Fatal(err)
13. Request
type Request struct {
Method string // GET, POST, PUT, etc.
RawURL string // The raw URL given in the request.
URL *URL // Parsed URL.
Proto string // "HTTP/1.0"
ContentLength int64
Header Header
Cookie []*Cookie
Body io.ReadCloser
TransferEncoding []string
Close bool
...
}
func ReadRequest
func (*Request) FormValue
func (*Request) MultipartReader
func (*Request) ParseForm
func (*Request) ProtoAtLeast
func (*Request) Write
func (*Request) WriteProxy
14. Response
type Response struct {
Status string // e.g. "200 OK"
StatusCode int // e.g. 200
Proto string // e.g. "HTTP/1.0"
RequestMethod string // e.g. "HEAD", "CONNECT", "GET", etc.
Header Header
SetCookie []*Cookie
Body io.ReadCloser
ContentLength int64
TransferEncoding []string
Close bool
Trailer Header
}
func Get
func Head
func Post
func PostForm
func ReadResponse
func (*Response) ProtoAtLeast
func (*Response) Write
15. URL
scheme://[userinfo@]host/path[?query][#fragment]
type URL struct {
Raw string // the original string
Scheme string // scheme
RawAuthority string // [userinfo@]host
RawUserinfo string // userinfo
Host string // host
RawPath string // /path[?query][#fragment]
Path string // /path
OpaquePath bool // path is opaque (unrooted when scheme is present)
RawQuery string // query
Fragment string // fragment
}
func ParseRequestURL
func ParseURL
func ParseURLReference
func (*URL) IsAbs
func (*URL) ParseURL
func (*URL) ResolveReference
func (*URL) String
16. Form
• request.Form
– Form map[string][]string
• request.ParseForm
• request.FormValue
– FormValue(key string) string
17. Cookie
type Cookie struct {
Name string
Value string
Path string
Domain string
Expires time.Time
RawExpires string
MaxAge int
Secure bool
HttpOnly bool
Raw string
Unparsed []string // Raw text of unparsed attribute-value pairs
}
• request.Cookie
• response.SetCookie