EdgexAgent/device-ble-go/vendor/github.com/muhlemmer/gu
2025-07-10 20:40:32 +08:00
..
gu.go 添加gitignore 2025-07-10 20:40:32 +08:00
LICENSE 添加gitignore 2025-07-10 20:40:32 +08:00
map.go 添加gitignore 2025-07-10 20:40:32 +08:00
pointer.go 添加gitignore 2025-07-10 20:40:32 +08:00
README.md 添加gitignore 2025-07-10 20:40:32 +08:00
slice.go 添加gitignore 2025-07-10 20:40:32 +08:00

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.