From: qydysky <32743305+qydysky@users.noreply.github.com> Date: Sun, 12 Mar 2023 14:13:46 +0000 (+0800) Subject: fix X-Git-Tag: v0.24.3~2 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=125167055b319b453d2820a90256ee159c5548fa;p=part%2F.git fix --- 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{}