diff --git a/docs/algorithm.md b/docs/algorithm.md index 4975ee2..ec30809 100644 --- a/docs/algorithm.md +++ b/docs/algorithm.md @@ -40,8 +40,6 @@ import ( ## Documentation - - ### BubbleSort

Sort slice with bubble sort algorithm. Param comparator should implements lancetconstraints.Comparator.

@@ -76,15 +74,18 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - intSlice := []int{2, 1, 5, 3, 6, 4} + numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} - algorithm.BubbleSort(intSlice, comparator) - fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} + algorithm.BubbleSort(numbers, comparator) + + fmt.Println(numbers) + + // Output: + // [1 2 3 4 5 6] } ``` - ### InsertionSort

Sort slice with insertion sort algorithm. Param comparator should implements lancetconstraints.Comparator.

@@ -122,33 +123,31 @@ func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int { } else if p1.Age > p2.Age { return 1 } - return 0 - //decending order - // if p1.Age > p2.Age { - // return -1 - // } else if p1.Age < p2.Age { - // return 1 - // } + return 0 } func main() { - var peoples = []people{ + peoples := []people{ {Name: "a", Age: 20}, {Name: "b", Age: 10}, {Name: "c", Age: 17}, {Name: "d", Age: 8}, {Name: "e", Age: 28}, } + comparator := &peopleAgeComparator{} + algorithm.InsertionSort(peoples, comparator) - fmt.Println(peoples) //[{d 8} {b 10} {c 17} {a 20} {e 28}] + fmt.Println(peoples) + + // Output: + //[{d 8} {b 10} {c 17} {a 20} {e 28}] } ``` - ### SelectionSort

Sort slice with selection sort algorithm. Param comparator should implements lancetconstraints.Comparator.

@@ -183,15 +182,18 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - intSlice := []int{2, 1, 5, 3, 6, 4} + numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} - algorithm.SelectionSort(intSlice, comparator) - fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} + algorithm.SelectionSort(numbers, comparator) + + fmt.Println(numbers) + + // Output: + // [1 2 3 4 5 6] } ``` - ### ShellSort

Sort slice with shell sort algorithm. Param comparator should implements lancetconstraints.Comparator.

@@ -226,15 +228,18 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - intSlice := []int{2, 1, 5, 3, 6, 4} + numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} - algorithm.ShellSort(intSlice, comparator) - fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} + algorithm.ShellSort(numbers, comparator) + + fmt.Println(numbers) + + // Output: + // [1 2 3 4 5 6] } ``` - ### QuickSort

Sort slice with quick sort algorithm. Param comparator should implements lancetconstraints.Comparator.

@@ -269,15 +274,18 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - intSlice := []int{2, 1, 5, 3, 6, 4} + numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} - algorithm.QuickSort(intSlice, comparator) - fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} + algorithm.QuickSort(numbers, comparator) + + fmt.Println(numbers) + + // Output: + // [1 2 3 4 5 6] } ``` - ### HeapSort

Sort slice with heap sort algorithm. Param comparator should implements lancetconstraints.Comparator.

@@ -312,15 +320,18 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - intSlice := []int{2, 1, 5, 3, 6, 4} + numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} - algorithm.HeapSort(intSlice, comparator) - fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} + algorithm.HeapSort(numbers, comparator) + + fmt.Println(numbers) + + // Output: + // [1 2 3 4 5 6] } ``` - ### MergeSort

Sort slice with merge sort algorithm. Param comparator should implements lancetconstraints.Comparator.

@@ -355,15 +366,18 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - intSlice := []int{2, 1, 5, 3, 6, 4} + numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} - algorithm.MergeSort(intSlice, comparator) - fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} + algorithm.MergeSort(numbers, comparator) + + fmt.Println(numbers) + + // Output: + // [1 2 3 4 5 6] } ``` - ### CountSort

Sort slice with count sort algorithm. Param comparator should implements lancetconstraints.Comparator.

@@ -399,15 +413,18 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - intSlice := []int{2, 1, 5, 3, 6, 4} + numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} - sortedSlice := algorithm.CountSort(intSlice, comparator) - fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6} + sortedNums := algorithm.CountSort(numbers, comparator) + + fmt.Println(sortedNums) + + // Output: + // [1 2 3 4 5 6] } ``` - ### BinarySearch

BinarySearch search for target within a sorted slice, recursive call itself. If a target is found, the index of the target is returned. Else the function return -1.

@@ -442,13 +459,18 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8} - comparator := &intComparator{} - foundIndex := algorithm.BinarySearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator) - fmt.Println(foundIndex) //4 + numbers := []int{1, 2, 3, 4, 5, 6, 7, 8} + comparator := &intComparator{} - notFoundIndex := algorithm.BinarySearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator) - fmt.Println(notFoundIndex) //-1 + result1 := algorithm.BinarySearch(numbers, 5, 0, len(numbers)-1, comparator) + result2 := algorithm.BinarySearch(numbers, 9, 0, len(numbers)-1, comparator) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 4 + // -1 } ``` @@ -486,17 +508,21 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8} - comparator := &intComparator{} - foundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator) - fmt.Println(foundIndex) //4 + numbers := []int{1, 2, 3, 4, 5, 6, 7, 8} + comparator := &intComparator{} - notFoundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator) - fmt.Println(notFoundIndex) //-1 + result1 := algorithm.BinaryIterativeSearch(numbers, 5, 0, len(numbers)-1, comparator) + result2 := algorithm.BinaryIterativeSearch(numbers, 9, 0, len(numbers)-1, comparator) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 4 + // -1 } ``` - ### LinearSearch

return the index of target in slice base on equal function.If a target is found, the index of the target is returned. Else the function return -1.

@@ -534,7 +560,6 @@ func main() { } ``` - ### LRUCache

LRUCache implements mem cache with lru.

@@ -560,16 +585,28 @@ import ( func main() { cache := algorithm.NewLRUCache[int, int](2) - cache.Put(1, 1) - cache.Put(2, 2) - cache.Put(3, 3) + cache.Put(1, 1) + cache.Put(2, 2) - fmt.Println(cache.Len()) // 3 + result1, ok1 := cache.Get(1) + result2, ok2 := cache.Get(2) + result3, ok3 := cache.Get(3) - v, ok := cache.Get(1) - fmt.Println(v, ok) // 1 true + fmt.Println(result1, ok1) + fmt.Println(result2, ok2) + fmt.Println(result3, ok3) - ok = cache.Delete(1) - fmt.Println(ok) // true + fmt.Println(cache.Len()) + + ok := cache.Delete(2) + fmt.Println(ok) + + + // Output: + // 1 true + // 2 true + // 0 false + // 2 + // true } ``` \ No newline at end of file diff --git a/docs/algorithm_zh-CN.md b/docs/algorithm_zh-CN.md index 7f42e6d..3ab5ec6 100644 --- a/docs/algorithm_zh-CN.md +++ b/docs/algorithm_zh-CN.md @@ -41,14 +41,14 @@ import ( ## 文档 ### BubbleSort -

冒泡排序,参数comparator需要实现包lancetconstraints.Comparator

+

冒泡排序,参数comparator需要实现包lancetconstraints.Comparator。

函数签名: ```go func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -Example: +示例: ```go package main @@ -74,24 +74,27 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - intSlice := []int{2, 1, 5, 3, 6, 4} + numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} - algorithm.BubbleSort(intSlice, comparator) - fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} + algorithm.BubbleSort(numbers, comparator) + + fmt.Println(numbers) + + // Output: + // [1 2 3 4 5 6] } - ``` ### InsertionSort -

插入排序,参数comparator需要实现包lancetconstraints.Comparator

+

插入排序,参数comparator需要实现包lancetconstraints.Comparator。

函数签名: ```go func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -Example: +示例: ```go package main @@ -120,40 +123,40 @@ func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int { } else if p1.Age > p2.Age { return 1 } - return 0 - //decending order - // if p1.Age > p2.Age { - // return -1 - // } else if p1.Age < p2.Age { - // return 1 - // } + return 0 } func main() { - var peoples = []people{ + peoples := []people{ {Name: "a", Age: 20}, {Name: "b", Age: 10}, {Name: "c", Age: 17}, {Name: "d", Age: 8}, {Name: "e", Age: 28}, } + comparator := &peopleAgeComparator{} + algorithm.InsertionSort(peoples, comparator) - fmt.Println(peoples) //[{d 8} {b 10} {c 17} {a 20} {e 28}] + fmt.Println(peoples) + + // Output: + //[{d 8} {b 10} {c 17} {a 20} {e 28}] } ``` + ### SelectionSort -

选择排序,参数comparator需要实现包lancetconstraints.Comparator

+

选择排序,参数comparator需要实现包lancetconstraints.Comparator。

函数签名: ```go func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -Example: +示例: ```go package main @@ -179,23 +182,27 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - intSlice := []int{2, 1, 5, 3, 6, 4} + numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} - algorithm.SelectionSort(intSlice, comparator) - fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} + algorithm.SelectionSort(numbers, comparator) + + fmt.Println(numbers) + + // Output: + // [1 2 3 4 5 6] } ``` ### ShellSort -

希尔排序,参数comparator需要实现包lancetconstraints.Comparator

+

希尔排序,参数comparator需要实现包lancetconstraints.Comparator。

函数签名: ```go func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -Example: +示例: ```go package main @@ -221,24 +228,27 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - intSlice := []int{2, 1, 5, 3, 6, 4} + numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} - algorithm.ShellSort(intSlice, comparator) - fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} + algorithm.ShellSort(numbers, comparator) + + fmt.Println(numbers) + + // Output: + // [1 2 3 4 5 6] } ``` - ### QuickSort -

快速排序,参数comparator需要实现包lancetconstraints.Comparator

+

快速排序,参数comparator需要实现包lancetconstraints.Comparator。

函数签名: ```go -func QuickSort[T any](slice []T, comparator lancetconstraints.Comparator) []T +func QuickSort[T any](slice []T comparator lancetconstraints.Comparator) ``` -Example: +示例: ```go package main @@ -264,24 +274,27 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - intSlice := []int{2, 1, 5, 3, 6, 4} + numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} - algorithm.QuickSort(intSlice, comparator) - fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} + algorithm.QuickSort(numbers, comparator) + + fmt.Println(numbers) + + // Output: + // [1 2 3 4 5 6] } ``` - ### HeapSort -

堆排序,参数comparator需要实现包lancetconstraints.Comparator

+

堆排序,参数comparator需要实现包lancetconstraints.Comparator。

函数签名: ```go -func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) []T +func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -Example: +示例: ```go package main @@ -307,24 +320,27 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - intSlice := []int{2, 1, 5, 3, 6, 4} + numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} - algorithm.HeapSort(intSlice, comparator) - fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} + algorithm.HeapSort(numbers, comparator) + + fmt.Println(numbers) + + // Output: + // [1 2 3 4 5 6] } - ``` ### MergeSort -

归并排序,参数comparator需要实现包lancetconstraints.Comparator

+

归并排序,参数comparator需要实现包lancetconstraints.Comparator。

函数签名: ```go func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator) ``` -Example: +示例: ```go package main @@ -350,24 +366,27 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - intSlice := []int{2, 1, 5, 3, 6, 4} + numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} - algorithm.MergeSort(intSlice, comparator) - fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6} + algorithm.MergeSort(numbers, comparator) + + fmt.Println(numbers) + + // Output: + // [1 2 3 4 5 6] } ``` - ### CountSort -

计数排序,参数comparator需要实现包lancetconstraints.Comparator

+

计数排序,参数comparator需要实现包lancetconstraints.Comparator。

函数签名: ```go func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T ``` -Example: +示例: ```go package main @@ -377,6 +396,7 @@ import ( "github.com/duke-git/lancet/v2/algorithm" ) + type intComparator struct{} func (c *intComparator) Compare(v1 any, v2 any) int { @@ -393,24 +413,27 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - intSlice := []int{2, 1, 5, 3, 6, 4} + numbers := []int{2, 1, 5, 3, 6, 4} comparator := &intComparator{} - sortedSlice := algorithm.CountSort(intSlice, comparator) - fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6} + sortedNums := algorithm.CountSort(numbers, comparator) + + fmt.Println(sortedNums) + + // Output: + // [1 2 3 4 5 6] } ``` - ### BinarySearch -

二分递归查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator

+

二分递归查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator。

函数签名: ```go func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int ``` -Example: +示例: ```go package main @@ -436,27 +459,30 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8} - comparator := &intComparator{} - foundIndex := algorithm.BinarySearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator) - fmt.Println(foundIndex) //4 + numbers := []int{1, 2, 3, 4, 5, 6, 7, 8} + comparator := &intComparator{} - notFoundIndex := algorithm.BinarySearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator) - fmt.Println(notFoundIndex) //-1 + result1 := algorithm.BinarySearch(numbers, 5, 0, len(numbers)-1, comparator) + result2 := algorithm.BinarySearch(numbers, 9, 0, len(numbers)-1, comparator) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 4 + // -1 } - ``` - ### BinaryIterativeSearch -

二分迭代查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator

+

二分迭代查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator。

函数签名: ```go func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int ``` -Example: +示例: ```go package main @@ -482,28 +508,30 @@ func (c *intComparator) Compare(v1 any, v2 any) int { } func main() { - var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8} - comparator := &intComparator{} - foundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator) - fmt.Println(foundIndex) //4 + numbers := []int{1, 2, 3, 4, 5, 6, 7, 8} + comparator := &intComparator{} - notFoundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator) - fmt.Println(notFoundIndex) //-1 + result1 := algorithm.BinaryIterativeSearch(numbers, 5, 0, len(numbers)-1, comparator) + result2 := algorithm.BinaryIterativeSearch(numbers, 9, 0, len(numbers)-1, comparator) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 4 + // -1 } ``` - - - ### LinearSearch -

基于传入的相等函数线性查找元素,返回元素索引,未找到元素返回-1

+

基于传入的相等函数线性查找元素,返回元素索引,未找到元素返回-1。

函数签名: ```go func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int ``` -Example: +示例: ```go package main @@ -532,9 +560,8 @@ func main() { } ``` - ### LRUCache -

lru实现缓存

+

lru算法实现缓存。

函数签名: @@ -545,7 +572,7 @@ func (l *LRUCache[K, V]) Put(key K, value V) func (l *LRUCache[K, V]) Delete(key K) bool func (l *LRUCache[K, V]) Len() int ``` -Example: +示例: ```go package main @@ -558,16 +585,28 @@ import ( func main() { cache := algorithm.NewLRUCache[int, int](2) - cache.Put(1, 1) - cache.Put(2, 2) - cache.Put(3, 3) + cache.Put(1, 1) + cache.Put(2, 2) - fmt.Println(cache.Len()) // 3 + result1, ok1 := cache.Get(1) + result2, ok2 := cache.Get(2) + result3, ok3 := cache.Get(3) - v, ok := cache.Get(1) - fmt.Println(v, ok) // 1 true + fmt.Println(result1, ok1) + fmt.Println(result2, ok2) + fmt.Println(result3, ok3) - ok = cache.Delete(1) - fmt.Println(ok) // true + fmt.Println(cache.Len()) + + ok := cache.Delete(2) + fmt.Println(ok) + + + // Output: + // 1 true + // 2 true + // 0 false + // 2 + // true } ``` \ No newline at end of file diff --git a/docs/concurrency.md b/docs/concurrency.md index 73acc61..bac75db 100644 --- a/docs/concurrency.md +++ b/docs/concurrency.md @@ -61,8 +61,6 @@ func main() { } ``` - - ### Bridge

Link multiple channels into one channel until cancel the context.

@@ -105,6 +103,7 @@ func main() { for v := range c.Bridge(ctx, genVals()) { fmt.Println(v) } + // Output: // 1 // 2 @@ -114,9 +113,6 @@ func main() { } ``` - - - ### FanIn

Merge multiple channels into one channel until cancel the context.

@@ -156,7 +152,6 @@ func main() { } ``` - ### Repeat

Create channel, put values into the channel repeatly until cancel the context.

@@ -187,6 +182,7 @@ func main() { for v := range intStream { fmt.Println(v) } + // Output: // 1 // 2 @@ -195,8 +191,6 @@ func main() { } ``` - - ### Generate

Creates a channel, then put values into the channel.

@@ -269,6 +263,7 @@ func main() { for v := range intStream { fmt.Println(v) } + // Output: // hello // hello @@ -276,8 +271,6 @@ func main() { } ``` - - ### Or

Read one or more channels into one channel, will close when any readin channel is closed.

@@ -321,9 +314,6 @@ func main() { } ``` - - - ### OrDone

Read a channel into another channel, will close until cancel context.

@@ -354,6 +344,7 @@ func main() { for v := range c.OrDone(ctx, intStream) { fmt.Println(v) } + // Output: // 1 // 1 @@ -361,9 +352,6 @@ func main() { } ``` - - - ### Take

Create a channel whose values are taken from another channel with limit number.

@@ -402,6 +390,7 @@ func main() { for v := range intStream { fmt.Println(v) } + // Output: // 1 // 2 @@ -409,8 +398,6 @@ func main() { } ``` - - ### Tee

Split one chanel into two channels, until cancel the context.

@@ -444,6 +431,7 @@ func main() { fmt.Println(v) fmt.Println(<-ch2) } + // Output: // 1 // 1 diff --git a/docs/concurrency_zh-CN.md b/docs/concurrency_zh-CN.md index ec398cd..1f553de 100644 --- a/docs/concurrency_zh-CN.md +++ b/docs/concurrency_zh-CN.md @@ -61,8 +61,6 @@ func main() { } ``` - - ### Bridge

将多个channel链接到一个channel,直到取消上下文。

@@ -105,6 +103,7 @@ func main() { for v := range c.Bridge(ctx, genVals()) { fmt.Println(v) } + // Output: // 1 // 2 @@ -114,9 +113,6 @@ func main() { } ``` - - - ### FanIn

将多个channel合并为一个channel,直到取消上下文。

@@ -156,7 +152,6 @@ func main() { } ``` - ### Generate

根据传入的值,生成channel.

@@ -225,6 +220,7 @@ func main() { for v := range intStream { fmt.Println(v) } + // Output: // 1 // 2 @@ -233,9 +229,6 @@ func main() { } ``` - - - ### RepeatFn

返回一个channel,重复执行函数fn,并将结果放入返回的channel,直到取消上下文。

@@ -277,8 +270,6 @@ func main() { } ``` - - ### Or

将一个或多个channel读取到一个channel中,当任何读取channel关闭时将结束读取。

@@ -322,9 +313,6 @@ func main() { } ``` - - - ### OrDone

将一个channel读入另一个channel,直到取消上下文。

@@ -355,6 +343,7 @@ func main() { for v := range c.OrDone(ctx, intStream) { fmt.Println(v) } + // Output: // 1 // 1 @@ -362,9 +351,6 @@ func main() { } ``` - - - ### Take

返回一个channel,其值从另一个channel获取,直到取消上下文。

@@ -403,6 +389,7 @@ func main() { for v := range intStream { fmt.Println(v) } + // Output: // 1 // 2 @@ -410,8 +397,6 @@ func main() { } ``` - - ### Tee

将一个channel分成两个channel,直到取消上下文。

diff --git a/docs/condition.md b/docs/condition.md index d3016b6..52a825f 100644 --- a/docs/condition.md +++ b/docs/condition.md @@ -58,40 +58,51 @@ import ( func main() { // bool - fmt.Println(condition.Bool(false)) // false - fmt.Println(condition.Bool(true)) // true + result1 := condition.Bool(false) + result2 := condition.Bool(true) + fmt.Println(result1) // false + fmt.Println(result2) // true // integer - fmt.Println(condition.Bool(0)) // false - fmt.Println(condition.Bool(1)) // true - - // float - fmt.Println(condition.Bool(0.0)) // false - fmt.Println(condition.Bool(0.1)) // true + result3 := condition.Bool(0) + result4 := condition.Bool(1) + fmt.Println(result3) // false + fmt.Println(result4) // true // string - fmt.Println(condition.Bool("")) // false - fmt.Println(condition.Bool(" ")) // true - fmt.Println(condition.Bool("0")) // true + result5 := condition.Bool("") + result6 := condition.Bool(" ") + fmt.Println(result5) // false + fmt.Println(result6) // true // slice - var nums [2]int - fmt.Println(condition.Bool(nums)) // false - nums = [2]int{0, 1} - fmt.Println(condition.Bool(nums)) // true + nums := []int{} + result7 := condition.Bool(nums) - // map - fmt.Println(condition.Bool(map[string]string{})) // false - fmt.Println(condition.Bool(map[string]string{"a": "a"})) // true + nums = append(nums, 1, 2) + result8 := condition.Bool(nums) + fmt.Println(result7) // false + fmt.Println(result8) // true // struct - fmt.Println(condition.Bool(struct{}{})) // false - fmt.Println(condition.Bool(time.Now())) // true + result9 = condition.Bool(struct{}{}) + fmt.Println(result8) // false + + + // Output: + // false + // true + // false + // true + // false + // true + // false + // true + // false } ``` - ### And

Returns true if both a and b are truthy.

@@ -277,10 +288,18 @@ import ( ) func main() { - trueValue := "1" - falseValue := "0" + conditionTrue := 2 > 1 + result1 := condition.TernaryOperator(conditionTrue, 0, 1) - fmt.Println(condition.TernaryOperator(true, trueValue, falseValue)) // "1" + conditionFalse := 2 > 3 + result2 := condition.TernaryOperator(conditionFalse, 0, 1) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 0 + // 1 } ``` diff --git a/docs/condition_zh-CN.md b/docs/condition_zh-CN.md index abe8134..758199b 100644 --- a/docs/condition_zh-CN.md +++ b/docs/condition_zh-CN.md @@ -57,40 +57,50 @@ import ( func main() { // bool - fmt.Println(condition.Bool(false)) // false - fmt.Println(condition.Bool(true)) // true + result1 := condition.Bool(false) + result2 := condition.Bool(true) + fmt.Println(result1) // false + fmt.Println(result2) // true // integer - fmt.Println(condition.Bool(0)) // false - fmt.Println(condition.Bool(1)) // true - - // float - fmt.Println(condition.Bool(0.0)) // false - fmt.Println(condition.Bool(0.1)) // true + result3 := condition.Bool(0) + result4 := condition.Bool(1) + fmt.Println(result3) // false + fmt.Println(result4) // true // string - fmt.Println(condition.Bool("")) // false - fmt.Println(condition.Bool(" ")) // true - fmt.Println(condition.Bool("0")) // true + result5 := condition.Bool("") + result6 := condition.Bool(" ") + fmt.Println(result5) // false + fmt.Println(result6) // true // slice - var nums [2]int - fmt.Println(condition.Bool(nums)) // false - nums = [2]int{0, 1} - fmt.Println(condition.Bool(nums)) // true + nums := []int{} + result7 := condition.Bool(nums) - // map - fmt.Println(condition.Bool(map[string]string{})) // false - fmt.Println(condition.Bool(map[string]string{"a": "a"})) // true + nums = append(nums, 1, 2) + result8 := condition.Bool(nums) + fmt.Println(result7) // false + fmt.Println(result8) // true // struct - fmt.Println(condition.Bool(struct{}{})) // false - fmt.Println(condition.Bool(time.Now())) // true + result9 = condition.Bool(struct{}{}) + fmt.Println(result8) // false + + + // Output: + // false + // true + // false + // true + // false + // true + // false + // true + // false } ``` - - ### And

逻辑且操作,当切仅当a和b都为true时返回true

@@ -117,8 +127,6 @@ func main() { } ``` - - ### Or

逻辑或操作,当切仅当a和b都为false时返回false

@@ -145,8 +153,6 @@ func main() { } ``` - - ### Xor

逻辑异或操作,a和b相同返回false,a和b不相同返回true

@@ -173,8 +179,6 @@ func main() { } ``` - - ### Nor

异或的取反操作

@@ -201,8 +205,6 @@ func main() { } ``` - - ### Xnor

如果a和b都是真的或a和b均是假的,则返回true。

@@ -255,8 +257,6 @@ func main() { } ``` - - ### TernaryOperator

三元运算符

@@ -276,10 +276,18 @@ import ( ) func main() { - trueValue := "1" - falseValue := "0" + conditionTrue := 2 > 1 + result1 := condition.TernaryOperator(conditionTrue, 0, 1) - fmt.Println(condition.TernaryOperator(true, trueValue, falseValue)) // "1" + conditionFalse := 2 > 3 + result2 := condition.TernaryOperator(conditionFalse, 0, 1) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 0 + // 1 } ``` diff --git a/docs/convertor.md b/docs/convertor.md index f874f03..ddb1b26 100644 --- a/docs/convertor.md +++ b/docs/convertor.md @@ -40,8 +40,6 @@ import ( ## Documentation - - ### ColorHexToRGB

Convert color hex to color rgb.

@@ -62,13 +60,15 @@ import ( func main() { colorHex := "#003366" - r, g, b := convertor.ColorHexToRGB(colorHex) - fmt.Println(r, g, b) //0,51,102 + r, g, b := convertor.ColorHexToRGB(colorHex) + + fmt.Println(r, g, b) + + // Output: + // 0 51 102 } ``` - - ### ColorRGBToHex

Convert color rgb to color hex.

@@ -90,16 +90,17 @@ import ( func main() { r := 0 - g := 51 - b := 102 - colorHex := convertor.ColorRGBToHex(r, g, b) + g := 51 + b := 102 + colorHex := ColorRGBToHex(r, g, b) - fmt.Println(colorHex) //#003366 + fmt.Println(colorHex) + + // Output: + // #003366 } ``` - - ### ToBool

Convert string to bool. Use strconv.ParseBool.

@@ -120,22 +121,26 @@ import ( ) func main() { - v1, _ := convertor.ToBool("1") - fmt.Println(v1) //true + cases := []string{"1", "true", "True", "false", "False", "0", "123", "0.0", "abc"} - v2, _ := convertor.ToBool("true") - fmt.Println(v2) //true + for i := 0; i < len(cases); i++ { + result, _ := convertor.ToBool(cases[i]) + fmt.Println(result) + } - v3, _ := convertor.ToBool("True") - fmt.Println(v3) //true - - v4, _ := convertor.ToBool("123") - fmt.Println(v4) //false + // Output: + // true + // true + // true + // false + // false + // false + // false + // false + // false } ``` - - ### ToBytes

Convert value to byte slice.

@@ -156,16 +161,18 @@ import ( ) func main() { - bytesData, err := convertor.ToBytes("0") + bytesData, err := convertor.ToBytes("abc") if err != nil { fmt.Println(err) } - fmt.Println(bytesData) //[]bytes{3, 4, 0, 0} + + fmt.Println(bytesData) + + // Output: + // [97 98 99] } ``` - - ### ToChar

Convert string to char slice.

@@ -186,18 +193,21 @@ import ( ) func main() { - chars := convertor.ToChar("") - fmt.Println(chars) //[]string{""} + result1 := convertor.ToChar("") + result2 := convertor.ToChar("abc") + result3 := convertor.ToChar("1 2#3") - chars = convertor.ToChar("abc") - fmt.Println(chars) //[]string{"a", "b", "c"} + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - chars = convertor.ToChar("1 2#3") - fmt.Println(chars) //[]string{"1", " ", "2", "#", "3"} + // Output: + // [] + // [a b c] + // [1 2 # 3] } ``` - ### ToChannel

Convert a collection of elements to a read-only channel.

@@ -219,23 +229,21 @@ import ( func main() { ch := convertor.ToChannel([]int{1, 2, 3}) + result1 := <-ch + result2 := <-ch + result3 := <-ch - val1, _ := <-ch - fmt.Println(val1) //1 + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - val2, _ := <-ch - fmt.Println(val2) //2 - - val3, _ := <-ch - fmt.Println(val3) //3 - - _, ok := <-ch - fmt.Println(ok) //false + // Output: + // 1 + // 2 + // 3 } ``` - - ### ToFloat

Convert value to a float64 value. If param is a invalid floatable, will return 0.0 and error.

@@ -256,19 +264,30 @@ import ( ) func main() { - v, err := convertor.ToFloat("") - if err != nil { - fmt.Println(err) //strconv.ParseFloat: parsing "": invalid syntax - } - fmt.Println(v) //0 + result1, _ := convertor.ToFloat("") + result2, err := convertor.ToFloat("abc") + result3, _ := convertor.ToFloat("-1") + result4, _ := convertor.ToFloat("-.11") + result5, _ := convertor.ToFloat("1.23e3") + result6, _ := convertor.ToFloat(true) - v, _ = convertor.ToFloat("-.11") - fmt.Println(v) //-0.11 + fmt.Println(result1) + fmt.Println(result2, err) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + fmt.Println(result6) + + // Output: + // 0 + // 0 strconv.ParseFloat: parsing "": invalid syntax + // -1 + // -0.11 + // 1230 + // 0 } ``` - - ### ToInt

Convert value to a int64 value. If param is a invalid intable, will return 0 and error.

@@ -289,19 +308,27 @@ import ( ) func main() { - v, err := convertor.ToInt("") - if err != nil { - fmt.Println(err) //strconv.ParseInt: parsing "": invalid syntax - } - fmt.Println(v) //0 + result1, _ := convertor.ToInt("123") + result2, _ := convertor.ToInt("-123") + result3, _ := convertor.ToInt(float64(12.3)) + result4, err := convertor.ToInt("abc") + result5, _ := convertor.ToInt(true) - v, _ = convertor.ToFloat(1.12) - fmt.Println(v) //1 + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4, err) + fmt.Println(result5) + + // Output: + // 123 + // -123 + // 12 + // 0 strconv.ParseInt: parsing "": invalid syntax + // 0 } ``` - - ### ToJson

Convert interface to json string. If param can't be converted, will return "" and error.

@@ -322,14 +349,20 @@ 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}" + aMap := map[string]int{"a": 1, "b": 2, "c": 3} + result, err := ToJson(aMap) + + if err != nil { + fmt.Printf("%v", err) + } + + fmt.Println(result) + + // Output: + // {"a":1,"b":2,"c":3} } ``` - - ### ToMap

Convert a slice of structs to a map based on iteratee function.

@@ -358,16 +391,18 @@ func main() { {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"} + fmt.Println(result) + + // Output: + // map[100:Hello 101:Hi] } ``` - - ### ToPointer

Returns a pointer to passed value.

@@ -389,11 +424,13 @@ import ( func main() { result := convertor.ToPointer(123) - fmt.Println(*result) //123 + fmt.Println(*result) + + // Output: + // 123 } ``` - ### ToString

ToString convert value to string, for number, string, []byte, will convert to string. For other type (slice, map, array, struct) will call json.Marshal

@@ -414,13 +451,33 @@ import ( ) func main() { - fmt.Printf("%q", convertor.ToString(1)) //"1" - fmt.Printf("%q", convertor.ToString(1.1)) //"1.1" - fmt.Printf("%q", convertor.ToString([]int{1, 2, 3})) //"[1,2,3]" + result1 := convertor.ToString("") + result2 := convertor.ToString(nil) + result3 := convertor.ToString(0) + result4 := convertor.ToString(1.23) + result5 := convertor.ToString(true) + result6 := convertor.ToString(false) + result7 := convertor.ToString([]int{1, 2, 3}) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + fmt.Println(result6) + fmt.Println(result7) + + // Output: + // + // + // 0 + // 1.23 + // true + // false + // [1,2,3] } ``` - ### StructToMap

Convert struct to map, only convert exported field, struct field tag `json` should be set.

@@ -442,16 +499,19 @@ import ( func main() { type People struct { - Name string `json:"name"` - age int - } - p := People{ - "test", - 100, - } - pm, _ := convertor.StructToMap(p) + Name string `json:"name"` + age int + } + p := People{ + "test", + 100, + } + pm, _ := convertor.StructToMap(p) - fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test] + fmt.Println(pm) + + // Output: + // map[name:test] } ``` @@ -508,12 +568,13 @@ import ( func main() { byteData, _ := convertor.EncodeByte("abc") - fmt.Println(byteData) //[]byte{6, 12, 0, 3, 97, 98, 99} + fmt.Println(byteData) + + // Output: + // [6 12 0 3 97 98 99] } ``` - - ### DecodeByte

Decode byte data to target object. target should be a pointer instance.

@@ -536,7 +597,15 @@ import ( func main() { var result string byteData := []byte{6, 12, 0, 3, 97, 98, 99} - convertor.DecodeByte(byteData, &result) - fmt.Println(result) //"abc" + + err := convertor.DecodeByte(byteData, &result) + if err != nil { + return + } + + fmt.Println(result) + + // Output: + // abc } ``` \ No newline at end of file diff --git a/docs/convertor_zh-CN.md b/docs/convertor_zh-CN.md index 4af1281..f6d62c0 100644 --- a/docs/convertor_zh-CN.md +++ b/docs/convertor_zh-CN.md @@ -43,16 +43,15 @@ import ( ## 文档 - ### ColorHexToRGB -

颜色值十六进制转rgb

+

颜色值十六进制转rgb。

函数签名: ```go func ColorHexToRGB(colorHex string) (red, green, blue int) ``` -列子: +示例: ```go package main @@ -64,23 +63,25 @@ import ( func main() { colorHex := "#003366" - r, g, b := convertor.ColorHexToRGB(colorHex) - fmt.Println(r, g, b) //0,51,102 + r, g, b := convertor.ColorHexToRGB(colorHex) + + fmt.Println(r, g, b) + + // Output: + // 0 51 102 } ``` - - ### ColorRGBToHex -

颜色值rgb转十六进制

+

颜色值rgb转十六进制。

函数签名: ```go func ColorRGBToHex(red, green, blue int) string ``` -列子: +示例: ```go package main @@ -92,26 +93,27 @@ import ( func main() { r := 0 - g := 51 - b := 102 - colorHex := convertor.ColorRGBToHex(r, g, b) + g := 51 + b := 102 + colorHex := ColorRGBToHex(r, g, b) - fmt.Println(colorHex) //#003366 + fmt.Println(colorHex) + + // Output: + // #003366 } ``` - - ### ToBool -

字符串转布尔类型,使用strconv.ParseBool

+

字符串转布尔类型,使用strconv.ParseBool。

函数签名: ```go func ToBool(s string) (bool, error) ``` -列子: +示例: ```go package main @@ -122,32 +124,36 @@ import ( ) func main() { - v1, _ := convertor.ToBool("1") - fmt.Println(v1) //true + cases := []string{"1", "true", "True", "false", "False", "0", "123", "0.0", "abc"} - v2, _ := convertor.ToBool("true") - fmt.Println(v2) //true + for i := 0; i < len(cases); i++ { + result, _ := convertor.ToBool(cases[i]) + fmt.Println(result) + } - v3, _ := convertor.ToBool("True") - fmt.Println(v3) //true - - v4, _ := convertor.ToBool("123") - fmt.Println(v4) //false + // Output: + // true + // true + // true + // false + // false + // false + // false + // false + // false } ``` - - ### ToBytes -

interface转字节切片.

+

Interface转字节切片。

函数签名: ```go func ToBytes(data any) ([]byte, error) ``` -列子: +示例: ```go package main @@ -158,26 +164,28 @@ import ( ) func main() { - bytesData, err := convertor.ToBytes("0") + bytesData, err := convertor.ToBytes("abc") if err != nil { fmt.Println(err) } - fmt.Println(bytesData) //[]bytes{3, 4, 0, 0} + + fmt.Println(bytesData) + + // Output: + // [97 98 99] } ``` - - ### ToChar -

字符串转字符切片

+

字符串转字符切片。

函数签名: ```go func ToChar(s string) []string ``` -列子: +示例: ```go package main @@ -188,29 +196,31 @@ import ( ) func main() { - chars := convertor.ToChar("") - fmt.Println(chars) //[]string{""} + result1 := convertor.ToChar("") + result2 := convertor.ToChar("abc") + result3 := convertor.ToChar("1 2#3") - chars = convertor.ToChar("abc") - fmt.Println(chars) //[]string{"a", "b", "c"} + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - chars = convertor.ToChar("1 2#3") - fmt.Println(chars) //[]string{"1", " ", "2", "#", "3"} + // Output: + // [] + // [a b c] + // [1 2 # 3] } ``` - - ### ToChannel -

将切片转为只读channel

+

将切片转为只读channel。

函数签名: ```go func ToChannel[T any](array []T) <-chan T ``` -例子: +示例: ```go package main @@ -222,33 +232,31 @@ import ( func main() { ch := convertor.ToChannel([]int{1, 2, 3}) + result1 := <-ch + result2 := <-ch + result3 := <-ch - val1, _ := <-ch - fmt.Println(val1) //1 + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) - val2, _ := <-ch - fmt.Println(val2) //2 - - val3, _ := <-ch - fmt.Println(val3) //3 - - _, ok := <-ch - fmt.Println(ok) //false + // Output: + // 1 + // 2 + // 3 } ``` - - ### ToFloat -

将interface转成float64类型,如果参数无法转换,会返回0和error

+

将interface转成float64类型,如果参数无法转换,会返回0和error。

函数签名: ```go func ToFloat(value any) (float64, error) ``` -列子: +示例: ```go package main @@ -259,29 +267,40 @@ import ( ) func main() { - v, err := convertor.ToFloat("") - if err != nil { - fmt.Println(err) //strconv.ParseFloat: parsing "": invalid syntax - } - fmt.Println(v) //0 + result1, _ := convertor.ToFloat("") + result2, err := convertor.ToFloat("abc") + result3, _ := convertor.ToFloat("-1") + result4, _ := convertor.ToFloat("-.11") + result5, _ := convertor.ToFloat("1.23e3") + result6, _ := convertor.ToFloat(true) - v, _ = convertor.ToFloat("-.11") - fmt.Println(v) //-0.11 + fmt.Println(result1) + fmt.Println(result2, err) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + fmt.Println(result6) + + // Output: + // 0 + // 0 strconv.ParseFloat: parsing "": invalid syntax + // -1 + // -0.11 + // 1230 + // 0 } ``` - - ### ToInt -

将interface转成int64类型,如果参数无法转换,会返回0和error

+

将interface转成int64类型,如果参数无法转换,会返回0和error。

函数签名: ```go func ToInt(value any) (int64, error) ``` -例子: +示例: ```go package main @@ -292,29 +311,37 @@ import ( ) func main() { - v, err := convertor.ToInt("") - if err != nil { - fmt.Println(err) //strconv.ParseInt: parsing "": invalid syntax - } - fmt.Println(v) //0 + result1, _ := convertor.ToInt("123") + result2, _ := convertor.ToInt("-123") + result3, _ := convertor.ToInt(float64(12.3)) + result4, err := convertor.ToInt("abc") + result5, _ := convertor.ToInt(true) - v, _ = convertor.ToFloat(1.12) - fmt.Println(v) //1 + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4, err) + fmt.Println(result5) + + // Output: + // 123 + // -123 + // 12 + // 0 strconv.ParseInt: parsing "": invalid syntax + // 0 } ``` - - ### ToJson -

将interface转成json字符串,如果参数无法转换,会返回""和error

+

将interface转成json字符串,如果参数无法转换,会返回""和error。

函数签名: ```go func ToJson(value any) (string, error) ``` -列子: +示例: ```go package main @@ -325,24 +352,30 @@ 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}" + aMap := map[string]int{"a": 1, "b": 2, "c": 3} + result, err := ToJson(aMap) + + if err != nil { + fmt.Printf("%v", err) + } + + fmt.Println(result) + + // Output: + // {"a":1,"b":2,"c":3} } ``` - - ### ToMap -

将切片转为map

+

将切片转为map。

函数签名: ```go func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V ``` -例子: +示例: ```go package main @@ -361,26 +394,28 @@ func main() { {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"} + fmt.Println(result) + + // Output: + // map[100:Hello 101:Hi] } ``` - - ### ToPointer -

返回传入值的指针

+

返回传入值的指针。

函数签名: ```go func ToPointer[T any](value T) *T ``` -例子: +示例: ```go package main @@ -392,22 +427,23 @@ import ( func main() { result := convertor.ToPointer(123) - fmt.Println(*result) //123 + fmt.Println(*result) + + // Output: + // 123 } ``` - - ### ToString -

将值转换为字符串,对于数字、字符串、[]byte,将转换为字符串。 对于其他类型(切片、映射、数组、结构)将调用 json.Marshal

+

将值转换为字符串,对于数字、字符串、[]byte,将转换为字符串。 对于其他类型(切片、映射、数组、结构体)将调用 json.Marshal

函数签名: ```go func ToString(value any) string ``` -例子: +示例: ```go package main @@ -418,24 +454,43 @@ import ( ) func main() { - fmt.Printf("%q", convertor.ToString(1)) //"1" - fmt.Printf("%q", convertor.ToString(1.1)) //"1.1" - fmt.Printf("%q", convertor.ToString([]int{1, 2, 3})) //"[1,2,3]" + result1 := convertor.ToString("") + result2 := convertor.ToString(nil) + result3 := convertor.ToString(0) + result4 := convertor.ToString(1.23) + result5 := convertor.ToString(true) + result6 := convertor.ToString(false) + result7 := convertor.ToString([]int{1, 2, 3}) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + fmt.Println(result6) + fmt.Println(result7) + + // Output: + // + // + // 0 + // 1.23 + // true + // false + // [1,2,3] } ``` - - ### StructToMap -

将struct转成map,只会转换struct中可导出的字段。struct中导出字段需要设置json tag标记

+

将struct转成map,只会转换struct中可导出的字段。struct中导出字段需要设置json tag标记。

函数签名: ```go func StructToMap(value any) (map[string]any, error) ``` -列子: +示例: ```go package main @@ -447,31 +502,32 @@ import ( func main() { type People struct { - Name string `json:"name"` - age int - } - p := People{ - "test", - 100, - } - pm, _ := convertor.StructToMap(p) + Name string `json:"name"` + age int + } + p := People{ + "test", + 100, + } + pm, _ := convertor.StructToMap(p) - fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test] + fmt.Println(pm) + + // Output: + // map[name:test] } ``` - - ### MapToSlice -

map中key和value执行函数iteratee后,转为切片

+

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 @@ -491,18 +547,16 @@ func main() { } ``` - - ### EncodeByte -

将data编码成字节切片

+

将data编码成字节切片。

函数签名: ```go func EncodeByte(data any) ([]byte, error) ``` -例子: +示例: ```go package main @@ -514,22 +568,23 @@ import ( func main() { byteData, _ := convertor.EncodeByte("abc") - fmt.Println(byteData) //[]byte{6, 12, 0, 3, 97, 98, 99} + fmt.Println(byteData) + + // Output: + // [6 12 0 3 97 98 99] } ``` - - ### DecodeByte -

解码字节切片到目标对象,目标对象需要传入一个指针实例

+

解码字节切片到目标对象,目标对象需要传入一个指针实例。

函数签名: ```go func DecodeByte(data []byte, target any) error ``` -例子: +示例: ```go package main @@ -542,7 +597,15 @@ import ( func main() { var result string byteData := []byte{6, 12, 0, 3, 97, 98, 99} - convertor.DecodeByte(byteData, &result) - fmt.Println(result) //"abc" + + err := convertor.DecodeByte(byteData, &result) + if err != nil { + return + } + + fmt.Println(result) + + // Output: + // abc } ``` \ No newline at end of file diff --git a/docs/cryptor.md b/docs/cryptor.md index b56da45..1cb2da9 100644 --- a/docs/cryptor.md +++ b/docs/cryptor.md @@ -61,8 +61,6 @@ import ( ## Documentation - - ### AesEcbEncrypt

Encrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.

@@ -83,16 +81,19 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key)) + data := "hello" + key := "abcdefghijklmnop" - fmt.Println(string(encrypted)) + encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### AesEcbDecrypt

Decrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.

@@ -113,16 +114,19 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key)) - decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key)) - fmt.Println(string(decrypted)) //hello world + data := "hello" + key := "abcdefghijklmnop" + + encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### AesCbcEncrypt

Encrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.

@@ -143,16 +147,19 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key)) + data := "hello" + key := "abcdefghijklmnop" - fmt.Println(string(encrypted)) + encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### AesCbcDecrypt

Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.

@@ -174,16 +181,19 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key)) - decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key)) - fmt.Println(string(decrypted)) //hello world + data := "hello" + key := "abcdefghijklmnop" + + encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### AesCtrCrypt

Encrypt or decrypt data with key use AES CTR algorithm. Length of `key` param should be 16, 24 or 32.

@@ -205,17 +215,19 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key)) - decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key)) + data := "hello" + key := "abcdefghijklmnop" - fmt.Println(string(decrypted)) //hello world + encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### AesCfbEncrypt

Encrypt data with key use AES CFB algorithm. Length of `key` param should be 16, 24 or 32.

@@ -237,15 +249,19 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key)) - fmt.Println(string(encrypted)) + data := "hello" + key := "abcdefghijklmnop" + + encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### AesCfbDecrypt

Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.

@@ -267,16 +283,19 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key)) - decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key)) - fmt.Println(string(decrypted)) //hello world + data := "hello" + key := "abcdefghijklmnop" + + encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### AesOfbEncrypt

Enecrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.

@@ -298,15 +317,18 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key)) - fmt.Println(string(encrypted)) + data := "hello" + key := "abcdefghijklmnop" + + encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - - ### AesOfbDecrypt

Decrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.

@@ -328,17 +350,19 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key)) - decrypted := cryptor.AesOfbDecrypt(encrypted, []byte(key)) - - fmt.Println(string(decrypted)) //hello world + data := "hello" + key := "abcdefghijklmnop" + + encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### Base64StdEncode

Encode string with base64 encoding.

@@ -359,13 +383,13 @@ import ( ) func main() { - base64Str := cryptor.Base64StdEncode("hello world") - fmt.Println(base64Str) //aGVsbG8gd29ybGQ= + base64Str := cryptor.Base64StdEncode("hello") + fmt.Println(base64Str) + + // Output: + // aGVsbG8= } ``` - - - ### Base64StdDecode

Decode a base64 encoded string.

@@ -387,13 +411,14 @@ import ( ) func main() { - str := cryptor.Base64StdDecode("aGVsbG8gd29ybGQ=") - fmt.Println(str) //hello world + str := cryptor.Base64StdDecode("aGVsbG8=") + fmt.Println(str) + + // Output: + // hello } ``` - - ### DesEcbEncrypt

Encrypt data with key use DES ECB algorithm. Length of `key` param should be 8.

@@ -415,16 +440,19 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key)) + data := "hello" + key := "abcdefgh" - fmt.Println(string(encrypted)) + encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key)) + + decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - - ### DesEcbDecrypt

Decrypt data with key use DES ECB algorithm. Length of `key` param should be 8.

@@ -446,17 +474,20 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesEcbEncrypt([]byte(data), []byt(key) - decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key)) - - fmt.Println(string(decrypted)) //hello world + data := "hello" + key := "abcdefgh" + + encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key)) + + decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### DesCbcEncrypt

Encrypt data with key use DES CBC algorithm. Length of `key` param should be 8.

@@ -478,16 +509,19 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesCbcEncrypt([]byte(data), []byt(key) + data := "hello" + key := "abcdefgh" - fmt.Println(string(encrypted)) + encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### DesCbcDecrypt

Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.

@@ -509,17 +543,18 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesCbcEncrypt([]byte(data), []byt(key) + data := "hello" + key := "abcdefgh" + + encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key)) decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key)) - - fmt.Println(string(decrypted)) //hello world + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - - ### DesCtrCrypt

Encrypt or decrypt data with key use DES CTR algorithm. Length of `key` param should be 8.

@@ -541,17 +576,19 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key)) - decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key)) + data := "hello" + key := "abcdefgh" - fmt.Println(string(decrypted)) //hello world + encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### DesCfbEncrypt

Encrypt data with key use DES CFB algorithm. Length of `key` param should be 8.

@@ -573,15 +610,18 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesCfbEncrypt([]byte(data), []byt(key) - fmt.Println(string(encrypted)) + data := "hello" + key := "abcdefgh" + + encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - - ### DesCfbDecrypt

Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.

@@ -603,16 +643,18 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesCfbEncrypt([]byte(data), []byt(key) - decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key)) - fmt.Println(string(decrypted)) //hello world + data := "hello" + key := "abcdefgh" + + encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - - ### DesOfbEncrypt

Enecrypt data with key use DES OFB algorithm. Length of `key` param should be 8.

@@ -634,15 +676,18 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key)) - fmt.Println(string(encrypted)) + data := "hello" + key := "abcdefgh" + + encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - - ### DesOfbDecrypt

Decrypt data with key use DES OFB algorithm. Length of `key` param should be 8.

@@ -664,17 +709,19 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key)) - decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key)) - - fmt.Println(string(decrypted)) //hello world + data := "hello" + key := "abcdefgh" + + encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### HmacMd5

Get the md5 hmac hash of string.

@@ -696,13 +743,16 @@ import ( ) func main() { - s := cryptor.HmacMd5("hello world", "12345")) - fmt.Println(s) //5f4c9faaff0a1ad3007d9ddc06abe36d + str := "hello" + key := "12345" + + hms := cryptor.HmacMd5(str, key) + fmt.Println(hms) + + // Output: + // e834306eab892d872525d4918a7a639a } ``` - - - ### HmacSha1

Get the sha1 hmac hash of string.

@@ -724,13 +774,16 @@ import ( ) func main() { - s := cryptor.HmacSha1("hello world", "12345")) - fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd + str := "hello" + key := "12345" + + hms := cryptor.HmacSha1(str, key) + fmt.Println(hms) + + // Output: + // 5c6a9db0cccb92e36ed0323fd09b7f936de9ace0 } ``` - - - ### HmacSha256

Get the sha256 hmac hash of string

@@ -752,13 +805,17 @@ import ( ) func main() { - s := cryptor.HmacSha256("hello world", "12345")) - fmt.Println(s) //9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8 + str := "hello" + key := "12345" + + hms := cryptor.HmacSha256(str, key) + fmt.Println(hms) + + // Output: + // 315bb93c4e989862ba09cb62e05d73a5f376cb36f0d786edab0c320d059fde75 } ``` - - ### HmacSha512

Get the sha512 hmac hash of string.

@@ -780,14 +837,18 @@ import ( ) func main() { - s := cryptor.HmacSha512("hello world", "12345")) - fmt.Println(s) - //5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175 + str := "hello" + key := "12345" + + hms := cryptor.HmacSha512(str, key) + fmt.Println(hms) + + // Output: + // dd8f1290a9dd23d354e2526d9a2e9ce8cffffdd37cb320800d1c6c13d2efc363288376a196c5458daf53f8e1aa6b45a6d856303d5c0a2064bff9785861d48cfc } ``` - ### Md5String

Get the md5 value of string.

@@ -809,13 +870,16 @@ import ( ) func main() { - s := cryptor.Md5String("hello")) - fmt.Println(s) //5d41402abc4b2a76b9719d911017c592 + str := "hello" + + md5Str := cryptor.Md5String(str) + fmt.Println(md5Str) + + // Output: + // 5d41402abc4b2a76b9719d911017c592 } ``` - - ### Md5File

Get the md5 value of file.

@@ -842,8 +906,6 @@ func main() { } ``` - - ### Sha1

Get the sha1 value of string.

@@ -865,13 +927,16 @@ import ( ) func main() { - s := cryptor.Sha1("hello world")) - fmt.Println(s) //2aae6c35c94fcfb415dbe95f408b9ce91ee846ed + str := "hello" + + result := cryptor.Sha1(str) + fmt.Println(result) + + // Output: + // aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d } ``` - - ### Sha256

Get the sha256 value of string.

@@ -893,13 +958,16 @@ import ( ) func main() { - s := cryptor.Sha256("hello world")) - fmt.Println(s) //b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9 + str := "hello" + + result := cryptor.Sha256(str) + fmt.Println(result) + + // Output: + // 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 } ``` - - ### Sha512

Get the sha512 value of string.

@@ -921,13 +989,16 @@ import ( ) func main() { - s := cryptor.Sha512("hello world")) - fmt.Println(s) //309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f + str := "hello" + + result := cryptor.Sha512(str) + fmt.Println(result) + + // Output: + // 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043 } ``` - - ### GenerateRsaKey

Create the rsa public and private key file in current directory.

@@ -956,8 +1027,6 @@ func main() { } ``` - - ### RsaEncrypt

Encrypt data with public key file useing ras algorithm.

@@ -981,19 +1050,21 @@ import ( func main() { err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem") if err != nil { - fmt.Println(err) + return } - data := []byte("hello world") + data := []byte("hello") encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem") decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem") - fmt.Println(string(decrypted)) //hello world + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - ### RsaDecrypt

Decrypt data with private key file useing ras algorithm.

@@ -1017,14 +1088,17 @@ import ( func main() { err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem") if err != nil { - fmt.Println(err) + return } - data := []byte("hello world") + data := []byte("hello") encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem") decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem") - fmt.Println(string(decrypted)) //hello world + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` diff --git a/docs/cryptor_zh-CN.md b/docs/cryptor_zh-CN.md index b144c08..5f1650c 100644 --- a/docs/cryptor_zh-CN.md +++ b/docs/cryptor_zh-CN.md @@ -69,7 +69,7 @@ import ( ```go func AesEcbEncrypt(data, key []byte) []byte ``` -列子: +示例: ```go package main @@ -80,26 +80,29 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key)) + data := "hello" + key := "abcdefghijklmnop" - fmt.Println(string(encrypted)) + encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### AesEcbDecrypt -

使用AES ECB算法模式解密数据. 参数`key`的长度是16, 24 or 32。 +

使用AES ECB算法模式解密数据,参数`key`的长度是16, 24 or 32。

函数签名: ```go func AesEcbDecrypt(encrypted, key []byte) []byte ``` -列子: +示例: ```go package main @@ -110,26 +113,29 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key)) - decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key)) - fmt.Println(string(decrypted)) //hello world + data := "hello" + key := "abcdefghijklmnop" + + encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### AesCbcEncrypt -

使用AES CBC算法模式加密数据. 参数`key`的长度是16, 24 or 32。

+

使用AES CBC算法模式加密数据,参数`key`的长度是16, 24 or 32。

函数签名: ```go func AesCbcEncrypt(data, key []byte) []byte ``` -列子: +示例: ```go package main @@ -140,19 +146,22 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key)) + data := "hello" + key := "abcdefghijklmnop" - fmt.Println(string(encrypted)) + encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### AesCbcDecrypt -

使用AES CBC算法模式解密数据. 参数`key`的长度是16, 24 or 32。

+

使用AES CBC算法模式解密数据,参数`key`的长度是16, 24 or 32。

函数签名: @@ -160,7 +169,7 @@ func main() { func AesCbcDecrypt(encrypted, key []byte) []byte ``` -列子: +示例: ```go package main @@ -171,17 +180,19 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key)) - decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key)) - - fmt.Println(string(decrypted)) //hello world + data := "hello" + key := "abcdefghijklmnop" + + encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### AesCtrCrypt

使用AES CTR算法模式加密/解密数据,参数`key`的长度是16, 24 or 32。

@@ -192,7 +203,7 @@ func main() { func AesCtrCrypt(data, key []byte) []byte ``` -列子: +示例: ```go package main @@ -203,20 +214,22 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key)) - decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key)) + data := "hello" + key := "abcdefghijklmnop" - fmt.Println(string(decrypted)) //hello world + encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### AesCfbEncrypt -

使用AES CFB算法模式加密数据. 参数`key`的长度是16, 24 or 32。

+

使用AES CFB算法模式加密数据,参数`key`的长度是16, 24 or 32。

函数签名: @@ -224,7 +237,7 @@ func main() { func AesCfbEncrypt(data, key []byte) []byte ``` -列子: +示例: ```go package main @@ -235,18 +248,22 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key)) - fmt.Println(string(encrypted)) + data := "hello" + key := "abcdefghijklmnop" + + encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### AesCfbDecrypt -

使用AES CFB算法模式解密数据. 参数`key`的长度是16, 24 or 32。

+

使用AES CFB算法模式解密数据,参数`key`的长度是16, 24 or 32。

函数签名: @@ -254,7 +271,7 @@ func main() { func AesCfbDecrypt(encrypted, key []byte) []byte ``` -列子: +示例: ```go package main @@ -265,19 +282,22 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key)) - decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key)) - fmt.Println(string(decrypted)) //hello world + data := "hello" + key := "abcdefghijklmnop" + + encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### AesOfbEncrypt -

使用AES OFB算法模式加密数据. 参数`key`的长度是16, 24 or 32

+

使用AES OFB算法模式加密数据,参数`key`的长度是16, 24 or 32。

函数签名: @@ -285,7 +305,7 @@ func main() { func AesOfbEncrypt(data, key []byte) []byte ``` -列子: +示例: ```go package main @@ -296,18 +316,21 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key)) - fmt.Println(string(encrypted)) + data := "hello" + key := "abcdefghijklmnop" + + encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - - ### AesOfbDecrypt -

使用AES OFB算法模式解密数据. 参数`key`的长度是16, 24 or 32

+

使用AES OFB算法模式解密数据,参数`key`的长度是16, 24 or 32。

函数签名: @@ -315,7 +338,7 @@ func main() { func AesOfbDecrypt(encrypted, key []byte) []byte ``` -Example: +示例: ```go package main @@ -326,26 +349,29 @@ import ( ) func main() { - data := "hello world" - key := "abcdefghijklmnop" - encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key)) - decrypted := cryptor.AesOfbDecrypt(encrypted, []byte(key)) - fmt.Println(string(decrypted)) //hello world + data := "hello" + key := "abcdefghijklmnop" + + encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### Base64StdEncode -

将字符串base64编码

+

将字符串base64编码。

函数签名: ```go func Base64StdEncode(s string) string ``` -列子: +示例: ```go package main @@ -356,16 +382,16 @@ import ( ) func main() { - base64Str := cryptor.Base64StdEncode("hello world") - fmt.Println(base64Str) //aGVsbG8gd29ybGQ= + base64Str := cryptor.Base64StdEncode("hello") + fmt.Println(base64Str) + + // Output: + // aGVsbG8= } ``` - - - ### Base64StdDecode -

解码base64字符串

+

解码base64字符串。

函数签名: @@ -373,7 +399,7 @@ func main() { func Base64StdDecode(s string) string ``` -列子: +示例: ```go package main @@ -384,16 +410,17 @@ import ( ) func main() { - str := cryptor.Base64StdDecode("aGVsbG8gd29ybGQ=") - fmt.Println(str) //hello world + str := cryptor.Base64StdDecode("aGVsbG8=") + fmt.Println(str) + + // Output: + // hello } ``` - - ### DesEcbEncrypt -

使用DES ECB算法模式加密数据. 参数`key`的长度是8

+

使用DES ECB算法模式加密数据,参数`key`的长度是8。

函数签名: @@ -401,7 +428,7 @@ func main() { func DesEcbEncrypt(data, key []byte) []byte ``` -列子: +示例: ```go package main @@ -412,19 +439,22 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key)) + data := "hello" + key := "abcdefgh" - fmt.Println(string(encrypted)) + encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key)) + + decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - - ### DesEcbDecrypt -

使用DES ECB算法模式解密数据. 参数`key`的长度是8

+

使用DES ECB算法模式解决密数据,参数`key`的长度是8。

函数签名: @@ -432,7 +462,7 @@ func main() { func DesEcbDecrypt(encrypted, key []byte) []byte ``` -列子: +示例: ```go package main @@ -443,20 +473,23 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key)) - decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key)) - - fmt.Println(string(decrypted)) //hello world + data := "hello" + key := "abcdefgh" + + encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key)) + + decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### DesCbcEncrypt -

使用DES CBC算法模式加密数据. 参数`key`的长度是8

+

使用DES CBC算法模式加密数据,参数`key`的长度是8。

函数签名: @@ -464,7 +497,7 @@ func main() { func DesCbcEncrypt(data, key []byte) []byte ``` -列子: +示例: ```go package main @@ -475,19 +508,22 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key)) + data := "hello" + key := "abcdefgh" - fmt.Println(string(encrypted)) + encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### DesCbcDecrypt -

使用DES CBC算法模式解密数据. 参数`key`的长度是8

+

使用DES CBC算法模式解密数据,参数`key`的长度是8。

函数签名: @@ -495,7 +531,7 @@ func main() { func DesCbcDecrypt(encrypted, key []byte) []byte ``` -列子: +示例: ```go package main @@ -506,20 +542,21 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key)) - decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key)) - - fmt.Println(string(decrypted)) //hello world + data := "hello" + key := "abcdefgh" + + encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - - ### DesCtrCrypt -

使用DES CTR算法模式加密/解密数据. 参数`key`的长度是8

+

使用DES CTR算法模式加密/解密数据,参数`key`的长度是8

函数签名: @@ -527,7 +564,7 @@ func main() { func DesCtrCrypt(data, key []byte) []byte ``` -列子: +示例: ```go package main @@ -538,20 +575,22 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key)) - decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key)) + data := "hello" + key := "abcdefgh" - fmt.Println(string(decrypted)) //hello world + encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### DesCfbEncrypt -

使用DES CFB算法模式加密数据. 参数`key`的长度是8

+

使用DES CFB算法模式加密数据,参数`key`的长度是8。

函数签名: @@ -559,7 +598,7 @@ func main() { func DesCfbEncrypt(data, key []byte) []byte ``` -列子: +示例: ```go package main @@ -570,18 +609,21 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key)) - fmt.Println(string(encrypted)) + data := "hello" + key := "abcdefgh" + + encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - - ### DesCfbDecrypt -

使用DES CFB算法模式解密数据. 参数`key`的长度是8

+

使用DES CFB算法模式解决密数据,参数`key`的长度是8。

函数签名: @@ -589,7 +631,7 @@ func main() { func DesCfbDecrypt(encrypted, key []byte) []byte ``` -列子: +示例: ```go package main @@ -600,19 +642,21 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key)) - decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key)) - fmt.Println(string(decrypted)) //hello world + data := "hello" + key := "abcdefgh" + + encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - - ### DesOfbEncrypt -

使用DES OFB算法模式加密数据. 参数`key`的长度是8

+

使用DES OFB算法模式加密数据,参数`key`的长度是8。

函数签名: @@ -620,7 +664,7 @@ func main() { func DesOfbEncrypt(data, key []byte) []byte ``` -列子: +示例: ```go package main @@ -631,18 +675,21 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key)) - fmt.Println(string(encrypted)) + data := "hello" + key := "abcdefgh" + + encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - - ### DesOfbDecrypt -

使用DES OFB算法模式解密数据. 参数`key`的长度是8

+

使用DES OFB算法模式解密数据,参数`key`的长度是8。

函数签名: @@ -650,7 +697,7 @@ func main() { func DesOfbDecrypt(encrypted, key []byte) []byte ``` -列子: +示例: ```go package main @@ -661,19 +708,22 @@ import ( ) func main() { - data := "hello world" - key := "abcdefgh" - encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key)) - decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key)) - fmt.Println(string(decrypted)) //hello world + data := "hello" + key := "abcdefgh" + + encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key)) + decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key)) + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - - ### HmacMd5 -

获取字符串md5 hmac值

+

获取字符串md5 hmac值。

函数签名: @@ -681,7 +731,7 @@ func main() { func HmacMd5(data, key string) string ``` -列子: +示例: ```go package main @@ -692,16 +742,19 @@ import ( ) func main() { - s := cryptor.HmacMd5("hello world", "12345")) - fmt.Println(s) //5f4c9faaff0a1ad3007d9ddc06abe36d + str := "hello" + key := "12345" + + hms := cryptor.HmacMd5(str, key) + fmt.Println(hms) + + // Output: + // e834306eab892d872525d4918a7a639a } ``` - - - ### HmacSha1 -

获取字符串sha1 hmac值

+

获取字符串sha1 hmac值。

函数签名: @@ -709,7 +762,7 @@ func main() { func HmacSha1(data, key string) string ``` -列子: +示例: ```go package main @@ -720,16 +773,19 @@ import ( ) func main() { - s := cryptor.HmacSha1("hello world", "12345")) - fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd + str := "hello" + key := "12345" + + hms := cryptor.HmacSha1(str, key) + fmt.Println(hms) + + // Output: + // 5c6a9db0cccb92e36ed0323fd09b7f936de9ace0 } ``` - - - ### HmacSha256 -

获取字符串sha256 hmac值

+

获取字符串sha256 hmac值。

函数签名: @@ -737,7 +793,7 @@ func main() { func HmacSha256(data, key string) string ``` -列子: +示例: ```go package main @@ -748,16 +804,20 @@ import ( ) func main() { - s := cryptor.HmacSha256("hello world", "12345")) - fmt.Println(s) //9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8 + str := "hello" + key := "12345" + + hms := cryptor.HmacSha256(str, key) + fmt.Println(hms) + + // Output: + // 315bb93c4e989862ba09cb62e05d73a5f376cb36f0d786edab0c320d059fde75 } ``` - - ### HmacSha512 -

获取字符串sha512 hmac值

+

获取字符串sha512 hmac值。

函数签名: @@ -765,7 +825,7 @@ func main() { func HmacSha512(data, key string) string ``` -列子: +示例: ```go package main @@ -776,17 +836,21 @@ import ( ) func main() { - s := cryptor.HmacSha512("hello world", "12345")) - fmt.Println(s) - //5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175 + str := "hello" + key := "12345" + + hms := cryptor.HmacSha512(str, key) + fmt.Println(hms) + + // Output: + // dd8f1290a9dd23d354e2526d9a2e9ce8cffffdd37cb320800d1c6c13d2efc363288376a196c5458daf53f8e1aa6b45a6d856303d5c0a2064bff9785861d48cfc } ``` - ### Md5String -

获取字符串md5值

+

获取字符串md5值。

函数签名: @@ -794,7 +858,7 @@ func main() { func Md5String(s string) string ``` -列子: +示例: ```go package main @@ -805,16 +869,19 @@ import ( ) func main() { - s := cryptor.Md5String("hello")) - fmt.Println(s) //5d41402abc4b2a76b9719d911017c592 + str := "hello" + + md5Str := cryptor.Md5String(str) + fmt.Println(md5Str) + + // Output: + // 5d41402abc4b2a76b9719d911017c592 } ``` - - ### Md5File -

获取文件md5值.

+

获取文件md5值。

函数签名: @@ -822,7 +889,7 @@ func main() { func Md5File(filepath string) (string, error) ``` -列子: +示例: ```go package main @@ -838,11 +905,9 @@ func main() { } ``` - - ### Sha1 -

获取字符串sha1值

+

获取字符串sha1值。

函数签名: @@ -850,7 +915,7 @@ func main() { func Sha1(data string) string ``` -列子: +示例: ```go package main @@ -861,16 +926,19 @@ import ( ) func main() { - s := cryptor.Sha1("hello world")) - fmt.Println(s) //2aae6c35c94fcfb415dbe95f408b9ce91ee846ed + str := "hello" + + result := cryptor.Sha1(str) + fmt.Println(result) + + // Output: + // aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d } ``` - - ### Sha256 -

获取字符串sha256值

+

获取字符串sha256值。

函数签名: @@ -878,7 +946,7 @@ func main() { func Sha256(data string) string ``` -列子: +示例: ```go package main @@ -889,16 +957,19 @@ import ( ) func main() { - s := cryptor.Sha256("hello world")) - fmt.Println(s) //b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9 + str := "hello" + + result := cryptor.Sha256(str) + fmt.Println(result) + + // Output: + // 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 } ``` - - ### Sha512 -

获取字符串sha512值

+

获取字符串sha512值。

函数签名: @@ -906,7 +977,7 @@ func main() { func Sha512(data string) string ``` -列子: +示例: ```go package main @@ -917,16 +988,19 @@ import ( ) func main() { - s := cryptor.Sha512("hello world")) - fmt.Println(s) //309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f + str := "hello" + + result := cryptor.Sha512(str) + fmt.Println(result) + + // Output: + // 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043 } ``` - - ### GenerateRsaKey -

在当前目录下创建rsa私钥文件和公钥文件

+

在当前目录下创建rsa私钥文件和公钥文件。

函数签名: @@ -934,7 +1008,7 @@ func main() { func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error ``` -列子: +示例: ```go package main @@ -952,11 +1026,9 @@ func main() { } ``` - - ### RsaEncrypt -

用公钥文件ras加密数据

+

用公钥文件ras加密数据。

函数签名: @@ -964,7 +1036,7 @@ func main() { func RsaEncrypt(data []byte, pubKeyFileName string) []byte ``` -列子: +示例: ```go package main @@ -977,20 +1049,24 @@ import ( func main() { err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem") if err != nil { - fmt.Println(err) + return } - data := []byte("hello world") + + data := []byte("hello") encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem") decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem") - fmt.Println(string(decrypted)) //hello world + + fmt.Println(string(decrypted)) + + // Output: + // hello } ``` - ### RsaDecrypt -

用私钥文件rsa解密数据

+

用私钥文件rsa解密数据。

函数签名: @@ -998,7 +1074,7 @@ func main() { func RsaDecrypt(data []byte, privateKeyFileName string) []byte ``` -列子: +示例: ```go package main @@ -1011,14 +1087,16 @@ import ( func main() { err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem") if err != nil { - fmt.Println(err) + return } - data := []byte("hello world") + + data := []byte("hello") encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem") decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem") - fmt.Println(string(decrypted)) //hello world + + fmt.Println(string(decrypted)) + + // Output: + // hello } -``` - - - +``` \ No newline at end of file diff --git a/docs/strutil.md b/docs/strutil.md index 3365bc3..5f402da 100644 --- a/docs/strutil.md +++ b/docs/strutil.md @@ -64,19 +64,27 @@ import ( ) func main() { - s1 := strutil.After("lancet", "") - fmt.Println(s1) //lancet + result1 := strutil.After("foo", "") + result2 := strutil.After("foo", "foo") + result3 := strutil.After("foo/bar", "foo") + result4 := strutil.After("foo/bar", "/") + result5 := strutil.After("foo/bar/baz", "/") - s2 := strutil.After("github.com/test/lancet", "/") - fmt.Println(s2) //test/lancet + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) - s3 := strutil.After("github.com/test/lancet", "test") - fmt.Println(s3) // /lancet + // Output: + // foo + // + // /bar + // bar + // bar/baz } ``` - - ### AfterLast

Returns the substring after the last occurrence of a specified string in the source string.

@@ -94,20 +102,27 @@ import ( ) func main() { - s1 := strutil.AfterLast("lancet", "") - fmt.Println(s1) //lancet + result1 := strutil.AfterLast("foo", "") + result2 := strutil.AfterLast("foo", "foo") + result3 := strutil.AfterLast("foo/bar", "/") + result4 := strutil.AfterLast("foo/bar/baz", "/") + result5 := strutil.AfterLast("foo/bar/foo/baz", "foo") - s2 := strutil.AfterLast("github.com/test/lancet", "/") - fmt.Println(s2) //lancet + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) - s3 := strutil.AfterLast("github.com/test/test/lancet", "test") - fmt.Println(s3) // /test/lancet + // Output: + // foo + // + // bar + // baz + // /baz } ``` - - - ### Before

Returns the substring of the source string up to the first occurrence of the specified string.

@@ -125,20 +140,24 @@ import ( ) func main() { - s1 := strutil.Before("lancet", "") - fmt.Println(s1) //lancet + result1 := strutil.Before("foo", "") + result2 := strutil.Before("foo", "foo") + result3 := strutil.Before("foo/bar", "/") + result4 := strutil.Before("foo/bar/baz", "/") - s2 := strutil.Before("github.com/test/lancet", "/") - fmt.Println(s2) //github.com + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) - s3 := strutil.Before("github.com/test/lancet", "test") - fmt.Println(s3) // github.com/ + // Output: + // foo + // + // foo + // foo } ``` - - - ### BeforeLast

Returns the substring of the source string up to the last occurrence of the specified string.

@@ -156,18 +175,24 @@ import ( ) func main() { - s1 := strutil.BeforeLast("lancet", "") - fmt.Println(s1) //lancet + result1 := strutil.BeforeLast("foo", "") + result2 := strutil.BeforeLast("foo", "foo") + result3 := strutil.BeforeLast("foo/bar", "/") + result4 := strutil.BeforeLast("foo/bar/baz", "/") - s2 := strutil.BeforeLast("github.com/test/lancet", "/") - fmt.Println(s2) //github.com/test + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) - s3 := strutil.BeforeLast("github.com/test/test/lancet", "test") - fmt.Println(s3) //github.com/test/ + // Output: + // foo + // + // foo + // foo/bar } ``` - ### CamelCase

Coverts string to camelCase string, non letters and numbers will be ignored.

@@ -185,25 +210,22 @@ import ( ) func main() { - s1 := strutil.CamelCase("foo_bar") - fmt.Println(s1) //fooBar + strings := []string{"", "foobar", "&FOO:BAR$BAZ", "$foo%", "Foo-#1😄$_%^&*(1bar"} - s2 := strutil.CamelCase("Foo-Bar") - fmt.Println(s2) //fooBar - - s3 := strutil.CamelCase("Foo&bar") - fmt.Println(s3) //fooBar - - s4 := strutil.CamelCase("foo bar") - fmt.Println(s4) //fooBar - - s4 := strutil.CamelCase("Foo-#1😄$_%^&*(1bar") - fmt.Println(s4) //foo11Bar + for _, v := range strings { + s := strutil.CamelCase(v) + fmt.Println(s) + } + // Output: + // + // foobar + // fooBarBaz + // foo + // foo11Bar } ``` - ### KebabCase

KebabCase covert string to kebab-case, non letters and numbers will be ignored.

@@ -221,29 +243,29 @@ import ( ) func main() { - s1 := strutil.KebabCase("Foo Bar-") - fmt.Println(s1) //foo-bar + strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"} - s2 := strutil.KebabCase("foo_Bar") - fmt.Println(s2) //foo-bar + for _, v := range strings { + s := strutil.KebabCase(v) + fmt.Println(s) + } - s3 := strutil.KebabCase("fooBar") - fmt.Println(s3) //foo-bar - - s4 := strutil.KebabCase("__FOO_BAR__") - fmt.Println(s4) //foo-bar + // Output: + // + // foo-bar + // foo-bar + // foobar + // foo-1-1-bar } ``` - - ### UpperKebabCase

UpperKebabCase covert string to upper KEBAB-CASE, non letters and numbers will be ignored.

Signature: ```go -func KebabCase(s string) string +func UpperKebabCase(s string) string ``` Example: @@ -254,23 +276,22 @@ import ( ) func main() { - s1 := strutil.UpperKebabCase("Foo Bar-") - fmt.Println(s1) //FOO-BAR + strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"} - s2 := strutil.UpperKebabCase("foo_Bar") - fmt.Println(s2) //FOO-BAR + for _, v := range strings { + s := strutil.UpperKebabCase(v) + fmt.Println(s) + } - s3 := strutil.UpperKebabCase("fooBar") - fmt.Println(s3) //FOO-BAR - - s4 := strutil.UpperKebabCase("__FOO_BAR__") - fmt.Println(s4) //FOO-BAR + // Output: + // + // FOO-BAR + // FOO-BAR + // FOO-BAR + // FOO-1-1-BAR } ``` - - - ### Capitalize

Convert the first character of a string to upper case.

@@ -288,19 +309,22 @@ import ( ) func main() { - s1 := strutil.Capitalize("foo") - fmt.Println(s1) //foo + strings := []string{"", "Foo", "_foo", "fooBar", "foo-bar"} - s2 := strutil.Capitalize("Foo") - fmt.Println(s2) //foo + for _, v := range strings { + s := strutil.Capitalize(v) + fmt.Println(s) + } - s3 := strutil.Capitalize("FOo" - fmt.Println(s3) //fOo + // Output: + // + // Foo + // _foo + // Foobar + // Foo-bar } ``` - - ### IsString

Check if the value's data type is string.

@@ -318,51 +342,27 @@ import ( ) func main() { - fmt.Println(strutil.IsString("lancet")) //true - fmt.Println(strutil.IsString("")) //true + result1 := strutil.IsString("") + result2 := strutil.IsString("a") + result3 := strutil.IsString(1) + result4 := strutil.IsString(true) + result5 := strutil.IsString([]string{"a"}) - fmt.Println(strutil.IsString(1)) //false - fmt.Println(strutil.IsString("")) //false - fmt.Println(strutil.IsString([]string{})) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + + // Output: + // true + // true + // false + // false + // false } ``` - - -### KebabCase -

Covert string to kebab-case.

- -Signature: - -```go -func KebabCase(s string) string -``` -Example: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - s1 := strutil.KebabCase("Foo Bar-") - fmt.Println(s1) //foo-bar - - s2 := strutil.KebabCase("foo_Bar") - fmt.Println(s2) //foo-bar - - s3 := strutil.KebabCase("fooBar") - fmt.Println(s3) //foo-bar - - s4 := strutil.KebabCase("__FOO_BAR__") - fmt.Println(s4) //f-o-o-b-a-r -} -``` - - - - ### LowerFirst

Convert the first character of string to lower case.

@@ -380,23 +380,21 @@ import ( ) func main() { - s1 := strutil.LowerFirst("foo") - fmt.Println(s1) //foo + strings := []string{"", "bar", "BAr", "Bar大"} - s2 := strutil.LowerFirst("BAR") - fmt.Println(s2) //bAR + for _, v := range strings { + s := strutil.LowerFirst(v) + fmt.Println(s) + } - s3 := strutil.LowerFirst("FOo") - fmt.Println(s3) //fOo - - s4 := strutil.LowerFirst("fOo大") - fmt.Println(s4) //fOo大 + // Output: + // + // bar + // bAr + // bar大 } ``` - - - ### UpperFirst

Convert the first character of string to upper case.

@@ -414,23 +412,21 @@ import ( ) func main() { - s1 := strutil.UpperFirst("foo") - fmt.Println(s1) //Foo + strings := []string{"", "bar", "BAr", "bar大"} - s2 := strutil.UpperFirst("bAR") - fmt.Println(s2) //BAR + for _, v := range strings { + s := strutil.UpperFirst(v) + fmt.Println(s) + } - s3 := strutil.UpperFirst("FOo") - fmt.Println(s3) //FOo - - s4 := strutil.UpperFirst("fOo大") - fmt.Println(s4) //FOo大 + // Output: + // + // Bar + // BAr + // Bar大 } ``` - - - ### PadEnd

Pads string on the right side if it's shorter than size.

@@ -448,23 +444,33 @@ import ( ) func main() { - s1 := strutil.PadEnd("a", 1, "b") - fmt.Println(s1) //a + result1 := strutil.PadEnd("foo", 1, "bar") + result2 := strutil.PadEnd("foo", 2, "bar") + result3 := strutil.PadEnd("foo", 3, "bar") + result4 := strutil.PadEnd("foo", 4, "bar") + result5 := strutil.PadEnd("foo", 5, "bar") + result6 := strutil.PadEnd("foo", 6, "bar") + result7 := strutil.PadEnd("foo", 7, "bar") - s2 := strutil.PadEnd("a", 2, "b") - fmt.Println(s2) //ab + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + fmt.Println(result6) + fmt.Println(result7) - s3 := strutil.PadEnd("abcd", 6, "mno") - fmt.Println(s3) //abcdmn - - s4 := strutil.PadEnd("abc", 6, "ab") - fmt.Println(s4) //abcaba + // Output: + // foo + // foo + // foo + // foob + // fooba + // foobar + // foobarb } ``` - - - ### PadStart

Pads string on the left side if it's shorter than size.

@@ -482,23 +488,33 @@ import ( ) func main() { - s1 := strutil.PadStart("a", 1, "b") - fmt.Println(s1) //a + result1 := strutil.PadStart("foo", 1, "bar") + result2 := strutil.PadStart("foo", 2, "bar") + result3 := strutil.PadStart("foo", 3, "bar") + result4 := strutil.PadStart("foo", 4, "bar") + result5 := strutil.PadStart("foo", 5, "bar") + result6 := strutil.PadStart("foo", 6, "bar") + result7 := strutil.PadStart("foo", 7, "bar") - s2 := strutil.PadStart("a", 2, "b") - fmt.Println(s2) //ba + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + fmt.Println(result6) + fmt.Println(result7) - s3 := strutil.PadStart("abcd", 6, "mno") - fmt.Println(s3) //mnabcd - - s4 := strutil.PadStart("abc", 6, "ab") - fmt.Println(s4) //abaabc + // Output: + // foo + // foo + // foo + // bfoo + // bafoo + // barfoo + // barbfoo } ``` - - - ### Reverse

Return string whose char order is reversed to the given string.

@@ -516,16 +532,18 @@ import ( ) func main() { - s1 := strutil.ReverseStr("abc") - fmt.Println(s1) //cba + s := "foo" + rs := strutil.Reverse(s) - s2 := strutil.ReverseStr("12345") - fmt.Println(s2) //54321 + fmt.Println(s) + fmt.Println(rs) + + // Output: + // foo + // oof } ``` - - ### SnakeCase

Coverts string to snake_case, non letters and numbers will be ignored.

@@ -543,24 +561,22 @@ import ( ) func main() { - s1 := strutil.SnakeCase("Foo Bar-") - fmt.Println(s1) //foo_bar + strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"} - s2 := strutil.SnakeCase("foo_Bar") - fmt.Println(s2) //foo_bar + for _, v := range strings { + s := strutil.SnakeCase(v) + fmt.Println(s) + } - s3 := strutil.SnakeCase("fooBar") - fmt.Println(s3) //foo_bar - - s4 := strutil.SnakeCase("__FOO_BAR__") - fmt.Println(s4) //foo_bar - - s5 := strutil.SnakeCase("Foo-#1😄$_%^&*(1bar") - fmt.Println(s5) //foo_1_1_bar + // Output: + // + // foo_bar + // foo_bar + // foobar + // foo_1_1_bar } ``` - ### UpperSnakeCase

Coverts string to upper KEBAB-CASE, non letters and numbers will be ignored.

@@ -578,24 +594,22 @@ import ( ) func main() { - s1 := strutil.UpperSnakeCase("Foo Bar-") - fmt.Println(s1) //FOO_BAR + strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"} - s2 := strutil.UpperSnakeCase("foo_Bar") - fmt.Println(s2) //FOO_BAR + for _, v := range strings { + s := strutil.UpperSnakeCase(v) + fmt.Println(s) + } - s3 := strutil.UpperSnakeCase("fooBar") - fmt.Println(s3) //FOO_BAR - - s4 := strutil.UpperSnakeCase("__FOO_BAR__") - fmt.Println(s4) //FOO_BAR - - s5 := strutil.UpperSnakeCase("Foo-#1😄$_%^&*(1bar") - fmt.Println(s5) //FOO_1_1_BAR + // Output: + // + // FOO_BAR + // FOO_BAR + // FOO_BAR + // FOO_1_1_BAR } ``` - ### SplitEx

Split a given string whether the result contains empty string.

@@ -613,25 +627,28 @@ import ( ) func main() { - arr1 := strutil.SplitEx(" a b c ", "", true) - fmt.Println(arr1) //[]string{} + result1 := strutil.SplitEx(" a b c ", "", true) - arr2 := strutil.SplitEx(" a b c ", " ", false) - fmt.Println(arr2) //[]string{"", "a", "b", "c", ""} + result2 := strutil.SplitEx(" a b c ", " ", false) + result3 := strutil.SplitEx(" a b c ", " ", true) - arr3 := strutil.SplitEx(" a b c ", " ", true) - fmt.Println(arr3) //[]string{"a", "b", "c"} + result4 := strutil.SplitEx("a = b = c = ", " = ", false) + result5 := strutil.SplitEx("a = b = c = ", " = ", true) - arr4 := strutil.SplitEx(" a = b = c = ", " = ", false) - fmt.Println(arr4) //[]string{" a", "b", "c", ""} + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) - arr5 := strutil.SplitEx(" a = b = c = ", " = ", true) - fmt.Println(arr5) //[]string{" a", "b", "c"} + // Output: + // [] + // [ a b c ] + // [a b c] + // [a b c ] } ``` - - ### Substring

Returns a substring of the specified length starting at the specified offset position.

@@ -650,22 +667,26 @@ import ( func main() { result1 := strutil.Substring("abcde", 1, 3) - fmt.Println(result1) //bcd - result2 := strutil.Substring("abcde", 1, 5) - fmt.Println(result2) //bcde - result3 := strutil.Substring("abcde", -1, 3) - fmt.Println(result3) //e - result4 := strutil.Substring("abcde", -2, 2) - fmt.Println(result4) //de - result5 := strutil.Substring("abcde", -2, 3) - fmt.Println(result5) //de - result6 := strutil.Substring("你好,欢迎你", 0, 2) - fmt.Println(result6) //你好 + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + fmt.Println(result6) + + // Output: + // bcd + // bcde + // e + // de + // de + // 你好 } ``` @@ -686,26 +707,25 @@ import ( ) func main() { - s1 := strutil.Wrap("ab", "") - fmt.Println(s1) //ab + result1 := strutil.Wrap("foo", "") + result2 := strutil.Wrap("foo", "*") + result3 := strutil.Wrap("'foo'", "'") + result4 := strutil.Wrap("", "*") - s2 := strutil.Wrap("", "*") - fmt.Println(s2) //"" + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) - s3 := strutil.Wrap("ab", "*") - fmt.Println(s3) //*ab* - - s4 := strutil.Wrap("ab", "\"") - fmt.Println(s4) //\"ab\" - - s5 := strutil.Wrap("ab", "'") - fmt.Println(s5) //'ab' + // Output: + // foo + // *foo* + // ''foo'' + // } ``` - - ### Wrap

Unwrap a given string from anther string. will change source string.

@@ -723,20 +743,24 @@ import ( ) func main() { - s1 := strutil.Unwrap("ab", "") - fmt.Println(s1) //ab + result1 := strutil.Unwrap("foo", "") + result2 := strutil.Unwrap("*foo*", "*") + result3 := strutil.Unwrap("*foo", "*") + result4 := strutil.Unwrap("foo*", "*") + result5 := strutil.Unwrap("**foo**", "*") - s2 := strutil.Unwrap("ab", "*") - fmt.Println(s2) //ab + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) - s3 := strutil.Unwrap("**ab**", "*") - fmt.Println(s3) //*ab* - - s4 := strutil.Unwrap("*ab", "*") - fmt.Println(s4) //*ab - - s5 := strutil.Unwrap("***", "**") - fmt.Println(s5) //*** + // Output: + // foo + // foo + // *foo + // foo* + // *foo* } ``` diff --git a/docs/strutil_zh-CN.md b/docs/strutil_zh-CN.md index eea20b1..74b3960 100644 --- a/docs/strutil_zh-CN.md +++ b/docs/strutil_zh-CN.md @@ -49,14 +49,14 @@ import ( ### After -

返回源字符串中特定字符串首次出现时的位置之后的子字符串

+

返回源字符串中特定字符串首次出现时的位置之后的子字符串。

函数签名: ```go func After(s, char string) string ``` -例子: +示例: ```go import ( @@ -65,28 +65,36 @@ import ( ) func main() { - s1 := strutil.After("lancet", "") - fmt.Println(s1) //lancet + result1 := strutil.After("foo", "") + result2 := strutil.After("foo", "foo") + result3 := strutil.After("foo/bar", "foo") + result4 := strutil.After("foo/bar", "/") + result5 := strutil.After("foo/bar/baz", "/") - s2 := strutil.After("github.com/test/lancet", "/") - fmt.Println(s2) //test/lancet + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) - s3 := strutil.After("github.com/test/lancet", "test") - fmt.Println(s3) // /lancet + // Output: + // foo + // + // /bar + // bar + // bar/baz } ``` - - ### AfterLast -

返回源字符串中指定字符串最后一次出现时的位置之后的子字符串

+

返回源字符串中指定字符串最后一次出现时的位置之后的子字符串。

函数签名: ```go func AfterLast(s, char string) string ``` -例子: +示例: ```go import ( @@ -95,29 +103,36 @@ import ( ) func main() { - s1 := strutil.AfterLast("lancet", "") - fmt.Println(s1) //lancet + result1 := strutil.AfterLast("foo", "") + result2 := strutil.AfterLast("foo", "foo") + result3 := strutil.AfterLast("foo/bar", "/") + result4 := strutil.AfterLast("foo/bar/baz", "/") + result5 := strutil.AfterLast("foo/bar/foo/baz", "foo") - s2 := strutil.AfterLast("github.com/test/lancet", "/") - fmt.Println(s2) //lancet + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) - s3 := strutil.AfterLast("github.com/test/test/lancet", "test") - fmt.Println(s3) // /lancet + // Output: + // foo + // + // bar + // baz + // /baz } ``` - - - ### Before -

返回源字符串中指定字符串第一次出现时的位置之前的子字符串

+

返回源字符串中指定字符串第一次出现时的位置之前的子字符串。

函数签名: ```go func Before(s, char string) string ``` -例子: +示例: ```go import ( @@ -126,29 +141,33 @@ import ( ) func main() { - s1 := strutil.Before("lancet", "") - fmt.Println(s1) //lancet + result1 := strutil.Before("foo", "") + result2 := strutil.Before("foo", "foo") + result3 := strutil.Before("foo/bar", "/") + result4 := strutil.Before("foo/bar/baz", "/") - s2 := strutil.Before("github.com/test/lancet", "/") - fmt.Println(s2) //github.com + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) - s3 := strutil.Before("github.com/test/lancet", "test") - fmt.Println(s3) // github.com/ + // Output: + // foo + // + // foo + // foo } ``` - - - ### BeforeLast -

返回源字符串中指定字符串最后一次出现时的位置之前的子字符串

+

返回源字符串中指定字符串最后一次出现时的位置之前的子字符串。

函数签名: ```go func BeforeLast(s, char string) string ``` -例子: +示例: ```go import ( @@ -157,29 +176,33 @@ import ( ) func main() { - s1 := strutil.BeforeLast("lancet", "") - fmt.Println(s1) //lancet + result1 := strutil.BeforeLast("foo", "") + result2 := strutil.BeforeLast("foo", "foo") + result3 := strutil.BeforeLast("foo/bar", "/") + result4 := strutil.BeforeLast("foo/bar/baz", "/") - s2 := strutil.BeforeLast("github.com/test/lancet", "/") - fmt.Println(s2) //github.com/test + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) - s3 := strutil.BeforeLast("github.com/test/test/lancet", "test") - fmt.Println(s3) //github.com/test/ + // Output: + // foo + // + // foo + // foo/bar } ``` - - - ### CamelCase -

将字符串转换为驼峰式字符串, 非字母和数字会被忽略

+

将字符串转换为驼峰式字符串, 非字母和数字会被忽略。

函数签名: ```go func CamelCase(s string) string ``` -例子: +示例: ```go import ( @@ -188,35 +211,97 @@ import ( ) func main() { - s1 := strutil.CamelCase("foo_bar") - fmt.Println(s1) //fooBar + strings := []string{"", "foobar", "&FOO:BAR$BAZ", "$foo%", "Foo-#1😄$_%^&*(1bar"} - s2 := strutil.CamelCase("Foo-Bar") - fmt.Println(s2) //fooBar + for _, v := range strings { + s := strutil.CamelCase(v) + fmt.Println(s) + } - s3 := strutil.CamelCase("Foo&bar") - fmt.Println(s3) //fooBar - - s4 := strutil.CamelCase("foo bar") - fmt.Println(s4) //fooBar - - s4 := strutil.CamelCase("Foo-#1😄$_%^&*(1bar") - fmt.Println(s4) //foo11Bar + // Output: + // + // foobar + // fooBarBaz + // foo + // foo11Bar } ``` +### KebabCase +

将字符串转换为kebab-case, 非字母和数字会被忽略。

+函数签名: +```go +func KebabCase(s string) string +``` +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"} + + for _, v := range strings { + s := strutil.KebabCase(v) + fmt.Println(s) + } + + // Output: + // + // foo-bar + // foo-bar + // foobar + // foo-1-1-bar +} +``` + +### UpperKebabCase +

将字符串转换为大写KEBAB-CASE, 非字母和数字会被忽略。

+ +函数签名: + +```go +func UpperKebabCase(s string) string +``` +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/strutil" +) + +func main() { + strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"} + + for _, v := range strings { + s := strutil.UpperKebabCase(v) + fmt.Println(s) + } + + // Output: + // + // FOO-BAR + // FOO-BAR + // FOO-BAR + // FOO-1-1-BAR +} +``` ### Capitalize -

将字符串的第一个字符转换为大写

+

将字符串的第一个字符转换为大写。

函数签名: ```go func Capitalize(s string) string ``` -例子: +示例: ```go import ( @@ -225,28 +310,31 @@ import ( ) func main() { - s1 := strutil.Capitalize("foo") - fmt.Println(s1) //foo + strings := []string{"", "Foo", "_foo", "fooBar", "foo-bar"} - s2 := strutil.Capitalize("Foo") - fmt.Println(s2) //foo + for _, v := range strings { + s := strutil.Capitalize(v) + fmt.Println(s) + } - s3 := strutil.Capitalize("FOo" - fmt.Println(s3) //fOo + // Output: + // + // Foo + // _foo + // Foobar + // Foo-bar } ``` - - ### IsString -

判断传入参数的数据类型是否为字符串

+

判断传入参数的数据类型是否为字符串。

函数签名: ```go func IsString(v any) bool ``` -例子: +示例: ```go import ( @@ -255,93 +343,36 @@ import ( ) func main() { - fmt.Println(strutil.IsString("lancet")) //true - fmt.Println(strutil.IsString("")) //true + result1 := strutil.IsString("") + result2 := strutil.IsString("a") + result3 := strutil.IsString(1) + result4 := strutil.IsString(true) + result5 := strutil.IsString([]string{"a"}) - fmt.Println(strutil.IsString(1)) //false - fmt.Println(strutil.IsString("")) //false - fmt.Println(strutil.IsString([]string{})) //false + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + + // Output: + // true + // true + // false + // false + // false } ``` - - -### KebabCase -

将字符串转换为kebab-case, 非字母和数字会被忽略

- -函数签名: - -```go -func KebabCase(s string) string -``` -例子: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - s1 := strutil.KebabCase("Foo Bar-") - fmt.Println(s1) //foo-bar - - s2 := strutil.KebabCase("foo_Bar") - fmt.Println(s2) //foo-bar - - s3 := strutil.KebabCase("fooBar") - fmt.Println(s3) //foo-bar - - s4 := strutil.KebabCase("__FOO_BAR__") - fmt.Println(s4) //foo-bar -} -``` - - - -### UpperKebabCase -

将字符串转换为大写KEBAB-CASE, 非字母和数字会被忽略

- -函数签名: - -```go -func KebabCase(s string) string -``` -例子: - -```go -import ( - "fmt" - "github.com/duke-git/lancet/v2/strutil" -) - -func main() { - s1 := strutil.UpperKebabCase("Foo Bar-") - fmt.Println(s1) //FOO-BAR - - s2 := strutil.UpperKebabCase("foo_Bar") - fmt.Println(s2) //FOO-BAR - - s3 := strutil.UpperKebabCase("fooBar") - fmt.Println(s3) //FOO-BAR - - s4 := strutil.UpperKebabCase("__FOO_BAR__") - fmt.Println(s4) //FOO-BAR -} -``` - - - - ### LowerFirst -

将字符串的第一个字符转换为小写

+

将字符串的第一个字符转换为小写。

函数签名: ```go func LowerFirst(s string) string ``` -例子: +示例: ```go import ( @@ -350,32 +381,30 @@ import ( ) func main() { - s1 := strutil.LowerFirst("foo") - fmt.Println(s1) //foo + strings := []string{"", "bar", "BAr", "Bar大"} - s2 := strutil.LowerFirst("BAR") - fmt.Println(s2) //bAR + for _, v := range strings { + s := strutil.LowerFirst(v) + fmt.Println(s) + } - s3 := strutil.LowerFirst("FOo") - fmt.Println(s3) //fOo - - s4 := strutil.LowerFirst("fOo大") - fmt.Println(s4) //fOo大 + // Output: + // + // bar + // bAr + // bar大 } ``` - - - ### UpperFirst -

将字符串的第一个字符转换为大写形式

+

将字符串的第一个字符转换为大写形式。

函数签名: ```go func UpperFirst(s string) string ``` -例子: +示例: ```go import ( @@ -384,32 +413,30 @@ import ( ) func main() { - s1 := strutil.UpperFirst("foo") - fmt.Println(s1) //Foo + strings := []string{"", "bar", "BAr", "bar大"} - s2 := strutil.UpperFirst("bAR") - fmt.Println(s2) //BAR + for _, v := range strings { + s := strutil.UpperFirst(v) + fmt.Println(s) + } - s3 := strutil.UpperFirst("FOo") - fmt.Println(s3) //FOo - - s4 := strutil.UpperFirst("fOo大") - fmt.Println(s4) //FOo大 + // Output: + // + // Bar + // BAr + // Bar大 } ``` - - - ### PadEnd -

如果字符串长度短于size,则在右侧填充字符串

+

如果字符串长度短于size,则在右侧填充字符串。

函数签名: ```go func PadEnd(source string, size int, padStr string) string ``` -例子: +示例: ```go import ( @@ -418,32 +445,42 @@ import ( ) func main() { - s1 := strutil.PadEnd("a", 1, "b") - fmt.Println(s1) //a + result1 := strutil.PadEnd("foo", 1, "bar") + result2 := strutil.PadEnd("foo", 2, "bar") + result3 := strutil.PadEnd("foo", 3, "bar") + result4 := strutil.PadEnd("foo", 4, "bar") + result5 := strutil.PadEnd("foo", 5, "bar") + result6 := strutil.PadEnd("foo", 6, "bar") + result7 := strutil.PadEnd("foo", 7, "bar") - s2 := strutil.PadEnd("a", 2, "b") - fmt.Println(s2) //ab + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + fmt.Println(result6) + fmt.Println(result7) - s3 := strutil.PadEnd("abcd", 6, "mno") - fmt.Println(s3) //abcdmn - - s4 := strutil.PadEnd("abc", 6, "ab") - fmt.Println(s4) //abcaba + // Output: + // foo + // foo + // foo + // foob + // fooba + // foobar + // foobarb } ``` - - - ### PadStart -

如果字符串长度短于size,则在左侧填充字符串

+

如果字符串长度短于size,则在左侧填充字符串。

函数签名: ```go func PadStart(source string, size int, padStr string) string ``` -例子: +示例: ```go import ( @@ -452,32 +489,42 @@ import ( ) func main() { - s1 := strutil.PadStart("a", 1, "b") - fmt.Println(s1) //a + result1 := strutil.PadStart("foo", 1, "bar") + result2 := strutil.PadStart("foo", 2, "bar") + result3 := strutil.PadStart("foo", 3, "bar") + result4 := strutil.PadStart("foo", 4, "bar") + result5 := strutil.PadStart("foo", 5, "bar") + result6 := strutil.PadStart("foo", 6, "bar") + result7 := strutil.PadStart("foo", 7, "bar") - s2 := strutil.PadStart("a", 2, "b") - fmt.Println(s2) //ba + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + fmt.Println(result6) + fmt.Println(result7) - s3 := strutil.PadStart("abcd", 6, "mno") - fmt.Println(s3) //mnabcd - - s4 := strutil.PadStart("abc", 6, "ab") - fmt.Println(s4) //abaabc + // Output: + // foo + // foo + // foo + // bfoo + // bafoo + // barfoo + // barbfoo } ``` - - - ### Reverse -

返回字符顺序与给定字符串相反的字符串

+

返回字符顺序与给定字符串相反的字符串。

函数签名: ```go func Reverse(s string) string ``` -例子: +示例: ```go import ( @@ -486,25 +533,27 @@ import ( ) func main() { - s1 := strutil.ReverseStr("abc") - fmt.Println(s1) //cba + s := "foo" + rs := strutil.Reverse(s) - s2 := strutil.ReverseStr("12345") - fmt.Println(s2) //54321 + fmt.Println(s) + fmt.Println(rs) + + // Output: + // foo + // oof } ``` - - ### SnakeCase -

将字符串转换为snake_case形式, 非字母和数字会被忽略

+

将字符串转换为snake_case形式, 非字母和数字会被忽略。

函数签名: ```go func SnakeCase(s string) string ``` -例子: +示例: ```go import ( @@ -513,33 +562,31 @@ import ( ) func main() { - s1 := strutil.SnakeCase("Foo Bar-") - fmt.Println(s1) //foo_bar + strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"} - s2 := strutil.SnakeCase("foo_Bar") - fmt.Println(s2) //foo_bar + for _, v := range strings { + s := strutil.SnakeCase(v) + fmt.Println(s) + } - s3 := strutil.SnakeCase("fooBar") - fmt.Println(s3) //foo_bar - - s4 := strutil.SnakeCase("__FOO_BAR__") - fmt.Println(s4) //foo_bar - - s5 := strutil.SnakeCase("Foo-#1😄$_%^&*(1bar") - fmt.Println(s5) //foo_1_1_bar + // Output: + // + // foo_bar + // foo_bar + // foobar + // foo_1_1_bar } ``` - ### UpperSnakeCase -

将字符串转换为大写SNAKE_CASE形式, 非字母和数字会被忽略

+

将字符串转换为大写SNAKE_CASE形式, 非字母和数字会被忽略。

函数签名: ```go func SnakeCase(s string) string ``` -例子: +示例: ```go import ( @@ -548,34 +595,31 @@ import ( ) func main() { - s1 := strutil.UpperSnakeCase("Foo Bar-") - fmt.Println(s1) //FOO_BAR + strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"} - s2 := strutil.UpperSnakeCase("foo_Bar") - fmt.Println(s2) //FOO_BAR + for _, v := range strings { + s := strutil.UpperSnakeCase(v) + fmt.Println(s) + } - s3 := strutil.UpperSnakeCase("fooBar") - fmt.Println(s3) //FOO_BAR - - s4 := strutil.UpperSnakeCase("__FOO_BAR__") - fmt.Println(s4) //FOO_BAR - - s5 := strutil.UpperSnakeCase("Foo-#1😄$_%^&*(1bar") - fmt.Println(s5) //FOO_1_1_BAR + // Output: + // + // FOO_BAR + // FOO_BAR + // FOO_BAR + // FOO_1_1_BAR } ``` - - ### SplitEx -

分割字符串为切片,removeEmptyString参数指定是否去除空字符串

+

分割字符串为切片,removeEmptyString参数指定是否去除空字符串。

函数签名: ```go func SplitEx(s, sep string, removeEmptyString bool) []string ``` -例子: +示例: ```go import ( @@ -584,32 +628,37 @@ import ( ) func main() { - arr1 := strutil.SplitEx(" a b c ", "", true) - fmt.Println(arr1) //[]string{} + result1 := strutil.SplitEx(" a b c ", "", true) - arr2 := strutil.SplitEx(" a b c ", " ", false) - fmt.Println(arr2) //[]string{"", "a", "b", "c", ""} + result2 := strutil.SplitEx(" a b c ", " ", false) + result3 := strutil.SplitEx(" a b c ", " ", true) - arr3 := strutil.SplitEx(" a b c ", " ", true) - fmt.Println(arr3) //[]string{"a", "b", "c"} + result4 := strutil.SplitEx("a = b = c = ", " = ", false) + result5 := strutil.SplitEx("a = b = c = ", " = ", true) - arr4 := strutil.SplitEx(" a = b = c = ", " = ", false) - fmt.Println(arr4) //[]string{" a", "b", "c", ""} + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) - arr5 := strutil.SplitEx(" a = b = c = ", " = ", true) - fmt.Println(arr5) //[]string{" a", "b", "c"} + // Output: + // [] + // [ a b c ] + // [a b c] + // [a b c ] } ``` ### Substring -

根据指定的位置和长度截取子字符串

+

根据指定的位置和长度截取字符串。

函数签名: ```go func Substring(s string, offset int, length uint) string ``` -例子: +示例: ```go import ( @@ -619,35 +668,38 @@ import ( func main() { result1 := strutil.Substring("abcde", 1, 3) - fmt.Println(result1) //bcd - result2 := strutil.Substring("abcde", 1, 5) - fmt.Println(result2) //bcde - result3 := strutil.Substring("abcde", -1, 3) - fmt.Println(result3) //e - result4 := strutil.Substring("abcde", -2, 2) - fmt.Println(result4) //de - result5 := strutil.Substring("abcde", -2, 3) - fmt.Println(result5) //de - result6 := strutil.Substring("你好,欢迎你", 0, 2) - fmt.Println(result6) //你好 + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) + fmt.Println(result6) + + // Output: + // bcd + // bcde + // e + // de + // de + // 你好 } ``` - ### Wrap -

用另一个字符串包裹一个字符串

+

用另一个字符串包裹一个字符串。

函数签名: ```go func Wrap(str string, wrapWith string) string ``` -例子: +示例: ```go import ( @@ -656,35 +708,34 @@ import ( ) func main() { - s1 := strutil.Wrap("ab", "") - fmt.Println(s1) //ab + result1 := strutil.Wrap("foo", "") + result2 := strutil.Wrap("foo", "*") + result3 := strutil.Wrap("'foo'", "'") + result4 := strutil.Wrap("", "*") - s2 := strutil.Wrap("", "*") - fmt.Println(s2) //"" - - s3 := strutil.Wrap("ab", "*") - fmt.Println(s3) //*ab* - - s4 := strutil.Wrap("ab", "\"") - fmt.Println(s4) //\"ab\" - - s5 := strutil.Wrap("ab", "'") - fmt.Println(s5) //'ab' + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // foo + // *foo* + // ''foo'' + // } ``` - - -### Unwrap -

用另一个字符串解开包裹一个字符串

+### Wrap +

用另一个字符串解开包裹一个字符串。

函数签名: ```go func Unwrap(str string, wrapToken string) string ``` -例子: +示例: ```go import ( @@ -693,28 +744,23 @@ import ( ) func main() { - s1 := strutil.Unwrap("ab", "") - fmt.Println(s1) //ab + result1 := strutil.Unwrap("foo", "") + result2 := strutil.Unwrap("*foo*", "*") + result3 := strutil.Unwrap("*foo", "*") + result4 := strutil.Unwrap("foo*", "*") + result5 := strutil.Unwrap("**foo**", "*") - s2 := strutil.Unwrap("ab", "*") - fmt.Println(s2) //ab + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + fmt.Println(result5) - s3 := strutil.Unwrap("**ab**", "*") - fmt.Println(s3) //*ab* - - s4 := strutil.Unwrap("*ab", "*") - fmt.Println(s4) //*ab - - s5 := strutil.Unwrap("***", "**") - fmt.Println(s5) //*** + // Output: + // foo + // foo + // *foo + // foo* + // *foo* } -``` - - - - - - - - - +``` \ No newline at end of file