goutil

repository·master·Indexed 25 days ago

https://github.com/gookit/goutil

A massive utility library for Go providing over 900 functions to simplify common tasks. It includes specialized packages such as arrutil for slice and array manipulation, byteutil for byte slice operations, cflag and capp for building CLI applications and multi-command tools, cliutil for terminal interaction and colored output, dump for human-readable data printing, and envutil for environment variable access and system detection.

Tokens
56.1K
Snippets
115
Records
222
Agent score
76%

What's inside goutil

  1. What is CColor and when to use it

    master

    CColor is a lightweight command-line color output library that uses ANSI color codes to output text with colors.

    It is a simplified version of the gookit/color package, specifically designed to support only simple 16-color ANSI color output.

    When to use CColor:

    • When you only need basic 16-color ANSI support.
    • When you want a minimal, lightweight dependency.

    When to use gookit/color instead:

    • If you require richer color rendering (e.g., 256-color or TrueColor support).
  2. Overview of GoUtil packages

    master

    GoUtil is an extensive collection of over 900 utility functions for Go. It is organized into several functional categories:

    Basic Utility Packages

    • arrutil: Array and slice operations (type conversion, element checking, etc.).
    • byteutil: Common byte operations.
    • maputil: Map operations (conversion, sub-value retrieval, merging).
    • mathutil: Integer and number operations (conversion, math calculations, random).
    • reflects: Extended reflection utilities.
    • structs: Struct utilities (tag parsing, struct data).
    • strutil: String operations (bytes, checking, conversion, encoding, formatting).
    • cliutil: CLI utilities (reading input, executing commands).
    • envutil: Environment variable utilities.
    • fsutil: File system operations.
    • jsonutil: JSON encoding/decoding and reading/writing.
    • sysutil: System operations (environment, exec, user, process).

    Debug, Test & Errors

    • dump: Variable printing tool with automatic line breaks for slices/maps and call-site location display.
    • errorx: Enhanced error implementation with stack traces and error wrapping.
    • testutil: Testing helpers (HTTP testing, mocking ENV).
    • assert: Assertion functions for unit testing.
    • fakeobj: Mock implementations for interfaces like fs.File, fs.FileInfo, and fs.DirEntry.

    Extended Utility Packages

    • cflag: Wraps and extends flag.FlagSet for building CLI applications.
    • ccolor: ANSI color output for command line text.
    • syncs: Synchronization, goroutine, and signal utilities.
    • httpreq: An easy-to-use HTTP client wrapping http.Client.
    • clipboard: OS clipboard read/write operations.
    • timex: Enhanced time.Time implementation (date parsing, DayStart, DayAfter, etc.).
    • netutil: Network utilities (IP, Mac, Port, Hostname).
    • cmdline: Command line parsing and argument building.
    • encodes: Encoding/decoding, hashing, and crypto (Base64, Hex, etc.).
    • finder: File/directory searching with filtering and exclusion.
    • textscan: Fast text scanning/parsing (INI, Properties).
    • textutil: Extended text processing.
    • cmdr: Rapidly build and run command tasks.
    • process: Process management utilities.
    • fmtutil: Data formatting (e.g., data size).
    • goinfo: Go runtime and info utilities.
  3. Overview of netutil sub-packages

    master

    The netutil package is organized into several specialized sub-packages:

    • httpctypes: Provides commonly used HTTP content types.
    • httpheader: Provides commonly used HTTP header names.
    • httpreq: Provides utility functions for making and managing HTTP requests.
  4. Explore Gookit ecosystem packages

    master

    GoUtil is part of the larger Gookit ecosystem. Depending on your needs, you may want to use other specialized Gookit libraries:

    • Configuration: gookit/ini (INI files) or gookit/config (JSON, YAML, TOML, INI, HCL, ENV, and Flags).
    • Web & Routing: gookit/rux (HTTP request router).
    • CLI Development: gookit/gcli (CLI application builder) and gookit/color (CLI color support).
    • Logging & Events: gookit/slog (logging) and gookit/event (event manager/dispatcher).
    • Data Handling: gookit/filter (filtering/sanitizing), gookit/validate (data validation), and gookit/cache (File, Memory, Redis, Memcached).
    • Utilities: gookit/goutil (string, array/slice, map, format, cli, env, filesystem, test, etc.).
  5. Use fsutil for filesystem operations

    master
    The fsutil package provides utility functions for quick filesystem checks, creation, reading, and writing. It includes functions for checking files and directories, as well as performing common file operations.
  6. Use structs for struct manipulation and tag parsing

    master

    The structs package provides utilities for working with Go structs, including tag parsing, initialization, and data binding.

    Key capabilities:

    • Struct to Map: Convert structs to maps using ToMap, ToSMap (string map), or MustToMap.
    • Tag Parsing: Extract and parse struct tags using ParseTags or NewTagParser.
    • Initialization: Initialize structs with default values using Init or InitDefaults.
    • Data Binding: Bind map data to a struct pointer using BindData or SetValues.
    • Struct Copying: Copy data between structs using MapStruct.
  7. Use mathutil for numeric operations and conversions

    master

    The mathutil package provides a wide range of mathematical utilities, including comparison, range checking, random number generation, and type conversion (e.g., converting any to int, float64, or string).

    Key capabilities:

    • Comparison: Functions like Min, Max, InRange, and Compare for various numeric types.
    • Conversions: Robust conversion functions such as Int(in any), Float(in any), and String(in any) that handle errors or provide default values.
    • Formatting: Utilities like DataSize and FormatBytes to convert raw numbers into human-readable strings.
    • Randomness: Generating random integers with optional seeding.
  8. Use reflects for advanced reflection tasks

    master

    The reflects package extends Go's standard reflect package with helper functions for type checking, value conversion, and manipulation.

    Key capabilities:

    • Type Checking: Determine if a value is a specific type (e.g., IsTimeType, IsArrayOrSlice, IsNil).
    • Value Conversion: Convert values to different types or kinds using ConvToType or ConvToKind.
    • Map & Slice Utilities: Iterate over maps with EachMap or flatten slices with FlatSlice.
    • Function Invocation: Call functions dynamically using Call or SafeCall with options.
    • Unexported Access: Access or set unexported fields using UnexportedValue and SetUnexportedValue.
  9. Capabilities of the structs package

    master

    The structs package provides several utility functions for working with Go structs:

    • Convert to Map: Quickly convert a struct to map[string]any.
    • Aliases: Use structs.Aliases to implement simple string alias mapping.
    • Default Initialization: Initialize struct fields using the default tag.
    • Map to Struct: Quickly set struct field values from map data.
    • Tag Parsing: Parse structs to collect and extract tag values.
  10. Use Finder for file and directory searching

    master

    The finder package provides simple and convenient functionality for searching files and directories. It supports various filtering, exclusion, matching, and ignoring capabilities.

    Key features include:

    • Built-in matching/exclusion: Support for filtering by name, extension, suffix, prefix, glob patterns, and paths.
    • Extended matching: Support for matching based on file attributes such as file size and modification time (ModTime).