GU is a collection of Generic Utility functions, using Type Parameters featured in Go 1.18 and later. I often found myself writing boilerplate code for slices, maps, poitners etc. Since 1.18 I started using generics in some of my repositories and found that some functions often are the same between projects. The repository is a collection of those (utiltity) functions.
Although the functions are pretty basic and almost don't justify putting them in a package, I share this code under the unlicense, with the purpose:
- Make my own life easier when reusing boiler plate code;
- So that others can easily use these utilities;
- People who want to learn more about generics in Go can read the code;
There is no logic in which order I'm adding features. Ussualy when I see repetative code that can be generalized, it is dropped in here. Which means that there might be other utilities that seem to be missing. Contributions are welcome.
Below features link to pkg.go.dev documentation where examples can be found.
Ptr
allows for getting a direct pointer. For example from fuction returns:t := gu.Ptr(time.Unix())
wheret := &time.Unix()
is illigal Go code.Value
safely returns a value through a pointer. When the pointer isnil
, the zero value is returned without panic.
- Transform a slice of any type into a slice of interface (
[]T
to[]interface{}
). - Assert a slice of interfaces to a slice of any type (
[]interface{}
to[]T
). - Transform slices of similar types that implement the Transformer interface.
- Copy a map.
- Copy a map by certain keys only.
- Check if two maps are equal.
Open for Pull Requests.
- In case of a bugfix, please clearly describe the issue and how to reproduce. Preferably a unit test that exposes the behaviour.
- A new feature should be properly documented (godoc), added to REAMDE.md and fully unit tested. If the function seems to be abstract an example needs to be provided in the testfile (
ExampleXxx()
format) - All code needs to be
go fmt
ed
Please note the unlicense: you forfait all copyright when contributing to this repository.