From 8f49078eb35639b11684e366877064874a704698 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Tue, 20 Jun 2023 14:04:18 +0800 Subject: [PATCH] doc: add go playground demo --- README.md | 9 +++++++++ README_zh-CN.md | 9 +++++++++ convertor/convertor.go | 4 ++-- docs/fileutil.md | 22 +++++++++++++++++----- docs/fileutil_zh-CN.md | 20 ++++++++++++++++---- docs/random.md | 2 +- docs/random_zh-CN.md | 2 +- fileutil/file.go | 2 +- mathutil/mathutil.go | 4 ++-- pointer/pointer.go | 6 +++--- random/random.go | 2 +- strutil/string.go | 2 +- 12 files changed, 63 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index df105ba..2a7bf57 100644 --- a/README.md +++ b/README.md @@ -284,8 +284,11 @@ import "github.com/duke-git/lancet/v2/convertor" [[play](https://go.dev/play/p/syqw0-WG7Xd)] - **Utf8ToGbk** : converts utf8 encoding data to GBK encoding data [[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#Utf8ToGbk)] + [[play](https://go.dev/play/p/9FlIaFLArIL)] - **GbkToUtf8** : converts GBK encoding data to utf8 encoding data. [[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#GbkToUtf8)] + [[play](https://go.dev/play/p/OphmHCN_9u8)] + ### 6. Cryptor package is for data encryption and decryption. @@ -613,6 +616,7 @@ import "github.com/duke-git/lancet/v2/fileutil" [[play](https://go.dev/play/p/OExTkhGEd3_u)] - **WriteCsvFile** : write content to target csv file. [[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#WriteCsvFile)] + [[play](https://go.dev/play/p/dAXm58Q5U1o)] - **WriteBytesToFile** : write bytes to target file. [[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#WriteBytesToFile)] [[play](https://go.dev/play/p/s7QlDxMj3P8)] @@ -835,8 +839,11 @@ import "github.com/duke-git/lancet/v2/mathutil" [[play](https://go.dev/play/p/TWMQlMywDsP)] - **Log** : returns the logarithm of base n. [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Log)] + [[play](https://go.dev/play/p/_d4bi8oyhat)] - **Sum** : return sum of passed numbers. [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Sum)] + [[play](https://go.dev/play/p/1To2ImAMJA7)] + ### 14. Netutil package contains functions to get net information and send http request. @@ -945,6 +952,8 @@ import "github.com/duke-git/lancet/v2/random" [[play](https://go.dev/play/p/_Z9SFmr28ft)] - **RandUniqueIntSlice** : generate a slice of random int of length n that do not repeat. [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandUniqueIntSlice)] + [[play](https://go.dev/play/p/uBkRSOz73Ec)] + ### 16. Retry package is for executing a function repeatedly until it was successful or canceled by the context. diff --git a/README_zh-CN.md b/README_zh-CN.md index 48842d9..280ffb9 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -283,8 +283,11 @@ import "github.com/duke-git/lancet/v2/convertor" [[play](https://go.dev/play/p/syqw0-WG7Xd)] - **Utf8ToGbk** : utf8 编码转 GBK 编码。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#Utf8ToGbk)] + [[play](https://go.dev/play/p/9FlIaFLArIL)] - **GbkToUtf8** : GBK 编码转 utf8 编码。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#GbkToUtf8)] + [[play](https://go.dev/play/p/OphmHCN_9u8)] + ### 6. cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。 @@ -614,6 +617,7 @@ import "github.com/duke-git/lancet/v2/fileutil" [[play](https://go.dev/play/p/OExTkhGEd3_u)] - **WriteCsvFile** : 向 csv 文件写入内容。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#WriteCsvFile)] + [[play](https://go.dev/play/p/dAXm58Q5U1o)] - **WriteBytesToFile** : 将 bytes 写入文件。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#WriteBytesToFile)] [[play](https://go.dev/play/p/s7QlDxMj3P8)] @@ -835,8 +839,11 @@ import "github.com/duke-git/lancet/v2/mathutil" [[play](https://go.dev/play/p/TWMQlMywDsP)] - **Log** : 计算以 base 为底 n 的对数。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Log)] + [[play](https://go.dev/play/p/_d4bi8oyhat)] - **Sum** : 求传入参数之和。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Sum)] + [[play](https://go.dev/play/p/1To2ImAMJA7)] + ### 14. netutil 网络包支持获取 ip 地址,发送 http 请求。 @@ -945,6 +952,8 @@ import "github.com/duke-git/lancet/v2/random" [[play](https://go.dev/play/p/_Z9SFmr28ft)] - **RandUniqueIntSlice** : 生成一个不重复的长度为n的随机int切片。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandUniqueIntSlice)] + [[play](https://go.dev/play/p/uBkRSOz73Ec)] + ### 16. retry 重试执行函数直到函数运行成功或被 context cancel。 diff --git a/convertor/convertor.go b/convertor/convertor.go index 320182e..d90479d 100644 --- a/convertor/convertor.go +++ b/convertor/convertor.go @@ -380,7 +380,7 @@ func ToInterface(v reflect.Value) (value interface{}, ok bool) { } // Utf8ToGbk convert utf8 encoding data to GBK encoding data. -// Play: todo +// Play: https://go.dev/play/p/9FlIaFLArIL func Utf8ToGbk(bs []byte) ([]byte, error) { r := transform.NewReader(bytes.NewReader(bs), simplifiedchinese.GBK.NewEncoder()) b, err := io.ReadAll(r) @@ -388,7 +388,7 @@ func Utf8ToGbk(bs []byte) ([]byte, error) { } // GbkToUtf8 convert GBK encoding data to utf8 encoding data. -// Play: todo +// Play: https://go.dev/play/p/OphmHCN_9u8 func GbkToUtf8(bs []byte) ([]byte, error) { r := transform.NewReader(bytes.NewReader(bs), simplifiedchinese.GBK.NewDecoder()) b, err := io.ReadAll(r) diff --git a/docs/fileutil.md b/docs/fileutil.md index 8d1c74a..70e591e 100644 --- a/docs/fileutil.md +++ b/docs/fileutil.md @@ -484,7 +484,7 @@ func main() { Signature: ```go -func ZipAppendEntry(fpath string, destPath string) error +func ZipAppendEntry(fpath string, destPath string) error ``` Example: @@ -714,18 +714,30 @@ import ( ) func main() { + fpath := "./test.csv" + fileutil.CreateFile(fpath) + + f, _ := os.OpenFile(fpath, os.O_WRONLY|os.O_TRUNC, 0777) + defer f.Close() + data := [][]string{ {"Lili", "22", "female"}, {"Jim", "21", "male"}, } - err := WriteCsvFile("./testdata/test2.csv", data, false) - fmt.Println(err) + err := fileutil.WriteCsvFile(fpath, data, false) - content, _ := ReadCsvFile("./testdata/test2.csv") + if err != nil { + return + } + + content, err := fileutil.ReadCsvFile(fpath) + + if err != nil { + return + } fmt.Println(content) // Output: - // // [[Lili 22 female] [Jim 21 male]] } ``` diff --git a/docs/fileutil_zh-CN.md b/docs/fileutil_zh-CN.md index 469b76b..b68cbd1 100644 --- a/docs/fileutil_zh-CN.md +++ b/docs/fileutil_zh-CN.md @@ -714,18 +714,30 @@ import ( ) func main() { + fpath := "./test.csv" + fileutil.CreateFile(fpath) + + f, _ := os.OpenFile(fpath, os.O_WRONLY|os.O_TRUNC, 0777) + defer f.Close() + data := [][]string{ {"Lili", "22", "female"}, {"Jim", "21", "male"}, } - err := WriteCsvFile("./testdata/test2.csv", data, false) - fmt.Println(err) + err := fileutil.WriteCsvFile(fpath, data, false) - content, _ := ReadCsvFile("./testdata/test2.csv") + if err != nil { + return + } + + content, err := fileutil.ReadCsvFile(fpath) + + if err != nil { + return + } fmt.Println(content) // Output: - // // [[Lili 22 female] [Jim 21 male]] } ``` diff --git a/docs/random.md b/docs/random.md index eac6d82..d4e9e73 100644 --- a/docs/random.md +++ b/docs/random.md @@ -269,7 +269,7 @@ import ( ) func main() { - result := RandUniqueIntSlice(5, 0, 10) + result := random.RandUniqueIntSlice(5, 0, 10) fmt.Println(result) //[0 4 7 1 5] (random) } ``` diff --git a/docs/random_zh-CN.md b/docs/random_zh-CN.md index 4cf38ec..a3debdf 100644 --- a/docs/random_zh-CN.md +++ b/docs/random_zh-CN.md @@ -268,7 +268,7 @@ import ( ) func main() { - result := RandUniqueIntSlice(5, 0, 10) + result := random.RandUniqueIntSlice(5, 0, 10) fmt.Println(result) //[0 4 7 1 5] (random) } ``` diff --git a/fileutil/file.go b/fileutil/file.go index 8b0f1f6..d0e48cb 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -507,7 +507,7 @@ func ReadCsvFile(filepath string) ([][]string, error) { } // WriteCsvFile write content to target csv file. -// Play: todo +// Play: https://go.dev/play/p/dAXm58Q5U1o func WriteCsvFile(filepath string, records [][]string, append bool) error { flag := os.O_RDWR | os.O_CREATE diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go index 8fc469e..cd8e999 100644 --- a/mathutil/mathutil.go +++ b/mathutil/mathutil.go @@ -173,7 +173,7 @@ func MinBy[T any](slice []T, comparator func(T, T) bool) T { } // Sum return sum of passed numbers. -// Play: todo +// Play: https://go.dev/play/p/1To2ImAMJA7 func Sum[T constraints.Integer | constraints.Float](numbers ...T) T { var sum T @@ -337,7 +337,7 @@ func Sin(radian float64, precision ...int) float64 { } // Log returns the logarithm of base n. -// Play: todo +// Play: https://go.dev/play/p/_d4bi8oyhat func Log(n, base float64) float64 { return math.Log(n) / math.Log(base) } diff --git a/pointer/pointer.go b/pointer/pointer.go index e28f734..812bdc1 100644 --- a/pointer/pointer.go +++ b/pointer/pointer.go @@ -7,19 +7,19 @@ package pointer import "reflect" // Of returns a pointer to the value `v`. -// Play: todo +// Play: https://go.dev/play/p/HFd70x4DrMj func Of[T any](v T) *T { return &v } // Unwrap returns the value from the pointer. -// Play: todo +// Play: https://go.dev/play/p/cgeu3g7cjWb func Unwrap[T any](p *T) T { return *p } // ExtractPointer returns the underlying value by the given interface type -// Play: todo +// Play: https://go.dev/play/p/D7HFjeWU2ZP func ExtractPointer(value any) any { t := reflect.TypeOf(value) v := reflect.ValueOf(value) diff --git a/random/random.go b/random/random.go index 698caf8..0ba4fcd 100644 --- a/random/random.go +++ b/random/random.go @@ -116,7 +116,7 @@ func UUIdV4() (string, error) { } // RandUniqueIntSlice generate a slice of random int of length n that do not repeat. -// Play: todo +// Play: https://go.dev/play/p/uBkRSOz73Ec func RandUniqueIntSlice(n, min, max int) []int { if min > max { return []int{} diff --git a/strutil/string.go b/strutil/string.go index 51c70bc..169fc1d 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -561,7 +561,7 @@ var ( // RemoveWhiteSpace remove whitespace characters from a string. // when set repalceAll is true removes all whitespace, false only replaces consecutive whitespace characters with one space. -// Play: todo +// Play: https://go.dev/play/p/HzLC9vsTwkf func RemoveWhiteSpace(str string, repalceAll bool) string { if repalceAll && str != "" { return strings.Join(strings.Fields(str), "")