Skip to content
/ g Public

The do package is a Go utility package that provides common and safe helper functions like short If or checking if a value is null, etc.

License

Notifications You must be signed in to change notification settings

goloop/g

Folders and files

NameName
Last commit message
Last commit date

Latest commit

c6f587e · Dec 1, 2024

History

63 Commits
Dec 1, 2024
Dec 1, 2024
Dec 1, 2024
Dec 1, 2024
May 29, 2023
Dec 1, 2024
Dec 1, 2024
Dec 1, 2024
Aug 20, 2024
Aug 20, 2024
Dec 1, 2024
Dec 1, 2024
Dec 1, 2024
Dec 1, 2024
Jun 5, 2023
Jun 4, 2023
Dec 1, 2024
Aug 20, 2024
Dec 1, 2024
Dec 1, 2024
Dec 1, 2024
Dec 1, 2024
Dec 1, 2024
Aug 20, 2024
Jul 2, 2023
Jun 13, 2024
Dec 1, 2024
Dec 1, 2024
Jun 13, 2024
Jun 13, 2024
Dec 1, 2024
Dec 1, 2024
Jun 4, 2023
Jun 4, 2023
Jun 15, 2023
Jun 15, 2023

Repository files navigation

Go Report Card License License Stay with Ukraine

G

The g package is a comprehensive utility library for Go that provides a rich collection of generic helper functions to streamline common programming tasks. Built with modern Go features, particularly generics, this package offers efficient, reliable, and easy-to-use solutions for everyday development challenges.

Installation

To install the package, use go get:

go get github.com/goloop/g

Note: This package requires Go 1.20 or later due to its extensive use of generics.

Why This Package?

Ternary Operator Alternative

In languages like C++ and Python, you can write concise conditional expressions:

// C++
int max = (a > b) ? a : b;

// Python
max = a if a > b else b

Go doesn't have a ternary operator, leading to verbose code:

max := a
if a < b {
    max = b
}

With the g package, you can write:

max := g.If(a > b, a, b)

Efficient List Operations

Python makes checking if an element is in a slice easy:

if a in some_slice:
    # do something

The g package provides an efficient concurrent implementation:

if g.In(a, someSlice...) {
    // do something
}

Key Features

Type Conversion & Validation

  • String to various types (bool, int, float)
  • Type checking and verification
  • Safe numeric conversions with overflow protection
// String to int conversion with default value
num, err := g.StringToInt("123", 0)

// Safe sum with overflow protection
sum, err := g.SafeSum(1, math.MaxInt64)

Mathematical Operations

  • Basic arithmetic with overflow protection
  • Statistical functions (Average, Median)
  • Random number generation
  • Number properties (Even, Odd, Whole)
avg := g.Average(1, 2, 3, 4, 5)
median := g.Median(1, 2, 3, 4, 5)
random := g.Random(1, 10)

Collection Operations

  • Set operations (Union, Intersection, Difference)
  • List manipulation (Sort, Shuffle, Reverse)
  • Functional programming helpers (Map, Filter, Reduce)
unique := g.Union(slice1, slice2)
g.Sort(numbers)
doubled := g.Map(numbers, func(n int) int { return n * 2 })

String Processing

  • Character filtering and preservation
  • String cleaning and normalization
  • Pattern-based manipulation
// Remove unwanted characters
cleaned := g.Weed("Hello\t World\n")

// Keep only specific characters
numbers := g.Preserve("+1-234-567-8900", g.Numbers)

Date & Time

  • Flexible date parsing
  • Time zone manipulation
  • Python-style date formatting
date, err := g.StringToDate("2023-12-01")
newTime, err := g.ChangeTimeZone(time.Now(), "America/New_York")

Excel-like Functions

  • HLOOKUP/VLOOKUP implementations
  • Range operations
  • Value ranking
rank := g.Rank(7, []float64{1, 5, 2, 3, 7, 8})
value := g.HLookup("key", lookupSlice, resultSlice, defaultValue)

Complete Function List

View the complete function documentation

Here are some key function categories:

Basic Operations

  • If - Ternary operator alternative
  • In - Check if element exists in slice
  • All/Any - Check conditions across values
  • IsEmpty/IsWhole/IsEven/IsOdd - Value validation

Mathematical

  • Min/Max - Find extremes
  • Sum/SafeSum - Addition with optional overflow protection
  • Average/Median - Statistical calculations
  • Random/RandomList - Random value generation

Collection Operations

  • Union/Intersection/Difference/SymmetricDifference - Set operations
  • Sort/Shuffle/Reverse - List manipulation
  • Map/Filter/Reduce - Functional programming
  • Zip/CartesianProduct - List combinations

String Operations

  • StringToInt/StringToFloat/StringToBool - String parsing
  • Weed/Preserve/Trim - String cleaning
  • IntToString/FloatToString/BoolToString - Value formatting

Date & Time

  • StringToDate/DateToString - Date parsing and formatting
  • ChangeTimeZone/SetTimeZone/MoveTimeZone - Time zone operations

Excel-like Functions

  • HLookup/VLookup - Value lookups
  • Rank - Value ranking
  • Range/Rangef - Range generation

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License

About

The do package is a Go utility package that provides common and safe helper functions like short If or checking if a value is null, etc.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages