utils

package
v0.0.0-...-9e693b3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StructToURLParams

func StructToURLParams(inputStruct interface{}) (string, error)

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

func WrapWithSignature(inputStruct interface{}, apiSecret string, timestamp int64) interface{}

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL