diff --git a/docs/convertor.md b/docs/convertor.md index 5c00488..4783e56 100644 --- a/docs/convertor.md +++ b/docs/convertor.md @@ -24,13 +24,16 @@ import ( - [ToBool](#ToBool) - [ToBytes](#ToBytes) - [ToChar](#ToChar) +- [ToChannel](#ToChannel) - [ToFloat](#ToFloat) - [ToInt](#ToInt) - [ToJson](#ToJson) +- [ToMap](#ToMap) - [ToPointer](#ToPointer) - [ToString](#ToString) - [StructToMap](#StructToMap) +- [MapToSlice](#MapToSlice)
@@ -194,6 +197,43 @@ func main() { ``` +### ToChannel + +Convert a collection of elements to a read-only channels.
+ +Signature: + +```go +func ToChannel[T any](array []T) <-chan T +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + ch := convertor.ToChannel([]int{1, 2, 3}) + + val1, _ := <-ch + fmt.Println(val1) //1 + + val2, _ := <-ch + fmt.Println(val2) //2 + + val3, _ := <-ch + fmt.Println(val3) //3 + + _, ok := <-ch + fmt.Println(ok) //false +} +``` + + ### ToFloat @@ -289,14 +329,14 @@ func main() { -### ToJson +### ToMap -Convert interface to json string. If param can't be converted, will return "" and error.
+Convert a slice or an array of structs to a map based on iteratee function.
Signature: ```go -func ToJson(value any) (string, error) +func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V ``` Example: @@ -309,9 +349,19 @@ import ( ) func main() { - var aMap = map[string]int{"a": 1, "b": 2, "c": 3} - jsonStr, _ := convertor.ToJson(aMap) - fmt.Printf("%q", jsonStr) //"{\"a\":1,\"b\":2,\"c\":3}" + type Message struct { + name string + code int + } + messages := []Message{ + {name: "Hello", code: 100}, + {name: "Hi", code: 101}, + } + result := convertor.ToMap(messages, func(msg Message) (int, string) { + return msg.code, msg.name + }) + + fmt.Println(result) //{100: "Hello", 101: "Hi"} } ``` @@ -376,4 +426,35 @@ func main() { fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test] } +``` + + + +### MapToSlice + +Convert a map to a slice based on iteratee function.
+ +Signature: + +```go +func MapToSlice[T any, K comparable, V any](aMap map[K]V, iteratee func(K, V) T) []T +``` +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + aMap := map[string]int{"a": 1, "b": 2, "c": 3} + result := MapToSlice(aMap, func(key string, value int) string { + return key + ":" + strconv.Itoa(value) + }) + + fmt.Println(result) //[]string{"a:1", "b:2", "c:3"} +} ``` \ No newline at end of file diff --git a/docs/convertor_zh-CN.md b/docs/convertor_zh-CN.md index 125b44c..3fcb54e 100644 --- a/docs/convertor_zh-CN.md +++ b/docs/convertor_zh-CN.md @@ -26,13 +26,16 @@ import ( - [ToBool](#ToBool) - [ToBytes](#ToBytes) - [ToChar](#ToChar) +- [ToChannel](#ToChannel) - [ToFloat](#ToFloat) - [ToInt](#ToInt) - [ToJson](#ToJson) +- [ToMap](#ToMap) - [ToPointer](#ToPointer) - [ToString](#ToString) - [StructToMap](#StructToMap) +- [MapToSlice](#MapToSlice) @@ -197,6 +200,44 @@ func main() { +### ToChannel + +将切片转为只读channel
+ +函数签名: + +```go +func ToChannel[T any](array []T) <-chan T +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + ch := convertor.ToChannel([]int{1, 2, 3}) + + val1, _ := <-ch + fmt.Println(val1) //1 + + val2, _ := <-ch + fmt.Println(val2) //2 + + val3, _ := <-ch + fmt.Println(val3) //3 + + _, ok := <-ch + fmt.Println(ok) //false +} +``` + + + ### ToFloat将interface转成float64类型,如果参数无法转换,会返回0和error
@@ -291,6 +332,44 @@ func main() { +### ToMap + +将切片转为map
+ +函数签名: + +```go +func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + type Message struct { + name string + code int + } + messages := []Message{ + {name: "Hello", code: 100}, + {name: "Hi", code: 101}, + } + result := convertor.ToMap(messages, func(msg Message) (int, string) { + return msg.code, msg.name + }) + + fmt.Println(result) //{100: "Hello", 101: "Hi"} +} +``` + + + ### ToPointer返回传入值的指针
@@ -378,4 +457,35 @@ func main() { fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test] } +``` + + + +### MapToSlice + +map中key和value执行函数iteratee后,转为切片
+ +函数签名: + +```go +func MapToSlice[T any, K comparable, V any](aMap map[K]V, iteratee func(K, V) T) []T +``` +例子: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/v2/convertor" +) + +func main() { + aMap := map[string]int{"a": 1, "b": 2, "c": 3} + result := MapToSlice(aMap, func(key string, value int) string { + return key + ":" + strconv.Itoa(value) + }) + + fmt.Println(result) //[]string{"a:1", "b:2", "c:3"} +} ``` \ No newline at end of file