utils

package module
v0.0.0-...-d637b58 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2025 License: MIT Imports: 11 Imported by: 0

README

Go Utils - High-Performance Toolkit

Go Reference License Go Report Card

Go Utils is a curated collection of high-performance, production-ready Go utility functions and packages. Designed for speed, efficiency, and robustness, these utilities have been rigorously tested and optimized for demanding applications.

Key Features:

  • Performance-Focused: Leveraging unsafe optimizations and efficient algorithms for maximum speed.
  • Production-Ready: Battle-tested across numerous production environments.
  • Comprehensive Suite: Covering a wide range of utility needs from string manipulation to concurrency management.
  • Well-Documented: Ensuring reliability and ease of use.

Packages

Package Description
bytesutils Optimized byte manipulation utilities.
cacheutils High-performance caching implementations.
channelutils Robust channel utilities with comprehensive testing.
gopool Efficient goroutine pool management for concurrent tasks.
intutils Optimized integer manipulation functions.
iterutils Flexible iterator implementations for data processing.
net Networking utilities, including HTTP client helpers and user agent parsing.
poolutils Versatile pool implementations (LRU, limited, recycler) for resource management.
sliceutils Optimized slice manipulation utilities for common operations.
stringutils String manipulation with unsafe optimizations for critical performance paths.
terminalutils ANSI terminal utilities for rich command-line interfaces.

Installation

go get github.com/sudosz/go-utils

Usage

Import the specific package you need:

import (
        "fmt"
        stringutils "github.com/sudosz/go-utils/strings"
)

func main() {
        result := stringutils.ToLower("EXAMPLE STRING")
        fmt.Println(result) // Output: example string
}

Contributing

Contributions are highly encouraged! Please follow these guidelines:

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature/your-feature).
  3. Implement your changes and write thorough tests.
  4. Ensure code adheres to gofmt and golint.
  5. Submit a pull request with a clear description of your changes.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[Fn ~func() bool](funcs ...Fn) bool

All returns true if all provided functions return true. Optimization: Short-circuits on first false result.

func Any

func Any(funcs ...func() bool) bool

Any returns true if any provided function returns true. Optimization: Short-circuits on first true result.

func Copy

func Copy[T any](src, dst *T)

Copy copies the value from src to dst using unsafe pointer operations for zero-overhead copying. Optimization: Uses unsafe to avoid allocation and deep copying.

func GenerateUUID

func GenerateUUID(ps ...string) string

GenerateUUID generates a UUID with optional prefix and suffix. Optimization: Efficient string concatenation with minimal allocations.

func GetMimeTypeByFileName

func GetMimeTypeByFileName(filename string) string

GetMimeTypeByFileName returns the MIME type based on the file extension. Optimization: Uses standard library mime.TypeByExtension for efficiency.

func IsDir

func IsDir(path string) error

IsDir checks if the path is a directory, returning an error if not. Optimization: Uses os.Stat directly for minimal overhead.

func IsImageFileExt

func IsImageFileExt(ext string) bool

IsImageFileExt checks if the extension matches a known image file extension. Optimization: Uses a fixed array and linear search (sufficient for small set).

func IsNil

func IsNil(v any) bool

IsNil checks if the value is nil or a nil pointer using reflection. Optimization: Combines direct nil check with reflection-based pointer check.

func IsTimeoutError

func IsTimeoutError(err error) bool

IsTimeoutError checks if the error indicates a timeout from various sources. Optimization: Uses errors.Is and os.IsTimeout for efficient error checking.

func Last

func Last[T any, Slice ~[]T](slice Slice) T

Last returns the last element of a slice. Optimization: Direct index access for O(1) performance.

func Must

func Must[T any](val T, err error) T

Must returns the value and discards the error, useful for simplifying code when errors are ignorable. Optimization: No additional overhead beyond returning the value.

func NotifyOnEnd

func NotifyOnEnd(ch chan<- struct{}, fn ...func())

NotifyOnEnd runs each function in a goroutine and sends a signal on the channel when each completes. Optimization: Launches goroutines concurrently for parallelism.

func Ptr

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

Ptr returns a pointer to the provided value. Optimization: Simple allocation, no additional overhead possible.

func RandomChoice

func RandomChoice[T any, Slice ~[]T](slice Slice) T

RandomChoice selects a random element from the slice using a seeded random source. Optimization: Pre-seeded random source avoids repeated initialization.

func Retry

func Retry(fn func() error, times int) (err error)

Retry attempts the function up to 'times' times until it succeeds or exhausts retries. Optimization: Minimal overhead with a simple loop; could add backoff but not requested.

func Reverse

func Reverse[T any](obj []T) []T

Reverse reverses the order of elements in the slice in-place and returns it. Optimization: Operates in-place to minimize memory usage.

Types

type RWNopeCloser

type RWNopeCloser interface {
	io.Reader
	io.Writer
	io.Closer
}

RWNopeCloser is an interface combining Reader, Writer, and Closer.

func NopeCloserRW

func NopeCloserRW(rw io.ReadWriter) RWNopeCloser

NopeCloserRW wraps an io.ReadWriter with a no-op Closer. Optimization: Simple wrapping with no additional overhead.

Directories

Path Synopsis
net

Jump to

Keyboard shortcuts

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