Documentation
¶
Overview ¶
Package stringutil provides domain-agnostic string utilities including sensitive PII masking (MaskEmail, MaskPhone) and cryptographically secure random string generation.
Package stringutil provides domain-agnostic string helper functions.
Has zero knowledge of any business domain. Suitable for use in any service that imports go-libs.
Index ¶
- func ContainsAny(s string, subs ...string) bool
- func IsBlank(s string) bool
- func MaskEmail(email string) string
- func MaskPhone(phone string) string
- func OrDefault(s, defaultVal string) string
- func RandomAlphanumeric(length int) (string, error)
- func RandomSecureString(length int) (string, error)
- func RandomStringFromCharset(length int, charset string) (string, error)
- func RandomStringFromReader(r io.Reader, length int, charset string) (string, error)
- func TrimAndLower(s string) string
- func Truncate(s string, n int) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContainsAny ¶
ContainsAny returns true if s contains any of the given substrings.
func MaskEmail ¶
MaskEmail masks an email address for safe logging. "john.doe@example.com" → "jo******@example.com"
Example ¶
package main
import (
"fmt"
"github.com/umesh0492/go-libs/stringutil"
)
func main() {
fmt.Println(stringutil.MaskEmail("alice.smith@example.com"))
}
Output: al*********@example.com
func MaskPhone ¶
MaskPhone masks a phone number for safe logging. Preserves leading '+' if present and masks all characters except the last 4 digits.
func OrDefault ¶
OrDefault returns s if non-empty, otherwise returns the defaultVal. Replaces ad-hoc ternary patterns across the codebase.
Example ¶
package main
import (
"fmt"
"github.com/umesh0492/go-libs/stringutil"
)
func main() {
fmt.Println(stringutil.OrDefault("", "default_value"))
fmt.Println(stringutil.OrDefault("custom_value", "default_value"))
}
Output: default_value custom_value
func RandomAlphanumeric ¶
RandomAlphanumeric returns a cryptographically secure random string of the specified length strictly consisting of alphanumeric characters ([a-zA-Z0-9]).
func RandomSecureString ¶
RandomSecureString returns a random string of the specified length containing alphanumeric characters and symbols (!@#$). Uses crypto/rand for cryptographic security.
func RandomStringFromCharset ¶
RandomStringFromCharset returns a cryptographically secure random string of length n drawn from charset.
func RandomStringFromReader ¶
RandomStringFromReader returns a random string of length n drawn from charset using reader r.
func TrimAndLower ¶
TrimAndLower trims whitespace and lowercases a string. Useful for normalising email addresses before DB lookup.
Types ¶
This section is empty.