This library provides a collection of utility functions designed to simplify common tasks and improve code robustness. It includes a main tool package and a safetool sub-package, where functions are designed to return errors instead of panicking.
- Convenient Utilities: A collection of helper functions for various tasks.
- Safe Alternatives: The
safetoolsub-package offers functions that return errors, promoting more resilient error handling. - Well-Tested: Includes a comprehensive test suite to ensure reliability.
To install the tool package, use go get:
go get -u github.com/iamwavecut/toolThis repository contains the following main packages:
The main package containing various utility functions. These functions will return zero values or errors instead of panicking on unexpected input or errors.
The safetool package provides alternative implementations of functions found in the tool package. These "safe" versions are designed to return an error instead of muting errors, allowing for more controlled error handling in your applications.
In(needle, ...haystack) bool- deprecated in favor ofslices.ContainsIsZero(comparable) bool- returns true if the value is the zero value for its typeNilPtr(comparable) *comparable- returns a pointer to the input value if it's not zero, otherwise returns nilNonZero(comparable) comparable- returns the first non-zero value from the input argumentsPtr(any) *any- returns a pointer to the input valueRetryFunc(attempts int, sleep time.Duration, f func() error) error- retries a function with exponential backoffStrtr(subject string, oldToNew map[string]string) string- replaces substrings in a string based on a mappingVal(*any) any- returns the value pointed to by the pointer, or zero value if pointer is nilZeroVal(any) any- returns the zero value of the type, regardless of the input value
Catch(func(error))- catches panics and calls a function with the errorConsole(...any)- prints to the consoleConvertSlice([]T, Y) []Y- converts a slice of one type to anotherErr(...any) error- returns an error with the input argumentsExecTemplate(string, []|map) string- executes a templateJsonify(any) safetool.Varchar- returns a Varchar from the input valueMultiMute(...any) []any- returns a slice of the input arguments with no latest errorMust(error, verbose ?bool)- panics if the error is not nilMustReturn(any, error) any- panics if the error is not nilObjectify(in any, target any) bool- decodes a JSON string into the target objectRandInt(min, max int) int- returns a random integer between min and maxRecoverer(maxPanics int, f func(), jobID ...string) error- recovers from panics and returns the errorReturn(any, _ error) any- returns the input valueSetLogger(l StdLogger)- sets the loggerTry(err error, verbose ?bool) bool- returns true if the error is nil
ConvertSlice([]T, Y) ([]Y, error)- converts a slice of one type to anotherExecTemplate(string, []|map) (string, error)- executes a templateFindRootCaller() string- returns the root caller filepath of the applicationGetRelativePath(string) (string, error)- returns a relative path from the directory of the root caller to the given filePathJsonify(any) (safetool.Varchar, error)- returns an encoded JSON string from the input value represented as aVarcharObjectify(in any, target any) error- decodes a JSON string into the target objectRandInt(min, max int) (int, error)- returns a random integer between min and maxZero(any) any- returns the zero value for the input type
Contributions are welcome! Please feel free to submit a pull request or open an issue if you have suggestions or find a bug.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License.