Go's Net/http pack uses

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Online information is a bit miscellaneous, and some also doped middleware into, resulting in the use of obstacles, so, directly on the official documents:

1, first understand []byte string conversion, the simplest way is

String goto []byte:[]byte (para string)

[]byte Turn string:string ([]byte)

Then there are some implementations of IO packages, such as:

bytes.NewBuffer(para []byte).String()

2, how to get form,body,head from request, here is the official API:

 type requestfunc newrequest (method, urlstr string, body io. Reader) (*request, error) func readrequest (b *bufio. Reader) (req *request, err Error) func (R *request) Protoatleast (major, Minor int) Boolfunc (R *request) useragent () string Func (R *request) Referer () Stringfunc (R *request) Addcookie (c *cookie) func (R *request) Setbasicauth (username, password String) func (R *request) Write (w io. Writer) Errorfunc (R *request) writeproxy (w io). Writer) Errorfunc (R *request) cookies () []*cookiefunc (R *request) cookie (name string) (*cookie, Error) func (R *request) Parseform () Errorfunc (R *request) parsemultipartform (maxmemory Int64) Errorfunc (R *request) Formvalue (key string) Strin Gfunc (R *request) Postformvalue (key string) Stringfunc (R *request) formfile (key string) (multipart. File, *multipart. Fileheader, Error) func (R *request) Multipartreader () (*multipart. Reader, error)  
type Headerfunc (h Header) Get(key string) stringfunc (h Header) Set(key, value string)func (h Header) Add(key, value string)func (h Header) Del(key string)func (h Header) Write(w io.Writer) errorfunc (h Header) WriteSubset(w io.Writer, exclude map[string]bool) error

Where the type of body is the type of Io.read,head is map[string][]string, so the acquisition method is not the same:

Get Body:io.ioutil.ReadAll (req. Body), return []byte type of data, generally used for JSON

Gets the header:req. Header.get ("Para")

Gets the data in the form: req. Parseform, then, Para=postformvalue ("Para")

3, return the data to the request:

The data structure of response is relatively simple:

type Responsefunc ReadResponse(r *bufio.Reader, req *Request) (*Response, error)func (r *Response) ProtoAtLeast(major, minor int) boolfunc (r *Response) Cookies() []*Cookiefunc (r *Response) Location() (*url.URL, error)func (r *Response) Write([]byte) error//不同版本这个有差异

With write, the data is converted to byte.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.