Skip to content

Commit

Permalink
🧨 feature of JSONSerializer and JSONDeserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
fengyuan-liang committed Jun 14, 2024
1 parent 4cab6a6 commit 2b4a35a
Show file tree
Hide file tree
Showing 19 changed files with 402 additions and 172 deletions.
80 changes: 55 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -38,7 +37,7 @@ func TestLinkedHashMap(t *testing.T) {
}
```

```go
```shell
$ go test -run TestLinkedHashMap
one: 1
two: 2
Expand All @@ -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) {
Expand All @@ -74,21 +105,21 @@ 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) {
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)
}
Expand All @@ -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) {
Expand All @@ -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()))
}
```
Expand All @@ -129,4 +160,3 @@ $ go test -run TestSliceToMap
PASS
ok GoKit/utils 0.176s
```

128 changes: 0 additions & 128 deletions README_ZH.md

This file was deleted.

Loading

0 comments on commit 2b4a35a

Please sign in to comment.