Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func StructToURLParams ¶
StructToURLParams converts a struct to a URL-encoded query string.
This function uses the `json` struct tags as parameter keys and excludes fields with empty or zero values. It supports various data types, including slices, arrays, integers, floats, booleans, and strings.
Supported Behavior:
- Fields with `json` tags are used as keys. Fields without tags or with `json:"-"` are ignored.
- Zero values (e.g., empty strings, 0 for integers, 0.0 for floats) are omitted.
- Slices and arrays are converted to multiple key-value pairs.
Parameters:
- inputStruct: The input struct to be converted into URL parameters. It must be of kind `struct`.
Returns:
- A URL-encoded query string as a `string`.
- An `error` if the input is not a struct or if any other issue occurs.
Example:
type MyStruct struct {
Name string `json:"name"`
Age int `json:"age"`
Tags []string `json:"tags"`
IsAdmin bool `json:"is_admin"`
}
data := MyStruct{
Name: "John",
Age: 30,
Tags: []string{"golang", "developer"},
IsAdmin: true,
}
query, err := StructToURLParams(data)
if err != nil {
log.Fatal(err)
}
fmt.Println(query)
// Output: name=John&age=30&tags=golang&tags=developer&is_admin=true
Limitations:
- Only fields with `json` tags are considered.
- Non-struct input will result in an error.
func WrapWithSignature ¶
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.