util

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 5 Imported by: 0

README

util

Generic utility functions for slices, maps, pointers, string sanitization, and validation helpers.

Install

go get github.com/kbukum/gokit

Quick Start

package main

import (
    "fmt"
    "github.com/kbukum/gokit/util"
)

func main() {
    // Pointer helpers
    name := util.Ptr("hello")
    fmt.Println(util.Deref(name)) // "hello"

    // Slice utilities
    nums := []int{1, 2, 3, 2, 1}
    unique := util.Unique(nums)           // [1, 2, 3]
    even := util.Filter(nums, func(n int) bool { return n%2 == 0 }) // [2, 2]

    // String sanitization
    safe := util.IsSafeString("SELECT * FROM users") // false
    clean := util.SanitizeString("  hello\x00world  ") // "helloworld"
}

Key Types & Functions

Name Description
Ptr[T]() / Deref[T]() Pointer creation and safe dereference
Contains[T]() / Filter[T]() / Map[T,U]() Generic slice operations
Unique[T]() / Keys[K,V]() / Values[K,V]() Collection utilities
Coalesce[T]() Return first non-zero value
SanitizeString() / SanitizeEnvValue() Input sanitization
IsSafeString() SQL injection / XSS detection
ValidateUUID() / ValidateNonEmpty() Input validation helpers

⬅ Back to main README

Documentation

Overview

Package util provides generic utility functions for gokit applications.

It includes slice operations, pointer helpers, map utilities, string sanitization, and common validation helpers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Coalesce

func Coalesce[T comparable](values ...T) T

Coalesce returns the first non-zero value, or the zero value if all are zero.

func Contains

func Contains[T comparable](slice []T, val T) bool

Contains checks if a slice contains a value.

func Deref

func Deref[T any](p *T) T

Deref returns the value pointed to by p, or the zero value if p is nil.

func Filter

func Filter[T any](slice []T, predicate func(T) bool) []T

Filter returns a new slice containing only elements that satisfy the predicate.

func IsSafeString

func IsSafeString(s string) bool

IsSafeString returns false if s contains patterns commonly associated with SQL injection or XSS attacks.

func Keys

func Keys[K comparable, V any](m map[K]V) []K

Keys returns the keys of a map.

func Map

func Map[T any, U any](slice []T, transform func(T) U) []U

Map transforms a slice using the given function.

func MaskSecret

func MaskSecret(s string, visiblePrefix int) string

MaskSecret hides sensitive parts of a string for safe display in logs. If the string is shorter than visiblePrefix, it is fully masked.

func ParseSize

func ParseSize(s string, defaultBytes int64) int64

ParseSize parses a human-readable size string (e.g. "10MB", "512KB", "2GB") into bytes. Returns defaultBytes if the string cannot be parsed.

func Ptr

func Ptr[T any](v T) *T

Ptr returns a pointer to the given value.

func SanitizeEnvValue

func SanitizeEnvValue(s string) string

SanitizeEnvValue cleans an environment variable value by removing surrounding quotes and trimming whitespace.

func SanitizeString

func SanitizeString(s string) string

SanitizeString trims whitespace and removes control characters from s.

func Unique

func Unique[T comparable](slice []T) []T

Unique returns a slice with duplicate values removed, preserving order.

func ValidateNonEmpty

func ValidateNonEmpty(field, value string) error

ValidateNonEmpty validates that value is not empty after trimming whitespace.

func ValidateUUID

func ValidateUUID(field, value string) (uuid.UUID, error)

ValidateUUID validates that value is a valid UUID string and returns the parsed UUID.

func Values

func Values[K comparable, V any](m map[K]V) []V

Values returns the values of a map.

Types

This section is empty.

Jump to

Keyboard shortcuts

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