From 2a2dbf19716a7f60a05ed2f171cbbdd068db5867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 9 Dec 2023 10:24:45 +0800 Subject: [PATCH] Add compat func for `clear` --- common/clear.go | 11 +++++++++++ common/clear_compat.go | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 common/clear.go create mode 100644 common/clear_compat.go 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) + } +}