1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-12 16:52:29 +08:00

doc: format document

This commit is contained in:
dudaodong
2023-01-13 10:57:40 +08:00
parent b422d98702
commit 5f2c3edff4
3 changed files with 264 additions and 266 deletions

View File

@@ -85,31 +85,31 @@ func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
c := concurrency.NewChannel[int]() c := concurrency.NewChannel[int]()
genVals := func() <-chan <-chan int { genVals := func() <-chan <-chan int {
out := make(chan (<-chan int)) out := make(chan (<-chan int))
go func() { go func() {
defer close(out) defer close(out)
for i := 1; i <= 5; i++ { for i := 1; i <= 5; i++ {
stream := make(chan int, 1) stream := make(chan int, 1)
stream <- i stream <- i
close(stream) close(stream)
out <- stream out <- stream
} }
}() }()
return out return out
} }
for v := range c.Bridge(ctx, genVals()) { for v := range c.Bridge(ctx, genVals()) {
fmt.Println(v) fmt.Println(v)
} }
// Output: // Output:
// 1 // 1
// 2 // 2
// 3 // 3
// 4 // 4
// 5 // 5
} }
``` ```

View File

@@ -60,12 +60,12 @@ import (
func main() { func main() {
colorHex := "#003366" colorHex := "#003366"
r, g, b := convertor.ColorHexToRGB(colorHex) r, g, b := convertor.ColorHexToRGB(colorHex)
fmt.Println(r, g, b) fmt.Println(r, g, b)
// Output: // Output:
// 0 51 102 // 0 51 102
} }
``` ```
@@ -90,14 +90,14 @@ import (
func main() { func main() {
r := 0 r := 0
g := 51 g := 51
b := 102 b := 102
colorHex := ColorRGBToHex(r, g, b) colorHex := ColorRGBToHex(r, g, b)
fmt.Println(colorHex) fmt.Println(colorHex)
// Output: // Output:
// #003366 // #003366
} }
``` ```
@@ -123,21 +123,21 @@ import (
func main() { func main() {
cases := []string{"1", "true", "True", "false", "False", "0", "123", "0.0", "abc"} cases := []string{"1", "true", "True", "false", "False", "0", "123", "0.0", "abc"}
for i := 0; i < len(cases); i++ { for i := 0; i < len(cases); i++ {
result, _ := convertor.ToBool(cases[i]) result, _ := convertor.ToBool(cases[i])
fmt.Println(result) fmt.Println(result)
} }
// Output: // Output:
// true // true
// true // true
// true // true
// false // false
// false // false
// false // false
// false // false
// false // false
// false // false
} }
``` ```
@@ -169,7 +169,7 @@ func main() {
fmt.Println(bytesData) fmt.Println(bytesData)
// Output: // Output:
// [97 98 99] // [97 98 99]
} }
``` ```
@@ -194,17 +194,17 @@ import (
func main() { func main() {
result1 := convertor.ToChar("") result1 := convertor.ToChar("")
result2 := convertor.ToChar("abc") result2 := convertor.ToChar("abc")
result3 := convertor.ToChar("1 2#3") result3 := convertor.ToChar("1 2#3")
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
fmt.Println(result3) fmt.Println(result3)
// Output: // Output:
// [] // []
// [a b c] // [a b c]
// [1 2 # 3] // [1 2 # 3]
} }
``` ```
@@ -229,18 +229,18 @@ import (
func main() { func main() {
ch := convertor.ToChannel([]int{1, 2, 3}) ch := convertor.ToChannel([]int{1, 2, 3})
result1 := <-ch result1 := <-ch
result2 := <-ch result2 := <-ch
result3 := <-ch result3 := <-ch
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
fmt.Println(result3) fmt.Println(result3)
// Output: // Output:
// 1 // 1
// 2 // 2
// 3 // 3
} }
``` ```
@@ -265,26 +265,26 @@ import (
func main() { func main() {
result1, _ := convertor.ToFloat("") result1, _ := convertor.ToFloat("")
result2, err := convertor.ToFloat("abc") result2, err := convertor.ToFloat("abc")
result3, _ := convertor.ToFloat("-1") result3, _ := convertor.ToFloat("-1")
result4, _ := convertor.ToFloat("-.11") result4, _ := convertor.ToFloat("-.11")
result5, _ := convertor.ToFloat("1.23e3") result5, _ := convertor.ToFloat("1.23e3")
result6, _ := convertor.ToFloat(true) result6, _ := convertor.ToFloat(true)
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2, err) fmt.Println(result2, err)
fmt.Println(result3) fmt.Println(result3)
fmt.Println(result4) fmt.Println(result4)
fmt.Println(result5) fmt.Println(result5)
fmt.Println(result6) fmt.Println(result6)
// Output: // Output:
// 0 // 0
// 0 strconv.ParseFloat: parsing "": invalid syntax // 0 strconv.ParseFloat: parsing "": invalid syntax
// -1 // -1
// -0.11 // -0.11
// 1230 // 1230
// 0 // 0
} }
``` ```
@@ -309,23 +309,23 @@ import (
func main() { func main() {
result1, _ := convertor.ToInt("123") result1, _ := convertor.ToInt("123")
result2, _ := convertor.ToInt("-123") result2, _ := convertor.ToInt("-123")
result3, _ := convertor.ToInt(float64(12.3)) result3, _ := convertor.ToInt(float64(12.3))
result4, err := convertor.ToInt("abc") result4, err := convertor.ToInt("abc")
result5, _ := convertor.ToInt(true) result5, _ := convertor.ToInt(true)
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
fmt.Println(result3) fmt.Println(result3)
fmt.Println(result4, err) fmt.Println(result4, err)
fmt.Println(result5) fmt.Println(result5)
// Output: // Output:
// 123 // 123
// -123 // -123
// 12 // 12
// 0 strconv.ParseInt: parsing "": invalid syntax // 0 strconv.ParseInt: parsing "": invalid syntax
// 0 // 0
} }
``` ```
@@ -350,16 +350,16 @@ import (
func main() { func main() {
aMap := map[string]int{"a": 1, "b": 2, "c": 3} aMap := map[string]int{"a": 1, "b": 2, "c": 3}
result, err := ToJson(aMap) result, err := ToJson(aMap)
if err != nil { if err != nil {
fmt.Printf("%v", err) fmt.Printf("%v", err)
} }
fmt.Println(result) fmt.Println(result)
// Output: // Output:
// {"a":1,"b":2,"c":3} // {"a":1,"b":2,"c":3}
} }
``` ```
@@ -399,7 +399,7 @@ func main() {
fmt.Println(result) fmt.Println(result)
// Output: // Output:
// map[100:Hello 101:Hi] // map[100:Hello 101:Hi]
} }
``` ```
@@ -427,7 +427,7 @@ func main() {
fmt.Println(*result) fmt.Println(*result)
// Output: // Output:
// 123 // 123
} }
``` ```
@@ -452,29 +452,29 @@ import (
func main() { func main() {
result1 := convertor.ToString("") result1 := convertor.ToString("")
result2 := convertor.ToString(nil) result2 := convertor.ToString(nil)
result3 := convertor.ToString(0) result3 := convertor.ToString(0)
result4 := convertor.ToString(1.23) result4 := convertor.ToString(1.23)
result5 := convertor.ToString(true) result5 := convertor.ToString(true)
result6 := convertor.ToString(false) result6 := convertor.ToString(false)
result7 := convertor.ToString([]int{1, 2, 3}) result7 := convertor.ToString([]int{1, 2, 3})
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
fmt.Println(result3) fmt.Println(result3)
fmt.Println(result4) fmt.Println(result4)
fmt.Println(result5) fmt.Println(result5)
fmt.Println(result6) fmt.Println(result6)
fmt.Println(result7) fmt.Println(result7)
// Output: // Output:
// //
// //
// 0 // 0
// 1.23 // 1.23
// true // true
// false // false
// [1,2,3] // [1,2,3]
} }
``` ```
@@ -499,24 +499,22 @@ import (
func main() { func main() {
type People struct { type People struct {
Name string `json:"name"` Name string `json:"name"`
age int age int
} }
p := People{ p := People{
"test", "test",
100, 100,
} }
pm, _ := convertor.StructToMap(p) pm, _ := convertor.StructToMap(p)
fmt.Println(pm) fmt.Println(pm)
// Output: // Output:
// map[name:test] // map[name:test]
} }
``` ```
### <span id="MapToSlice">MapToSlice</span> ### <span id="MapToSlice">MapToSlice</span>
<p>Convert a map to a slice based on iteratee function.</p> <p>Convert a map to a slice based on iteratee function.</p>
@@ -571,7 +569,7 @@ func main() {
fmt.Println(byteData) fmt.Println(byteData)
// Output: // Output:
// [6 12 0 3 97 98 99] // [6 12 0 3 97 98 99]
} }
``` ```
@@ -596,16 +594,16 @@ import (
func main() { func main() {
var result string var result string
byteData := []byte{6, 12, 0, 3, 97, 98, 99} byteData := []byte{6, 12, 0, 3, 97, 98, 99}
err := convertor.DecodeByte(byteData, &result) err := convertor.DecodeByte(byteData, &result)
if err != nil { if err != nil {
return return
} }
fmt.Println(result) fmt.Println(result)
// Output: // Output:
// abc // abc
} }
``` ```

View File

@@ -63,12 +63,12 @@ import (
func main() { func main() {
colorHex := "#003366" colorHex := "#003366"
r, g, b := convertor.ColorHexToRGB(colorHex) r, g, b := convertor.ColorHexToRGB(colorHex)
fmt.Println(r, g, b) fmt.Println(r, g, b)
// Output: // Output:
// 0 51 102 // 0 51 102
} }
``` ```
@@ -93,14 +93,14 @@ import (
func main() { func main() {
r := 0 r := 0
g := 51 g := 51
b := 102 b := 102
colorHex := ColorRGBToHex(r, g, b) colorHex := ColorRGBToHex(r, g, b)
fmt.Println(colorHex) fmt.Println(colorHex)
// Output: // Output:
// #003366 // #003366
} }
``` ```
@@ -126,21 +126,21 @@ import (
func main() { func main() {
cases := []string{"1", "true", "True", "false", "False", "0", "123", "0.0", "abc"} cases := []string{"1", "true", "True", "false", "False", "0", "123", "0.0", "abc"}
for i := 0; i < len(cases); i++ { for i := 0; i < len(cases); i++ {
result, _ := convertor.ToBool(cases[i]) result, _ := convertor.ToBool(cases[i])
fmt.Println(result) fmt.Println(result)
} }
// Output: // Output:
// true // true
// true // true
// true // true
// false // false
// false // false
// false // false
// false // false
// false // false
// false // false
} }
``` ```
@@ -172,7 +172,7 @@ func main() {
fmt.Println(bytesData) fmt.Println(bytesData)
// Output: // Output:
// [97 98 99] // [97 98 99]
} }
``` ```
@@ -197,17 +197,17 @@ import (
func main() { func main() {
result1 := convertor.ToChar("") result1 := convertor.ToChar("")
result2 := convertor.ToChar("abc") result2 := convertor.ToChar("abc")
result3 := convertor.ToChar("1 2#3") result3 := convertor.ToChar("1 2#3")
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
fmt.Println(result3) fmt.Println(result3)
// Output: // Output:
// [] // []
// [a b c] // [a b c]
// [1 2 # 3] // [1 2 # 3]
} }
``` ```
@@ -232,18 +232,18 @@ import (
func main() { func main() {
ch := convertor.ToChannel([]int{1, 2, 3}) ch := convertor.ToChannel([]int{1, 2, 3})
result1 := <-ch result1 := <-ch
result2 := <-ch result2 := <-ch
result3 := <-ch result3 := <-ch
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
fmt.Println(result3) fmt.Println(result3)
// Output: // Output:
// 1 // 1
// 2 // 2
// 3 // 3
} }
``` ```
@@ -268,26 +268,26 @@ import (
func main() { func main() {
result1, _ := convertor.ToFloat("") result1, _ := convertor.ToFloat("")
result2, err := convertor.ToFloat("abc") result2, err := convertor.ToFloat("abc")
result3, _ := convertor.ToFloat("-1") result3, _ := convertor.ToFloat("-1")
result4, _ := convertor.ToFloat("-.11") result4, _ := convertor.ToFloat("-.11")
result5, _ := convertor.ToFloat("1.23e3") result5, _ := convertor.ToFloat("1.23e3")
result6, _ := convertor.ToFloat(true) result6, _ := convertor.ToFloat(true)
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2, err) fmt.Println(result2, err)
fmt.Println(result3) fmt.Println(result3)
fmt.Println(result4) fmt.Println(result4)
fmt.Println(result5) fmt.Println(result5)
fmt.Println(result6) fmt.Println(result6)
// Output: // Output:
// 0 // 0
// 0 strconv.ParseFloat: parsing "": invalid syntax // 0 strconv.ParseFloat: parsing "": invalid syntax
// -1 // -1
// -0.11 // -0.11
// 1230 // 1230
// 0 // 0
} }
``` ```
@@ -312,23 +312,23 @@ import (
func main() { func main() {
result1, _ := convertor.ToInt("123") result1, _ := convertor.ToInt("123")
result2, _ := convertor.ToInt("-123") result2, _ := convertor.ToInt("-123")
result3, _ := convertor.ToInt(float64(12.3)) result3, _ := convertor.ToInt(float64(12.3))
result4, err := convertor.ToInt("abc") result4, err := convertor.ToInt("abc")
result5, _ := convertor.ToInt(true) result5, _ := convertor.ToInt(true)
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
fmt.Println(result3) fmt.Println(result3)
fmt.Println(result4, err) fmt.Println(result4, err)
fmt.Println(result5) fmt.Println(result5)
// Output: // Output:
// 123 // 123
// -123 // -123
// 12 // 12
// 0 strconv.ParseInt: parsing "": invalid syntax // 0 strconv.ParseInt: parsing "": invalid syntax
// 0 // 0
} }
``` ```
@@ -353,16 +353,16 @@ import (
func main() { func main() {
aMap := map[string]int{"a": 1, "b": 2, "c": 3} aMap := map[string]int{"a": 1, "b": 2, "c": 3}
result, err := ToJson(aMap) result, err := ToJson(aMap)
if err != nil { if err != nil {
fmt.Printf("%v", err) fmt.Printf("%v", err)
} }
fmt.Println(result) fmt.Println(result)
// Output: // Output:
// {"a":1,"b":2,"c":3} // {"a":1,"b":2,"c":3}
} }
``` ```
@@ -402,7 +402,7 @@ func main() {
fmt.Println(result) fmt.Println(result)
// Output: // Output:
// map[100:Hello 101:Hi] // map[100:Hello 101:Hi]
} }
``` ```
@@ -430,7 +430,7 @@ func main() {
fmt.Println(*result) fmt.Println(*result)
// Output: // Output:
// 123 // 123
} }
``` ```
@@ -455,29 +455,29 @@ import (
func main() { func main() {
result1 := convertor.ToString("") result1 := convertor.ToString("")
result2 := convertor.ToString(nil) result2 := convertor.ToString(nil)
result3 := convertor.ToString(0) result3 := convertor.ToString(0)
result4 := convertor.ToString(1.23) result4 := convertor.ToString(1.23)
result5 := convertor.ToString(true) result5 := convertor.ToString(true)
result6 := convertor.ToString(false) result6 := convertor.ToString(false)
result7 := convertor.ToString([]int{1, 2, 3}) result7 := convertor.ToString([]int{1, 2, 3})
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
fmt.Println(result3) fmt.Println(result3)
fmt.Println(result4) fmt.Println(result4)
fmt.Println(result5) fmt.Println(result5)
fmt.Println(result6) fmt.Println(result6)
fmt.Println(result7) fmt.Println(result7)
// Output: // Output:
// //
// //
// 0 // 0
// 1.23 // 1.23
// true // true
// false // false
// [1,2,3] // [1,2,3]
} }
``` ```
@@ -502,19 +502,19 @@ import (
func main() { func main() {
type People struct { type People struct {
Name string `json:"name"` Name string `json:"name"`
age int age int
} }
p := People{ p := People{
"test", "test",
100, 100,
} }
pm, _ := convertor.StructToMap(p) pm, _ := convertor.StructToMap(p)
fmt.Println(pm) fmt.Println(pm)
// Output: // Output:
// map[name:test] // map[name:test]
} }
``` ```
@@ -571,7 +571,7 @@ func main() {
fmt.Println(byteData) fmt.Println(byteData)
// Output: // Output:
// [6 12 0 3 97 98 99] // [6 12 0 3 97 98 99]
} }
``` ```
@@ -596,16 +596,16 @@ import (
func main() { func main() {
var result string var result string
byteData := []byte{6, 12, 0, 3, 97, 98, 99} byteData := []byte{6, 12, 0, 3, 97, 98, 99}
err := convertor.DecodeByte(byteData, &result) err := convertor.DecodeByte(byteData, &result)
if err != nil { if err != nil {
return return
} }
fmt.Println(result) fmt.Println(result)
// Output: // Output:
// abc // abc
} }
``` ```