diff --git a/common/clear.go b/common/clear.go new file mode 100644 index 0000000..9768e9c --- /dev/null +++ b/common/clear.go @@ -0,0 +1,11 @@ +//go:build go1.21 + +package common + +func ClearArray[T ~[]E, E any](t T) { + clear(t) +} + +func ClearMap[T ~map[K]V, K comparable, V any](t T) { + clear(t) +} diff --git a/common/clear_compat.go b/common/clear_compat.go new file mode 100644 index 0000000..4e7e9cd --- /dev/null +++ b/common/clear_compat.go @@ -0,0 +1,16 @@ +//go:build !go1.21 + +package common + +func ClearArray[T ~[]E, E any](t T) { + var defaultValue E + for i := range t { + t[i] = defaultValue + } +} + +func ClearMap[T ~map[K]V, K comparable, V any](t T) { + for k := range t { + delete(t, k) + } +}