|
| 1 | +# go-utils |
| 2 | + |
| 3 | +A collection of generic utility functions for Go projects. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +go get github.com/aykhans/go-utils |
| 9 | +``` |
| 10 | + |
| 11 | +## Packages |
| 12 | + |
| 13 | +### common |
| 14 | + |
| 15 | +Generic type conversion utilities. |
| 16 | + |
| 17 | +**ToPtr** - Convert any value to a pointer |
| 18 | +```go |
| 19 | +import "github.com/aykhans/go-utils/common" |
| 20 | + |
| 21 | +num := 42 |
| 22 | +ptr := common.ToPtr(num) // *int |
| 23 | +``` |
| 24 | + |
| 25 | +### parser |
| 26 | + |
| 27 | +String parsing utilities with generic type support. |
| 28 | + |
| 29 | +**ParseString** - Parse string to various types |
| 30 | +```go |
| 31 | +import "github.com/aykhans/go-utils/parser" |
| 32 | + |
| 33 | +num, err := parser.ParseString[int]("42") |
| 34 | +duration, err := parser.ParseString[time.Duration]("5s") |
| 35 | +isValid, err := parser.ParseString[bool]("true") |
| 36 | +``` |
| 37 | + |
| 38 | +**ParseStringOrZero** - Parse string or return zero value on error |
| 39 | +```go |
| 40 | +num := parser.ParseStringOrZero[int]("invalid") // returns 0 |
| 41 | +``` |
| 42 | + |
| 43 | +**ParseStringWithDefault** - Parse string with default value fallback |
| 44 | +```go |
| 45 | +num, err := parser.ParseStringWithDefault("invalid", 10) // returns 10, error |
| 46 | +``` |
| 47 | + |
| 48 | +**ParseStringOrDefault** - Parse string or return default value without error |
| 49 | +```go |
| 50 | +num := parser.ParseStringOrDefault("invalid", 10) // returns 10 |
| 51 | +``` |
| 52 | + |
| 53 | +Supported types: `string`, `int`, `int8`, `int16`, `int32`, `int64`, `uint`, `uint8`, `uint16`, `uint32`, `uint64`, `float64`, `bool`, `time.Duration`, `url.URL` |
| 54 | + |
| 55 | +### slice |
| 56 | + |
| 57 | +Slice manipulation utilities. |
| 58 | + |
| 59 | +**Cycle** - Create an infinite cycler through items |
| 60 | +```go |
| 61 | +import "github.com/aykhans/go-utils/slice" |
| 62 | + |
| 63 | +next := slice.Cycle(1, 2, 3) |
| 64 | +fmt.Println(next()) // 1 |
| 65 | +fmt.Println(next()) // 2 |
| 66 | +fmt.Println(next()) // 3 |
| 67 | +fmt.Println(next()) // 1 |
| 68 | +``` |
| 69 | + |
| 70 | +**RandomCycle** - Cycle through items with randomization |
| 71 | +```go |
| 72 | +next := slice.RandomCycle(nil, "a", "b", "c") |
| 73 | +// Cycles through all items, then starts from a random position |
| 74 | +``` |
| 75 | + |
| 76 | +### maps |
| 77 | + |
| 78 | +Map utility functions. |
| 79 | + |
| 80 | +**InitMap** - Initialize a map pointer if nil |
| 81 | +```go |
| 82 | +import "github.com/aykhans/go-utils/maps" |
| 83 | + |
| 84 | +var m map[string]int |
| 85 | +maps.InitMap(&m) |
| 86 | +m["key"] = 42 // safe to use |
| 87 | +``` |
| 88 | + |
| 89 | +**UpdateMap** - Merge entries from one map into another |
| 90 | +```go |
| 91 | +old := map[string]int{"a": 1, "b": 2} |
| 92 | +new := map[string]int{"b": 3, "c": 4} |
| 93 | +maps.UpdateMap(&old, new) |
| 94 | +// old is now: {"a": 1, "b": 3, "c": 4} |
| 95 | +``` |
| 96 | + |
| 97 | +### errors |
| 98 | + |
| 99 | +Advanced error handling utilities. |
| 100 | + |
| 101 | +**HandleError** - Process errors with custom matchers |
| 102 | +```go |
| 103 | +import "github.com/aykhans/go-utils/errors" |
| 104 | + |
| 105 | +handled, result := errors.HandleError(err, |
| 106 | + errors.OnSentinelError(io.EOF, func(e error) error { |
| 107 | + return nil // EOF is expected, ignore it |
| 108 | + }), |
| 109 | + errors.OnCustomError(func(e *CustomError) error { |
| 110 | + return fmt.Errorf("custom error: %w", e) |
| 111 | + }), |
| 112 | +) |
| 113 | +``` |
| 114 | + |
| 115 | +**HandleErrorOrDie** - Handle errors or panic if unhandled |
| 116 | +```go |
| 117 | +result := errors.HandleErrorOrDie(err, |
| 118 | + errors.OnSentinelError(context.Canceled, func(e error) error { |
| 119 | + return fmt.Errorf("operation canceled") |
| 120 | + }), |
| 121 | +) // Panics if err doesn't match any handler |
| 122 | +``` |
| 123 | + |
| 124 | +**OnSentinelError** - Create matcher for sentinel errors (like `io.EOF`) |
| 125 | +```go |
| 126 | +matcher := errors.OnSentinelError(io.EOF, func(e error) error { |
| 127 | + log.Println("reached end of file") |
| 128 | + return nil |
| 129 | +}) |
| 130 | +``` |
| 131 | + |
| 132 | +**OnCustomError** - Create matcher for custom error types |
| 133 | +```go |
| 134 | +type ValidationError struct { |
| 135 | + Field string |
| 136 | + Msg string |
| 137 | +} |
| 138 | + |
| 139 | +matcher := errors.OnCustomError(func(e *ValidationError) error { |
| 140 | + log.Printf("validation failed on field %s", e.Field) |
| 141 | + return fmt.Errorf("invalid input: %w", e) |
| 142 | +}) |
| 143 | +``` |
| 144 | + |
| 145 | +## Requirements |
| 146 | + |
| 147 | +- Go 1.25.0 or higher |
0 commit comments