diff --git a/README.md b/README.md index 5b539f5..04c8a66 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,27 @@ # GoKit -[简体中文](https://github.com/fengyuan-liang/GoKit/blob/main/README_ZH.md) +[en](https://github.com/fengyuan-liang/GoKit/blob/main/README_en.md) -GoKit is your ultimate toolbox of utilities for seamless development in Go 😉 +GoKit是您在Go开发中的终极工具箱😉 -## import +## 导入 -```shell +``` go get github.com/fengyuan-liang/GoKit ``` - ## 1. collection -- maps - - EnhancedMap:enhance go raw map - - LinkedHashMap:LinkedHashMap is a data structure that combines the features of a hash table and a linked list, `providing predictable iteration order based on the insertion sequence`. - - HashMap:The map with a underlying data structure lower than a red-black tree. - - TreeMap:TreeMap in Java is a data structure that allows the storage of key-value pairs in a sorted order based on the keys, providing operations like insertion, deletion, and retrieval with logarithmic time complexity. +- maps + - EnhancedMap:增强原生Go map + - LinkedHashMap:LinkedHashMap是将哈希表和链表的特性结合在一起的数据结构,`根据插入顺序提供可预测的迭代顺序`。 + - HashMap:底层数据结构低于红黑树的映射。 + - TreeMap:TreeMap是一种基于红黑树数据结构,它允许按键的排序顺序存储键值对,并提供插入、删除和检索等操作,时间复杂度为对数级别。 - lists - - ArrayList:enhance go slice - - LinkedList:LinkedList is a data structure that implements a sequence of elements using a doubly-linked list as its underlying structure. + - ArrayList:增强Go切片 + - LinkedList:LinkedList是一种使用双向链表作为其底层结构来实现元素序列的数据结构。 -example +示例 ```go func TestLinkedHashMap(t *testing.T) { @@ -38,7 +37,7 @@ func TestLinkedHashMap(t *testing.T) { } ``` -```go +```shell $ go test -run TestLinkedHashMap one: 1 two: 2 @@ -47,11 +46,43 @@ PASS ok GoKit/collection/maps 0.166s ``` +当然也可以完成序列化和反序列化 + +```go +func TestLinkedHashMap_Serialization(t *testing.T) { + // test Marshal + m := NewLinkedHashMap[string, int]() + m.Put("one", 1) + m.Put("two", 2) + m.Put("three", 3) + data, _ := json.Marshal(m) + fmt.Printf("%v\n", string(data)) + // test UnMarshal + m.Clear() + _ = json.Unmarshal([]byte(`{"two":2,"one":1,"three":3}`), &m) + m.ForEach(func(k string, v int) { + fmt.Printf("k:%v, v:%v\n", k, v) + }) +} +``` + +```shell +$ go test -run TestLinkedHashMap_Serialization +{"one":1,"two":2,"three":3} +k:one, v:1 +k:three, v:3 +k:two, v:2 +PASS +ok github.com/fengyuan-liang/GoKit/collection/maps 0.131s +``` + + + ## 2. stream -In Go, there are various ways to manipulate collections, and the `stream` library provides a convenient and practical approach, particularly for those familiar with Java. By leveraging the power of functional programming, the stream package enables seamless operations on collections, allowing for concise and expressive code. With 'stream', developers can effortlessly perform transformations, filtering, mapping, and aggregations on data, simplifying complex data processing tasks and enhancing code readability. +在Go中,有多种方法可以操作集合,而`stream`库提供了一种方便实用的方法,特别适合熟悉Java的人。通过利用函数式编程的威力,stream包能够对集合进行无缝操作,使代码简洁而富有表现力。使用`stream`,开发人员可以轻松进行数据的转换、过滤、映射和聚合,简化复杂的数据处理任务,提高代码的可读性。 -example +示例 ```go func TestStream_Map(t *testing.T) { @@ -74,11 +105,11 @@ ok GoKit/collection/stream 0.00s ## 3. future -Go-future gives an implementation similar to Java/Scala Futures. +Go-future提供了类似于Java/Scala Future的实现。 -Although there are many ways to handle this behaviour in Golang. This library is useful for people who got used to Java/Scala Future implementation. +尽管在Golang中有很多处理此行为的方法,但对于习惯了Java/Scala Future实现的人来说,这个库非常有用。 -example +示例 ```go func TestFutureFunc(t *testing.T) { @@ -86,9 +117,9 @@ func TestFutureFunc(t *testing.T) { time.Sleep(5 * time.Second) return 1 * 10 }) - // do something else here + // 在此处执行其他操作 - // get result when needed + // 在需要时获取结果 result, err := futureFunc.Get() fmt.Printf("result:%v, err:%v\n", result, err) } @@ -103,9 +134,9 @@ ok GoKit/collection/stream 5.177s ## 4. utils -The 'utils' package encompasses a majority of commonly used utility methods in Go development. It provides a comprehensive set of tools that are frequently utilized during the development process. +`utils`包涵盖了Go开发中大多数常用的实用方法。它提供了一套全面的工具,这些工具在开发过程中经常被使用。 -example +示例 ```go func TestSliceToMap(t *testing.T) { @@ -118,7 +149,7 @@ func TestSliceToMap(t *testing.T) { people := make([]*Person, 0) people = append(people, p1, p2, p3) // k:ID v:person - m := utils.SliceToMap(people, func(element *Person) int { return element.ID }) + m:= utils.SliceToMap(people, func(element *Person) int { return element.ID }) fmt.Printf("%v\n", utils.ObjToJsonStr(m.RawMap())) } ``` @@ -129,4 +160,3 @@ $ go test -run TestSliceToMap PASS ok GoKit/utils 0.176s ``` - diff --git a/README_ZH.md b/README_ZH.md deleted file mode 100644 index d03969f..0000000 --- a/README_ZH.md +++ /dev/null @@ -1,128 +0,0 @@ -# GoKit - -GoKit是您在Go开发中的终极工具箱😉 - -## 导入 - -``` -go get github.com/fengyuan-liang/GoKit -``` - -## 1. collection - -- maps - - EnhancedMap:增强原生Go map - - LinkedHashMap:LinkedHashMap是将哈希表和链表的特性结合在一起的数据结构,`根据插入顺序提供可预测的迭代顺序`。 - - HashMap:底层数据结构低于红黑树的映射。 - - TreeMap:TreeMap是一种基于红黑树数据结构,它允许按键的排序顺序存储键值对,并提供插入、删除和检索等操作,时间复杂度为对数级别。 -- lists - - ArrayList:增强Go切片 - - LinkedList:LinkedList是一种使用双向链表作为其底层结构来实现元素序列的数据结构。 - -示例 - -```go -func TestLinkedHashMap(t *testing.T) { - m := maps.NewLinkedHashMap[string, int]() - - m.Put("one", 1) - m.Put("two", 2) - m.Put("three", 3) - - m.ForEach(func(k string, v int) { - t.Logf("%s: %d\n", k, v) - }) -} -``` - -```shell -$ go test -run TestLinkedHashMap -one: 1 -two: 2 -three: 3 -PASS -ok GoKit/collection/maps 0.166s -``` - -## 2. stream - -在Go中,有多种方法可以操作集合,而`stream`库提供了一种方便实用的方法,特别适合熟悉Java的人。通过利用函数式编程的威力,stream包能够对集合进行无缝操作,使代码简洁而富有表现力。使用`stream`,开发人员可以轻松进行数据的转换、过滤、映射和聚合,简化复杂的数据处理任务,提高代码的可读性。 - -示例 - -```go -func TestStream_Map(t *testing.T) { - list := Of[int, int]([]int{1, 2, 3, 4, 5, 6, 7, 8}). - Filter(func(element int) bool { return element%2 == 0 }). - Skip(1). - Limit(10). - Map(func(element int) int { return element * 2 }). - CollectToSlice() - t.Logf("%v\n", list) -} -``` - -```shell -$ go test -run TestStream_Map -[8 12 16] -PASS -ok GoKit/collection/stream 0.00s -``` - -## 3. future - -Go-future提供了类似于Java/Scala Future的实现。 - -尽管在Golang中有很多处理此行为的方法,但对于习惯了Java/Scala Future实现的人来说,这个库非常有用。 - -示例 - -```go -func TestFutureFunc(t *testing.T) { - futureFunc := future.FutureFunc[int](func() int { - time.Sleep(5 * time.Second) - return 1 * 10 - }) - // 在此处执行其他操作 - - // 在需要时获取结果 - result, err := futureFunc.Get() - fmt.Printf("result:%v, err:%v\n", result, err) -} -``` - -```shell -$ go test -run TestFutureFunc -result:10, err: -PASS -ok GoKit/collection/stream 5.177s -``` - -## 4. utils - -`utils`包涵盖了Go开发中大多数常用的实用方法。它提供了一套全面的工具,这些工具在开发过程中经常被使用。 - -示例 - -```go -func TestSliceToMap(t *testing.T) { - type Person struct { - ID int - } - p1 := &Person{ID: 1} - p2 := &Person{ID: 2} - p3 := &Person{ID: 3} - people := make([]*Person, 0) - people = append(people, p1, p2, p3) - // k:ID v:person - m:= utils.SliceToMap(people, func(element *Person) int { return element.ID }) - fmt.Printf("%v\n", utils.ObjToJsonStr(m.RawMap())) -} -``` - -```shell -$ go test -run TestSliceToMap -{"1":{"ID":1},"2":{"ID":2},"3":{"ID":3}} -PASS -ok GoKit/utils 0.176s -``` diff --git a/README_en.md b/README_en.md new file mode 100644 index 0000000..5257b45 --- /dev/null +++ b/README_en.md @@ -0,0 +1,132 @@ +# GoKit + +[简体中文](https://github.com/fengyuan-liang/GoKit/blob/main/README.md) + +GoKit is your ultimate toolbox of utilities for seamless development in Go 😉 + +## import + +```shell +go get github.com/fengyuan-liang/GoKit +``` + + +## 1. collection + +- maps + - EnhancedMap:enhance go raw map + - LinkedHashMap:LinkedHashMap is a data structure that combines the features of a hash table and a linked list, `providing predictable iteration order based on the insertion sequence`. + - HashMap:The map with a underlying data structure lower than a red-black tree. + - TreeMap:TreeMap in Java is a data structure that allows the storage of key-value pairs in a sorted order based on the keys, providing operations like insertion, deletion, and retrieval with logarithmic time complexity. +- lists + - ArrayList:enhance go slice + - LinkedList:LinkedList is a data structure that implements a sequence of elements using a doubly-linked list as its underlying structure. + +example + +```go +func TestLinkedHashMap(t *testing.T) { + m := maps.NewLinkedHashMap[string, int]() + + m.Put("one", 1) + m.Put("two", 2) + m.Put("three", 3) + + m.ForEach(func(k string, v int) { + t.Logf("%s: %d\n", k, v) + }) +} +``` + +```go +$ go test -run TestLinkedHashMap +one: 1 +two: 2 +three: 3 +PASS +ok GoKit/collection/maps 0.166s +``` + +## 2. stream + +In Go, there are various ways to manipulate collections, and the `stream` library provides a convenient and practical approach, particularly for those familiar with Java. By leveraging the power of functional programming, the stream package enables seamless operations on collections, allowing for concise and expressive code. With 'stream', developers can effortlessly perform transformations, filtering, mapping, and aggregations on data, simplifying complex data processing tasks and enhancing code readability. + +example + +```go +func TestStream_Map(t *testing.T) { + list := Of[int, int]([]int{1, 2, 3, 4, 5, 6, 7, 8}). + Filter(func(element int) bool { return element%2 == 0 }). + Skip(1). + Limit(10). + Map(func(element int) int { return element * 2 }). + CollectToSlice() + t.Logf("%v\n", list) +} +``` + +```shell +$ go test -run TestStream_Map +[8 12 16] +PASS +ok GoKit/collection/stream 0.00s +``` + +## 3. future + +Go-future gives an implementation similar to Java/Scala Futures. + +Although there are many ways to handle this behaviour in Golang. This library is useful for people who got used to Java/Scala Future implementation. + +example + +```go +func TestFutureFunc(t *testing.T) { + futureFunc := future.FutureFunc[int](func() int { + time.Sleep(5 * time.Second) + return 1 * 10 + }) + // do something else here + + // get result when needed + result, err := futureFunc.Get() + fmt.Printf("result:%v, err:%v\n", result, err) +} +``` + +```shell +$ go test -run TestFutureFunc +result:10, err: +PASS +ok GoKit/collection/stream 5.177s +``` + +## 4. utils + +The 'utils' package encompasses a majority of commonly used utility methods in Go development. It provides a comprehensive set of tools that are frequently utilized during the development process. + +example + +```go +func TestSliceToMap(t *testing.T) { + type Person struct { + ID int + } + p1 := &Person{ID: 1} + p2 := &Person{ID: 2} + p3 := &Person{ID: 3} + people := make([]*Person, 0) + people = append(people, p1, p2, p3) + // k:ID v:person + m := utils.SliceToMap(people, func(element *Person) int { return element.ID }) + fmt.Printf("%v\n", utils.ObjToJsonStr(m.RawMap())) +} +``` + +```shell +$ go test -run TestSliceToMap +{"1":{"ID":1},"2":{"ID":2},"3":{"ID":3}} +PASS +ok GoKit/utils 0.176s +``` + diff --git a/collection/coll_serialization.go b/collection/coll_serialization.go new file mode 100644 index 0000000..eeb0533 --- /dev/null +++ b/collection/coll_serialization.go @@ -0,0 +1,21 @@ +// Copyright The GoKit authors. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package collection + +// JSONSerializer provides JSON serialization +type JSONSerializer interface { + // ToJSON outputs the JSON representation of collection's elements. + ToJSON() ([]byte, error) + // MarshalJSON @implements json.Marshaler + MarshalJSON() ([]byte, error) +} + +// JSONDeserializer provides JSON deserialization +type JSONDeserializer interface { + // FromJSON populates collection's elements from the input JSON representation. + FromJSON([]byte) error + // UnmarshalJSON @implements json.Unmarshaler + UnmarshalJSON([]byte) error +} diff --git a/collection/heaps/heap_interface.go b/collection/heaps/heap_interface.go new file mode 100644 index 0000000..2014c7f --- /dev/null +++ b/collection/heaps/heap_interface.go @@ -0,0 +1,12 @@ +// Copyright The GoKit authors. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package heaps + +type IHeap[E any] interface { + Size() int + IsEmpty() bool + Clear() + Add(element E) +} diff --git a/collection/lists/ArrayList.go b/collection/lists/ArrayList.go index a654245..52d5c3f 100644 --- a/collection/lists/ArrayList.go +++ b/collection/lists/ArrayList.go @@ -17,9 +17,10 @@ func NewArrayList[E any]() IList[E] { } // NewArrayListWithCapacity creates a new ArrayList with the specified capacity. -func NewArrayListWithCapacity[E any](capacity int) IList[E] { +func NewArrayListWithCapacity[E any](expectedSize int) IList[E] { + checkNonnegative(expectedSize, "initialArraySize") return &ArrayList[E]{ - elements: make([]E, capacity), + elements: make([]E, expectedSize), } } diff --git a/collection/lists/ArrayList_test.go b/collection/lists/ArrayList_test.go index 6ef1783..a9137d0 100644 --- a/collection/lists/ArrayList_test.go +++ b/collection/lists/ArrayList_test.go @@ -103,13 +103,11 @@ func TestNewArrayList_Contains_struct(t *testing.T) { } func TestArrayList_Stream(t *testing.T) { - list := AsList(11, 2, 3, 4).Stream().Filter(func(ele int) bool { - return ele%2 == 0 - }).Map(func(ele int) int { - return ele * 2 - }).Sort(func(o1 int, o2 int) int { - return o1 - o2 - }).CollectToSlice() + list := AsList(11, 2, 3, 4).Stream(). + Filter(func(ele int) bool { return ele%2 == 0 }). + Map(func(ele int) int { return ele * 2 }). + Sort(func(o1 int, o2 int) int { return o1 - o2 }). + CollectToSlice() t.Logf("%v", list) } diff --git a/collection/lists/list_util.go b/collection/lists/list_util.go index e6c8982..da9f510 100644 --- a/collection/lists/list_util.go +++ b/collection/lists/list_util.go @@ -4,6 +4,8 @@ package lists +import "fmt" + func AsList[T any](arr ...T) IList[T] { list := NewArrayList[T]() for _, t := range arr { @@ -11,3 +13,10 @@ func AsList[T any](arr ...T) IList[T] { } return list } + +func checkNonnegative(value int, name string) int { + if value < 0 { + panic(fmt.Sprintf("%s cannot be negative but was: %d", name, value)) + } + return value +} diff --git a/collection/maps/EnhancedMap.go b/collection/maps/EnhancedMap.go index 7bb0256..a2e7f70 100644 --- a/collection/maps/EnhancedMap.go +++ b/collection/maps/EnhancedMap.go @@ -15,6 +15,13 @@ func NewEnhancedMap[K comparable, V any]() IMap[K, V] { } } +// NewEnhancedMapWithExpectedSize creates and returns a new instance of the EnhancedMap. +func NewEnhancedMapWithExpectedSize[K comparable, V any](expectedSize int) IMap[K, V] { + return &EnhancedMap[K, V]{ + baseMap[K, V]{m: make(map[K]V, expectedSize)}, + } +} + // EnhancedMap is a map implementation that satisfies the IMap interface. type EnhancedMap[K comparable, V any] struct { baseMap[K, V] diff --git a/collection/maps/EnhancedMap_serialization.go b/collection/maps/EnhancedMap_serialization.go new file mode 100644 index 0000000..bbaeb06 --- /dev/null +++ b/collection/maps/EnhancedMap_serialization.go @@ -0,0 +1,39 @@ +// Copyright The GoKit authors. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package maps + +import "encoding/json" + +// ToJSON outputs the JSON representation of the map. +func (m *EnhancedMap[K, V]) ToJSON() ([]byte, error) { + copyMap := make(map[K]V, m.Size()) + for k, v := range m.m { + copyMap[k] = v + } + return json.Marshal(©Map) +} + +// FromJSON populates the map from the input JSON representation. +func (m *EnhancedMap[K, V]) FromJSON(data []byte) error { + rawMap := make(map[K]V) + err := json.Unmarshal(data, &rawMap) + if err == nil { + m.Clear() + for k, v := range rawMap { + m.Put(k, v) + } + } + return err +} + +// UnmarshalJSON @implements json.Unmarshaler +func (m *EnhancedMap[K, V]) UnmarshalJSON(bytes []byte) error { + return m.FromJSON(bytes) +} + +// MarshalJSON @implements json.Marshaler +func (m *EnhancedMap[K, V]) MarshalJSON() ([]byte, error) { + return m.ToJSON() +} diff --git a/collection/maps/EnhancedMap_test.go b/collection/maps/EnhancedMap_test.go index 3f28c29..27cb915 100644 --- a/collection/maps/EnhancedMap_test.go +++ b/collection/maps/EnhancedMap_test.go @@ -5,6 +5,8 @@ package maps import ( + "encoding/json" + "fmt" "testing" ) @@ -59,3 +61,19 @@ func TestEnhancedMap(t *testing.T) { } }) } + +func TestEnhancedMapSerialization(t *testing.T) { + // test Marshal + m := NewEnhancedMap[string, int]() + m.Put("one", 1) + m.Put("two", 2) + m.Put("three", 3) + data, _ := json.Marshal(m) + fmt.Printf("%v\n", string(data)) + // test UnMarshal + m.Clear() + _ = json.Unmarshal([]byte(`{"one":1,"three":3,"two":2}`), &m) + m.ForEach(func(k string, v int) { + fmt.Printf("k:%v, v:%v\n", k, v) + }) +} diff --git a/collection/maps/LinkedHashMap.go b/collection/maps/LinkedHashMap.go index ae1e61a..01745ac 100644 --- a/collection/maps/LinkedHashMap.go +++ b/collection/maps/LinkedHashMap.go @@ -13,7 +13,7 @@ func NewLinkedHashMap[K comparable, V any]() IMap[K, V] { } } -func NewLinkedHashMapWithRawMap[K comparable, V any](m map[K]V) IMap[K, V] { +func NewLinkedHashMapWithRawMap[K comparable, V any]() IMap[K, V] { linkedHashMap := &LinkedHashMap[K, V]{ keys: make([]K, 0), baseMap: baseMap[K, V]{ @@ -82,3 +82,8 @@ func (m *LinkedHashMap[K, V]) ForEach(f func(K, V)) { f(k, m.m[k]) } } + +func (m *LinkedHashMap[K, V]) Clear() { + m.m = make(map[K]V) + m.keys = make([]K, 0) +} diff --git a/collection/maps/LinkedHashMap_serialization.go b/collection/maps/LinkedHashMap_serialization.go new file mode 100644 index 0000000..4ac1d83 --- /dev/null +++ b/collection/maps/LinkedHashMap_serialization.go @@ -0,0 +1,67 @@ +// Copyright The GoKit authors. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package maps + +import ( + "bytes" + "encoding/json" +) + +func (m *LinkedHashMap[K, V]) MarshalJSON() ([]byte, error) { + return m.ToJSON() +} + +// ToJSON outputs the JSON representation of map. +func (m *LinkedHashMap[K, V]) ToJSON() ([]byte, error) { + var b []byte + buf := bytes.NewBuffer(b) + + buf.WriteRune('{') + index := 0 + lastIndex := m.Size() - 1 + m.ForEach(func(k K, v V) { + km, err := json.Marshal(k) + if err != nil { + return + } + buf.Write(km) + + buf.WriteRune(':') + + vm, err := json.Marshal(v) + if err != nil { + return + } + buf.Write(vm) + + if index != lastIndex { + buf.WriteRune(',') + } + index++ + }) + + buf.WriteRune('}') + + return buf.Bytes(), nil +} + +// UnmarshalJSON @implements json.Unmarshaler +func (m *LinkedHashMap[K, V]) UnmarshalJSON(bytes []byte) error { + return m.FromJSON(bytes) +} + +func (m *LinkedHashMap[K, V]) FromJSON(data []byte) error { + elements := make(map[K]V) + err := json.Unmarshal(data, &elements) + if err != nil { + return err + } + // resize map + m.Clear() + for k, v := range elements { + m.Put(k, v) + } + return nil +} diff --git a/collection/maps/LinkedHashMap_test.go b/collection/maps/LinkedHashMap_test.go index 3ca214c..f934c3f 100644 --- a/collection/maps/LinkedHashMap_test.go +++ b/collection/maps/LinkedHashMap_test.go @@ -5,6 +5,7 @@ package maps import ( + "encoding/json" "fmt" "testing" ) @@ -76,3 +77,19 @@ func TestLinkedHashMap(t *testing.T) { } } } + +func TestLinkedHashMap_Serialization(t *testing.T) { + // test Marshal + m := NewLinkedHashMap[string, int]() + m.Put("one", 1) + m.Put("two", 2) + m.Put("three", 3) + data, _ := json.Marshal(m) + fmt.Printf("%v\n", string(data)) + // test UnMarshal + m.Clear() + _ = json.Unmarshal([]byte(`{"two":2,"one":1,"three":3}`), &m) + m.ForEach(func(k string, v int) { + fmt.Printf("k:%v, v:%v\n", k, v) + }) +} diff --git a/collection/maps/TreeMap.go b/collection/maps/TreeMap.go index b967070..fc65d89 100644 --- a/collection/maps/TreeMap.go +++ b/collection/maps/TreeMap.go @@ -89,7 +89,7 @@ func (t TreeMap[K, V]) ContainsValue(value V) bool { panic("implement me") } -func (t TreeMap[K, V]) RawMap() map[K]V { +func (t TreeMap[K, V]) Clear() { //TODO implement me panic("implement me") } diff --git a/collection/maps/coll_base_map.go b/collection/maps/coll_base_map.go index b81cb5e..893c30d 100644 --- a/collection/maps/coll_base_map.go +++ b/collection/maps/coll_base_map.go @@ -43,6 +43,6 @@ func (m *baseMap[K, V]) ContainsValue(value V) bool { return false } -func (m *baseMap[K, V]) RawMap() map[K]V { - return m.m +func (m *baseMap[K, V]) Clear() { + m.m = make(map[K]V) } diff --git a/collection/maps/map_interface.go b/collection/maps/map_interface.go index ea5033d..8631610 100644 --- a/collection/maps/map_interface.go +++ b/collection/maps/map_interface.go @@ -43,7 +43,7 @@ type IMap[K comparable, V any] interface { IsEmpty() bool // ForEach applies the specified function to each key-value pair in the map. - ForEach(f func(K, V)) + ForEach(f func(k K, v V)) // ContainsKey checks if the map contains the specified key. // It returns true if the key is found; otherwise, it returns false. @@ -53,5 +53,6 @@ type IMap[K comparable, V any] interface { // It returns true if the value is found; otherwise, it returns false. ContainsValue(value V) bool - RawMap() map[K]V + // Clear removes all elements from the map. + Clear() } diff --git a/go.mod b/go.mod index 8f0bca2..7fa26aa 100644 --- a/go.mod +++ b/go.mod @@ -2,12 +2,14 @@ module github.com/fengyuan-liang/GoKit go 1.19 -require github.com/stretchr/testify v1.8.4 +require ( + github.com/jinzhu/copier v0.4.0 + github.com/json-iterator/go v1.1.12 + github.com/stretchr/testify v1.8.4 +) require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/jinzhu/copier v0.4.0 // indirect - github.com/json-iterator/go v1.1.12 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/go.sum b/go.sum index fae42ad..c4fbd3e 100644 --- a/go.sum +++ b/go.sum @@ -6,7 +6,6 @@ github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8= github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=