Skip to content
/ gu Public

Package gu provides Generic Utilities for the Go programming language. These utilities are low in complexity, but I use them frequently in multiple projects. This package depends on type parameter support, available in Go 1.18 and later.

License

Notifications You must be signed in to change notification settings

muhlemmer/gu

Repository files navigation

Generic Utilities

Go Reference Go codecov

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;

Features

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.

Pointers

  • Ptr allows for getting a direct pointer. For example from fuction returns: t := gu.Ptr(time.Unix()) where t := &time.Unix() is illigal Go code.
  • Value safely returns a value through a pointer. When the pointer is nil, the zero value is returned without panic.

Slices

  • 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.

Maps

Contributing

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 fmted

Please note the unlicense: you forfait all copyright when contributing to this repository.

About

Package gu provides Generic Utilities for the Go programming language. These utilities are low in complexity, but I use them frequently in multiple projects. This package depends on type parameter support, available in Go 1.18 and later.

Topics

Resources

License

Stars

Watchers

Forks

Languages