Documentation
¶
Index ¶
- func ArrayInt64ToString(arr []int64, delim string) string
- func ArrayStringToString(arr []string, delim string) string
- func CompareHashAndPassword(hashedPassword, password string) bool
- func DefaultIfEmpty[T any](value any, fallback T) T
- func GetFromInterface[T any](src map[string]any, key string, defaultValue T, checkZeroValue ...bool) T
- func HashPassword(password string) (*string, error)
- func RandInt64() (int64, error)
- func RandString(length int) (string, error)
- func StringToArrayInt64(str string, delim string) []int64
- func StringToArrayString(str, delim string, trim bool) []string
- type RsaKey
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ArrayInt64ToString ¶
ArrayInt64ToString converts a slice of int64 to a delimited string. It efficiently joins all numbers with the specified delimiter using a string builder.
Parameters:
- arr: The slice of int64 numbers to convert
- delim: The delimiter to insert between numbers
Returns:
- string: The resulting delimited string, or empty string if input is empty
Examples:
ArrayInt64ToString([]int64{1, 2, 3}, ",") // returns "1,2,3" ArrayInt64ToString([]int64{}, ",") // returns "" ArrayInt64ToString([]int64{42}, "-") // returns "42"
func ArrayStringToString ¶
ArrayStringToString converts a slice of strings to a delimited string. It efficiently joins all strings with the specified delimiter using a string builder.
Parameters:
- arr: The slice of strings to convert
- delim: The delimiter to insert between strings
Returns:
- string: The resulting delimited string, or empty string if input is empty
Examples:
ArrayStringToString([]string{"a", "b", "c"}, ",") // returns "a,b,c" ArrayStringToString([]string{}, ",") // returns "" ArrayStringToString([]string{"hello"}, " ") // returns "hello"
func CompareHashAndPassword ¶ added in v1.0.1
CompareHashAndPassword checks if a plain text password matches its hashed version. It uses bcrypt's comparison function to safely verify the password.
Parameters:
- hashedPassword: The hashed password to compare against
- password: The plain text password to verify
Returns:
- bool: true if passwords match, false otherwise
Examples:
isValid := CompareHashAndPassword(hashedPassword, "mypassword123") if isValid { // Password is correct }
func DefaultIfEmpty ¶
DefaultIfEmpty determines whether to return the original value or a fallback. It uses validate.IsZero to check if the input value is considered "zero".
Parameters:
- value: The value to check
- fallback: The value to return if empty
Returns:
- T: The original value if not empty, fallback otherwise
Examples:
DefaultIfEmpty[string]("", "fallback") // returns "fallback" DefaultIfEmpty[int](0, 42) // returns 42 DefaultIfEmpty[string]("hello", "fallback") // returns "hello"
func GetFromInterface ¶
func GetFromInterface[T any](src map[string]any, key string, defaultValue T, checkZeroValue ...bool) T
GetFromInterface safely retrieves a typed value from a map with a default fallback. Supports optional zero-value checking for determining when to use the default.
Parameters:
- src: Source map to retrieve value from
- key: Key to look up in the map
- defaultValue: Value to return if key missing or value is zero
- checkZeroValue: Optional flag to enable zero-value checking
Returns:
- T: The found value (type-asserted to T) or defaultValue
Examples:
m := map[string]any{"count": 5, "name": ""} GetFromInterface[int](m, "count", 0) // returns 5 GetFromInterface[string](m, "missing", "default") // returns "default" GetFromInterface[string](m, "name", "default", true) // returns "default"
func HashPassword ¶ added in v1.0.1
HashPassword securely hashes a plain text password using bcrypt. It uses the default cost factor for the hashing algorithm.
Parameters:
- password: The plain text password to hash
Returns:
- *string: A pointer to the hashed password string
- error: Any error that occurred during hashing, or nil if successful
Examples:
hashedPwd, err := HashPassword("mypassword123") if err != nil { // Handle error }
func RandInt64 ¶ added in v1.0.3
RandInt64 generates a cryptographically secure random 64-bit integer. It uses crypto/rand to generate random bytes which are then converted to an int64.
Returns:
- int64: A random 64-bit integer
- error: Any error that occurred during random number generation
Examples:
RandInt64() // returns 8674665223082153551, nil RandInt64() // returns -6129484611666145821, nil
func RandString ¶ added in v1.0.3
RandString generates a random hex string of the specified length. It uses crypto/rand for secure random number generation.
Parameters:
- length: The desired length of the output hex string (actual output will be 2x this length)
Returns:
- string: The generated random hex string
- error: Any error that occurred during random number generation
Examples:
RandString(4) // returns "a1b2c3d4", nil RandString(2) // returns "f5e9", nil RandString(0) // returns "", nil
func StringToArrayInt64 ¶
StringToArrayInt64 converts a delimited string to a slice of int64 numbers. It splits the string by the delimiter, trims whitespace, and converts valid numbers. Invalid numbers and empty strings are skipped.
Parameters:
- str: The input string to split and convert
- delim: The delimiter that separates the numbers
Returns:
- []int64: A slice containing the successfully parsed numbers
Examples:
StringToArrayInt64("1,2,3", ",") // returns []int64{1, 2, 3} StringToArrayInt64("1, 2, 3", ",") // returns []int64{1, 2, 3} StringToArrayInt64("1,invalid,3", ",") // returns []int64{1, 3} StringToArrayInt64("", ",") // returns []int64{}
func StringToArrayString ¶
StringToArrayString converts a delimited string to a slice of strings. It splits the string by the delimiter and optionally trims whitespace and empty entries.
Parameters:
- str: The input string to split
- delim: The delimiter to split on
- trim: If true, trims whitespace from each part and removes empty strings
Returns:
- []string: The resulting slice of strings
Examples:
StringToArrayString("a,b,c", ",", false) // returns []string{"a", "b", "c"} StringToArrayString("a, b, c", ",", true) // returns []string{"a", "b", "c"} StringToArrayString("a,,c", ",", false) // returns []string{"a", "", "c"} StringToArrayString("a,,c", ",", true) // returns []string{"a", "c"} StringToArrayString("", ",", true) // returns []string{}
Types ¶
type RsaKey ¶
func GenerateRsaKeyPair ¶
GenerateRsaKeyPair generates RSA private and public keys and returns them as PEM-encoded strings. It uses a 2048-bit key size which provides a good balance of security and performance.
Parameters:
- none
Returns:
- RsaKey: Struct containing the generated key pair as PEM-encoded strings
- error: Any error that occurred during key generation or encoding
Examples:
keys, err := GenerateRsaKeyPair() if err != nil { log.Fatal(err) } fmt.Println(keys.PrivateKey) // prints PEM-encoded private key fmt.Println(keys.PublicKey) // prints PEM-encoded public key