Skip to content

goloop/g

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Report Card Go Reference License Stay with Ukraine

g

g is a comprehensive utility library for Go — a flat collection of small, generic, type-safe helpers for the tasks you write over and over: conditionals, collection and set operations, math, string cleaning, date parsing and more.

It favours correctness and predictable performance: hot paths delegate to the standard library (slices, maps, cmp, math/rand/v2), edge cases (empty input, NaN, overflow, degenerate ranges) are handled deterministically instead of panicking, and there is no hidden concurrency.

Features

  • A ternary you can readIf/IfFunc (eager and lazy).
  • CollectionsSort, Reverse, Shuffle, Distinct, Map, Filter, Reduce, Zip, CartesianProduct.
  • Set algebraUnion, Intersection, Difference, SymmetricDifference.
  • MathClamp, Sum/SafeSum (overflow-aware), Average, Median, Min/Max with …Ok and …List variants.
  • RandomnessRandom, RandomList, RandomMap on the concurrency-safe math/rand/v2 source; never panics on degenerate ranges.
  • StringsStringToInt/Float/Bool, Weed/Preserve/Trim with ready-made character-set constants.
  • Date & timeStringToDate, DateToString, time-zone helpers.
  • Spreadsheet helpersHLookup/VLookup, Rank, Range/Rangef.

Installation

go get github.com/goloop/g/v2
import g "github.com/goloop/g/v2"

Requires Go 1.24 or newer. The package has no third-party dependencies.

Quick start

package main

import (
    "fmt"

    g "github.com/goloop/g/v2"
)

func main() {
    // A readable ternary.
    max := g.If(3 > 5, 3, 5) // 5

    // Membership without a manual loop.
    ok := g.In("PATCH", "GET", "POST", "PATCH") // true

    // Functional pipeline.
    nums := []int{3, 1, 4, 1, 5, 9, 2}
    g.Sort(nums)                                        // in place
    evens := g.Filter(nums, func(n int) bool { return n%2 == 0 })
    doubled := g.Map(evens, func(n int) int { return n * 2 })

    // Overflow-aware math.
    sum, err := g.SafeSum(nums...)

    fmt.Println(max, ok, doubled, sum, err)
}

The API at a glance

Area Functions
Logic If, IfFunc, All/Any, In/Contains, Index, IsEmpty, Value
Pointers Ptr, PtrIf
Math Abs, Clamp, Sum/SafeSum, Product, Min/Max (+Ok/List), IsEven/IsOdd/IsWhole
Stats & random Average, Median, Random, RandomList/RandomMap (+Plural)
Collections Sort, Reverse, Shuffle, Distinct, Merge, Zip, CartesianProduct
Functional Map, Filter, Reduce
Sets Union, Intersection, Difference, SymmetricDifference, Complement
Strings StringToInt/Float/Bool, …ToString, Weed/Preserve/Trim
Date & time StringToDate, DateToString(s), ChangeTimeZone/SetTimeZone/MoveTimeZone
Lookup HLookup/VLookup, Rank, Range/Rangef

See DOC.md for the full reference — every function, signatures, examples and recipes (Ukrainian: DOC.UK.md).

Documentation

Contributing

Contributions are welcome. Please run go test ./..., go vet ./... and gofmt -l . before submitting a pull request.

License

g is released under the MIT License. See 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

Contributing

Stars

5 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors

Languages