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.
To install the package, use go get:
go get github.com/goloop/gNote: This package requires Go 1.20 or later due to its extensive use of generics.
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 bGo 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)Python makes checking if an element is in a slice easy:
if a in some_slice:
# do somethingThe g package provides an efficient concurrent implementation:
if g.In(a, someSlice...) {
// do something
}- 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)- 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)- 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 })- 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)- 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")- 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)View the complete function documentation
Here are some key function categories:
If- Ternary operator alternativeIn- Check if element exists in sliceAll/Any- Check conditions across valuesIsEmpty/IsWhole/IsEven/IsOdd- Value validation
Min/Max- Find extremesSum/SafeSum- Addition with optional overflow protectionAverage/Median- Statistical calculationsRandom/RandomList- Random value generation
Union/Intersection/Difference/SymmetricDifference- Set operationsSort/Shuffle/Reverse- List manipulationMap/Filter/Reduce- Functional programmingZip/CartesianProduct- List combinations
StringToInt/StringToFloat/StringToBool- String parsingWeed/Preserve/Trim- String cleaningIntToString/FloatToString/BoolToString- Value formatting
StringToDate/DateToString- Date parsing and formattingChangeTimeZone/SetTimeZone/MoveTimeZone- Time zone operations
HLookup/VLookup- Value lookupsRank- Value rankingRange/Rangef- Range generation
Contributions are welcome! Please feel free to submit a Pull Request.