Documentation ¶
Overview ¶
Package httpheader implements encoding of structs into http.Header fields.
As a simple example:
type Options struct { ContentType string `header:"Content-Type"` Length int } opt := Options{"application/json", 2} h, _ := httpheader.Headers(opt) fmt.Printf("%#v", h) // will output: // http.Headers{"Content-Type":[]string{"application/json"},"Length":[]string{"2"}}
The exact mapping between Go values and http.Header is described in the documentation for the Header() function.
Index ¶
Constants ¶
const Version = "0.3.1"
Version ...
Variables ¶
This section is empty.
Functions ¶
func Header ¶
Header returns the http.Header encoding of v.
Header expects to be passed a struct, and traverses it recursively using the following encoding rules.
Each exported struct field is encoded as a Header field unless
- the field's tag is "-", or
- the field is empty and its tag specifies the "omitempty" option
The empty values are false, 0, any nil pointer or interface value, any array slice, map, or string of length zero, and any time.Time that returns true for IsZero().
The Header field name defaults to the struct field name but can be specified in the struct field's tag value. The "header" key in the struct field's tag value is the key name, followed by an optional comma and options. For example:
// Field is ignored by this package. Field int `header:"-"` // Field appears as Headers field "X-Name". Field int `header:"X-Name"` // Field appears as Headers field "X-Name" and the field is omitted if // its value is empty Field int `header:"X-Name,omitempty"` // Field appears as Headers field "Field" (the default), but the field // is skipped if empty. Note the leading comma. Field int `header:",omitempty"`
For encoding individual field values, the following type-dependent rules apply:
Boolean values default to encoding as the strings "true" or "false". Including the "int" option signals that the field should be encoded as the strings "1" or "0".
time.Time values default to encoding as RFC1123("Mon, 02 Jan 2006 15:04:05 GMT") timestamps. Including the "unix" option signals that the field should be encoded as a Unix time (see time.Unix())
Slice and Array values default to encoding as multiple Header values of the same name. example: X-Name: []string{"Tom", "Jim"}, etc.
http.Header values will be used to extend the Header fields.
Anonymous struct fields are usually encoded as if their inner exported fields were fields in the outer struct, subject to the standard Go visibility rules. An anonymous struct field with a name given in its Header tag is treated as having that name, rather than being anonymous.
Non-nil pointer values are encoded as the value pointed to.
All other values are encoded using their default string representation.
Multiple fields that encode to the same Header filed name will be included as multiple Header values of the same name.