Involved Source Files
Package query implements encoding of structs into URL query parameters.
As a simple example:
type Options struct {
Query string `url:"q"`
ShowAll bool `url:"all"`
Page int `url:"page"`
}
opt := Options{ "foo", true, 2 }
v, _ := query.Values(opt)
fmt.Print(v.Encode()) // will output: "q=foo&all=true&page=2"
The exact mapping between Go values and url.Values is described in the
documentation for the Values() function.
Package-Level Type Names (total 2, in which 1 are exported)
/* sort exporteds by: | */
Encoder is an interface implemented by any type that wishes to encode
itself into URL values in a non-standard way.
( T) EncodeValues(key string, v *url.Values) error
tagOptions is the string following a comma in a struct field's "url" tag, or
the empty string. It does not include the leading comma.
Contains checks whether the tagOptions contains the specified option.
func parseTag(tag string) (string, tagOptions)
func valueString(v reflect.Value, opts tagOptions) string
Package-Level Functions (total 5, in which 1 are exported)
Values returns the url.Values encoding of v.
Values expects to be passed a struct, and traverses it recursively using the
following encoding rules.
Each exported struct field is encoded as a URL parameter 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 URL parameter name defaults to the struct field name but can be
specified in the struct field's tag value. The "url" 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 `url:"-"`
// Field appears as URL parameter "myName".
Field int `url:"myName"`
// Field appears as URL parameter "myName" and the field is omitted if
// its value is empty
Field int `url:"myName,omitempty"`
// Field appears as URL parameter "Field" (the default), but the field
// is skipped if empty. Note the leading comma.
Field int `url:",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 RFC3339 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 URL values of the
same name. Including the "comma" option signals that the field should be
encoded as a single comma-delimited value. Including the "space" option
similarly encodes the value as a single space-delimited string. Including
the "semicolon" option will encode the value as a semicolon-delimited string.
Including the "brackets" option signals that the multiple URL values should
have "[]" appended to the value name. "numbered" will append a number to
the end of each incidence of the value name, example:
name0=value0&name1=value1, etc.
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 URL
tag is treated as having that name, rather than being anonymous.
Non-nil pointer values are encoded as the value pointed to.
Nested structs are encoded including parent fields in value names for
scoping. e.g:
"user[name]=acme&user[addr][postcode]=1234&user[addr][city]=SFO"
All other values are encoded using their default string representation.
Multiple fields that encode to the same URL parameter name will be included
as multiple URL values of the same name.
isEmptyValue checks if a value should be considered empty for the purposes
of omitting fields with the "omitempty" option.
parseTag splits a struct field's url tag into its name and comma-separated
options.
reflectValue populates the values parameter from the struct fields in val.
Embedded structs are followed recursively (using the rules defined in the
Values function documentation) breadth-first.
valueString returns the string representation of a value.
Package-Level Variables (total 2, neither is exported)
The pages are generated with Goldsv0.3.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.