From 125167055b319b453d2820a90256ee159c5548fa Mon Sep 17 00:00:00 2001 From: qydysky <32743305+qydysky@users.noreply.github.com> Date: Sun, 12 Mar 2023 22:13:46 +0800 Subject: [PATCH] fix --- sync/Map.go | 8 ++++++++ sync/Map_test.go | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/sync/Map.go b/sync/Map.go index fb62b0c..7553fcf 100644 --- a/sync/Map.go +++ b/sync/Map.go @@ -45,3 +45,11 @@ func (t *Map) Copy() (m Map) { }) return } + +func Copy[T comparable, S any](s map[T]S) map[T]S { + t := make(map[T]S) + for k, v := range s { + t[k] = v + } + return t +} diff --git a/sync/Map_test.go b/sync/Map_test.go index 1457b20..a8d468f 100644 --- a/sync/Map_test.go +++ b/sync/Map_test.go @@ -177,6 +177,16 @@ func Test_1(t *testing.T) { } } +func Test_2(t *testing.T) { + var c = make(map[string]int) + c["1"] = 1 + cc := Copy(c) + c["1"] = 2 + if cc["1"] != 1 { + t.Fatal() + } +} + func Benchmark_customMap_Range(b *testing.B) { var c Map var w = &sync.WaitGroup{} -- 2.39.2