Documentation
¶
Index ¶
- func DerefPointer[T any](p *T, defaultValue T) T
- func GenerateMessageID() string
- func GenerateRandomString(length int) string
- func GenerateTrxID() string
- func GenerateTrxIDWithPrefix(prefix string) string
- func GenerateTrxIDWithSuffix(suffix string) string
- func GenerateUniqueID() string
- func NormalizePhoneNumber(phone string) string
- func Pointer[T any](v T) *T
- func StringToDate(dateStr string) (time.Time, error)
- func UnmarshalFromMap[T any](dataMap map[string]interface{}) (T, error)
- func UnmarshalTo[T any](jsonString string) (T, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DerefPointer ¶
func DerefPointer[T any](p *T, defaultValue T) T
DerefPointer safely dereferences a pointer and returns its value. If the pointer is nil, it returns the provided defaultValue instead. This function is generic and works with any type T.
Parameters:
- p: A pointer to a value of type T that may be nil
- defaultValue: The value to return if p is nil
Returns:
- The dereferenced value if p is not nil, otherwise defaultValue
Example:
var ptr *int value := DerefPointer(ptr, 42) // returns 42 num := 10 value = DerefPointer(&num, 42) // returns 10
func GenerateMessageID ¶
func GenerateMessageID() string
GenerateMessageID generates a new UUID v4 string to be used as a message ID. Uses the standard UUID format with hyphens.
Returns:
- string: A UUID v4 string in the format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Example:
messageID := GenerateMessageID() // Output: 550e8400-e29b-41d4-a716-446655440000
func GenerateRandomString ¶
GenerateRandomString generates a random alphanumeric string of the specified length. The string consists of uppercase letters, lowercase letters, and digits.
Parameters:
- length: Desired length of the random string
Returns:
- string: Randomly generated alphanumeric string
Example:
randomStr := GenerateRandomString(10) // Output: a random string like "aZ3bC9dE1F"
func GenerateTrxID ¶
func GenerateTrxID() string
GenerateTrxID generates a unique transaction ID based on the current timestamp and a random 4-digit number. The ID format is YYMMDDHHMMSS followed by a 4-digit random number (0000-9999).
Returns:
- string: A 16-character transaction ID
Example:
trxID := GenerateTrxID() // Output: 2411131534251234 (YY=24, MM=11, DD=13, HH=15, MM=34, SS=25, Random=1234)
func GenerateTrxIDWithPrefix ¶
GenerateTrxIDWithPrefix generates a transaction ID with a specified prefix. The transaction ID follows the format: prefix + YYMMDDHHMMSS + 4-digit random number.
Parameters:
- prefix: String to prepend to the transaction ID
Returns:
- string: Transaction ID with the specified prefix
Example:
trxID := GenerateTrxIDWithPrefix("TRX-")
// Output: TRX-2411131534251234
func GenerateTrxIDWithSuffix ¶
GenerateTrxIDWithSuffix generates a transaction ID with a specified suffix. The transaction ID follows the format: YYMMDDHHMMSS + 4-digit random number + suffix.
Parameters:
- suffix: String to append to the transaction ID
Returns:
- string: Transaction ID with the specified suffix
Example:
trxID := GenerateTrxIDWithSuffix("-END")
// Output: 2411131534251234-END
func GenerateUniqueID ¶
func GenerateUniqueID() string
GenerateUniqueID generates a short unique ID string by extracting the first 8 characters of a UUID v4. This provides a shorter identifier while maintaining reasonable uniqueness for most use cases.
Returns:
- string: The first 8 characters of a UUID, or the full UUID if it's shorter than 8 characters
Example:
uniqueID := GenerateUniqueID() // Output: 550e8400
func NormalizePhoneNumber ¶
NormalizePhoneNumber normalizes phone numbers by removing all non-numeric characters and adding the Indonesian country code (62) only if the phone starts with "08".
Parameters:
- phone: Phone number string in various formats
Returns:
- string: Normalized phone number containing only digits
Examples:
NormalizePhoneNumber("+62-812-3456-789") // Returns: 6281234567890
NormalizePhoneNumber("0812-3456-789") // Returns: 6281234567890
NormalizePhoneNumber("812-3456-789") // Returns: 8123456789
NormalizePhoneNumber("+1-202-555-1234") // Returns: 12025551234
func Pointer ¶
func Pointer[T any](v T) *T
Pointer returns a pointer to the given value of any type T. This is a generic helper function useful for creating pointers to literals or values where taking the address directly is not possible.
Example:
s := Pointer("hello") // *string
i := Pointer(42) // *int
b := Pointer(true) // *bool
func StringToDate ¶
StringToDate converts a string to a date format (YYYY-MM-DD).
Parameters:
- dateStr: String representing the date in "YYYY-MM-DD" format
Returns:
- string: Date in "YYYY-MM-DD" format
- error: Error if parsing fails
Example:
date, err := StringToDate("2024-11-13")
// Output: "2024-11-13", nil
func UnmarshalFromMap ¶
UnmarshalFromMap deserializes a map[string]interface{} into a value of type T. It takes a map as input and returns the unmarshaled value of type T along with any error that occurred during unmarshaling.
Type parameter T can be any type that is compatible with json.Unmarshal.
Parameters:
- dataMap: A map[string]interface{} containing data to be unmarshaled.
Returns:
- T: The unmarshaled value of the specified type.
- error: An error if the data cannot be marshaled/unmarshaled into type T.
Example:
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
dataMap := map[string]interface{}{
"name": "John",
"age": 30,
}
person, err := UnmarshalFromMap[Person](dataMap)
func UnmarshalTo ¶
UnmarshalTo deserializes a JSON string into a value of type T. It takes a JSON string as input and returns the unmarshaled value of type T along with any error that occurred during unmarshaling.
Type parameter T can be any type that is compatible with json.Unmarshal.
Parameters:
- jsonString: A string containing valid JSON data to be unmarshaled.
Returns:
- T: The unmarshaled value of the specified type.
- error: An error if the JSON is invalid or cannot be unmarshaled into type T.
Example:
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
person, err := UnmarshalTo[Person](`{"name":"John","age":30}`)
Types ¶
This section is empty.