utils

module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: MIT

README

Utils

A collection of small Go helpers that can be shared between projects. The repository is organised by package so you can import only the utilities you need.

File

Utilities that simplify common file system operations.

  • RemoveAll(dir string) - Recursively delete a directory while ignoring errors.

    file.RemoveAll("/tmp/cache")
    
  • RemoveFile(path string) - Delete a single file and log any failures.

    file.RemoveFile("/tmp/out.log")
    
  • CloseFile(c io.Closer) - Safely close a file descriptor and log errors.

    f, _ := os.Open("data.txt")
    file.CloseFile(f)
    
  • ReadLines(filename string) ([]string, error) - Read a text file into a slice of lines.

    lines, err := file.ReadLines("notes.txt")
    if err != nil {
        log.Fatal(err)
    }
    
  • SaveFile(dir, name string, data []byte) error - Write a .html file to a directory, creating it if necessary.

    err := file.SaveFile("public", "index", []byte("<h1>Hello</h1>"))
    if err != nil {
        log.Fatal(err)
    }
    
  • *ReadFile(path string) (bytes.Reader, error) - Load file contents into a bytes.Reader.

    r, err := file.ReadFile("public/index.html")
    if err != nil {
        log.Fatal(err)
    }
    

Math

Helpers for basic numeric calculations and probability checks.

  • Min(a, b int) int and Max(a, b int) int - Return the smaller or larger of two integers.

    m := math.Min(3, 5) // 3
    M := math.Max(3, 5) // 5
    _ = m
    _ = M
    
  • *FormatNumber(f float64) string - Convert a floating number to a human-friendly string without trailing zeros.

    v := pointers.FromFloat(12.3400)
    s := math.FormatNumber(v) // "12.34"
    _ = s
    
  • ChanceOf(p float64) bool - Return true with the given probability using cryptographic randomness.

    if math.ChanceOf(0.1) {
        fmt.Println("10% chance hit")
    }
    

Text

String normalisation helpers.

  • Normalize(s string) string - Trim whitespace from each line and remove empty lines.

    clean := text.Normalize(" Line 1 \n\n  Line 2 ")
    _ = clean
    
  • SanitizeToCamelCase(s string) string - Create a camelCase identifier suitable for HTML IDs.

    id := text.SanitizeToCamelCase("Example Title") // "exampleTitle"
    

System

Helpers for interacting with environment variables.

  • GetEnvOrFail(name string) string - Retrieve a required environment variable or exit the program.

    token := system.GetEnvOrFail("API_TOKEN")
    _ = token
    
  • ExpandEnvVar(s string) (string, error) - Expand $VAR style references and trim the result.

    path, _ := system.ExpandEnvVar("$HOME/tmp")
    

Pointers

Convenience functions for obtaining pointers to primitive values.

  • FromFloat(f float64) *float64 - Return a pointer to the provided float.

    ptr := pointers.FromFloat(3.14)
    _ = ptr
    

Unexported helpers for strings, integers and booleans exist for internal tests.

Scheduler

Retry-aware scheduling helpers.

  • Worker - Runs a periodic scan over pending jobs, applies exponential backoff, and persists attempt results via a repository interface.
  • ClaimingRepository (optional) - Lets repositories atomically claim a job before side effects run; when claim is lost, the worker skips dispatch to avoid duplicate execution under contention.

Testing

The tool includes table-driven tests to ensure consistent behavior for a variety of inputs.

Run Tests:

go test ./test -v

Dependencies

Contributing

Contributions are welcome!

  1. Fork the repository.
  2. Create a new branch (feature/my-feature).
  3. Commit changes and submit a pull request.

License

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

Directories

Path Synopsis
Package crawler provides a reusable crawling service that fetches web pages, applies configurable rules, and emits normalized results.
Package crawler provides a reusable crawling service that fetches web pages, applies configurable rules, and emits normalized results.
Package file provides helpers for common file system interactions such as creating, reading and removing files.
Package file provides helpers for common file system interactions such as creating, reading and removing files.
Package jseval provides headless browser page rendering via chromedp.
Package jseval provides headless browser page rendering via chromedp.
Package llm provides reusable client and factory implementations for chat-based large language model integrations.
Package llm provides reusable client and factory implementations for chat-based large language model integrations.
Package math contains helpers for basic numeric calculations and probability-based utilities.
Package math contains helpers for basic numeric calculations and probability-based utilities.
Package pointers provides helper functions to obtain pointers to basic primitive values.
Package pointers provides helper functions to obtain pointers to basic primitive values.
Package scheduler provides a small retry-aware job runner that can be embedded in services needing persisted scheduling semantics.
Package scheduler provides a small retry-aware job runner that can be embedded in services needing persisted scheduling semantics.
Package system contains utilities for interacting with the host environment such as reading environment variables.
Package system contains utilities for interacting with the host environment such as reading environment variables.
Package text provides utilities for normalising and sanitising strings that are commonly needed when generating text or HTML content.
Package text provides utilities for normalising and sanitising strings that are commonly needed when generating text or HTML content.

Jump to

Keyboard shortcuts

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