Package form Decodes url.Values into Go value(s) and Encodes Go value(s) into url.Values.
It has the following features:
- Supports map of almost all types.
- Supports both Numbered and Normal arrays eg. `"Array[0]"` and just `"Array"` with multiple values passed.
- Slice honours the specified index. eg. if "Slice[2]" is the only Slice value passed down, it will be put at index 2; if slice isn't big enough it will be expanded.
- Array honours the specified index. eg. if "Array[2]" is the only Array value passed down, it will be put at index 2; if array isn't big enough a warning will be printed and value ignored.
- Only creates objects as necessary eg. if no `array` or `map` values are passed down, the `array` and `map` are left as their default values in the struct.
- Allows for Custom Type registration.
- Handles time.Time using RFC3339 time format by default, but can easily be changed by registering a Custom Type, see below.
- Handles Encoding & Decoding of almost all Go types eg. can Decode into struct, array, map, int... and Encode a struct, array, map, int...
Common Questions
- Does it support encoding.TextUnmarshaler? No because TextUnmarshaler only accepts []byte but posted values can have multiple values, so is not suitable.
- Mixing `array/slice` with `array[idx]/slice[idx]`, in which order are they parsed? `array/slice` then `array[idx]/slice[idx]`
Supported Types ( out of the box )
----------
*`string`
*`bool`
*`int`, `int8`, `int16`, `int32`, `int64`
*`uint`, `uint8`, `uint16`, `uint32`, `uint64`
*`float32`, `float64`
*`struct` and `anonymous struct`
*`interface{}`
*`time.Time` - by default using RFC3339
* a `pointer` to one of the above types
*`slice`, `array`
*`map`
*`custom types` can override any of the above types
* many other types may be supported inherently
**NOTE**: `map`, `struct` and `slice` nesting are ad infinitum.
Installation
------------
Use go get.
go get github.com/go-playground/form
Then import the form package into your own code.
import "github.com/go-playground/form/v4"
Usage
-----
- Use symbol `.` for separating fields/structs. (eg. `structfield.field`)
- Use `[index or key]` for access to index of a slice/array or key for map. (eg. `arrayfield[0]`, `mapfield[keyvalue]`)
```html
<formmethod="POST">
<inputtype="text"name="Name"value="joeybloggs"/>
<inputtype="text"name="Age"value="3"/>
<inputtype="text"name="Gender"value="Male"/>
<inputtype="text"name="Address[0].Name"value="26 Here Blvd."/>
NOTE: the 1 allocation and B/op in the first 4 decodes is actually the struct allocating when passing it in, so primitives are actually zero allocation.
Competitor benchmarks can be found [here](https://github.com/go-playground/form/blob/master/benchmarks/benchmarks.md)
Complimentary Software
----------------------
Here is a list of software that compliments using this library post decoding.
* [Validator](https://github.com/go-playground/validator) - Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving.
* [mold](https://github.com/go-playground/mold) - Is a general library to help modify or set data within data structures and other objects.
License
------
Distributed under MIT License, please see license file in code for more details.