generics.go 519 B

12345678910111213141516171819202122
  1. package generics
  2. import "golang.org/x/exp/constraints"
  3. func InitNew[T any](p **T) {
  4. *p = new(T)
  5. }
  6. func SetZero[T any](p *T) {
  7. *p = ZeroValue[T]()
  8. }
  9. func PtrTo[T any](t T) *T {
  10. return &t
  11. }
  12. // Returns a zero-size, zero-allocation slice of the given length that can be used with range to
  13. // loop n times. Also has the advantage of not requiring a loop variable. Similar to bradfitz's
  14. // iter.N, and my clone in anacrolix/missinggo.
  15. func Range[T constraints.Integer](n T) []struct{} {
  16. return make([]struct{}, n)
  17. }