mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-08 14:42:27 +08:00
Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1acf977b24 | ||
|
|
5c878d0873 | ||
|
|
ab0716a472 | ||
|
|
3c2e0ca5b3 | ||
|
|
551e66ba29 | ||
|
|
4eeeabb227 | ||
|
|
b3437fdddf | ||
|
|
312dcab369 | ||
|
|
0cb89f4f46 | ||
|
|
a2dec87995 | ||
|
|
9094cb29bf | ||
|
|
ddb992ad2f | ||
|
|
24f18aaaec | ||
|
|
36169874e5 | ||
|
|
23aeb6ac99 | ||
|
|
a1984f0a59 | ||
|
|
c95db23d2c | ||
|
|
fbf251d805 | ||
|
|
337f08a04b | ||
|
|
c6a7371049 |
10
README.md
10
README.md
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -37,7 +37,7 @@ go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
|||||||
|
|
||||||
2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.3.1. </b>
|
2. <b>For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.3.1. </b>
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet@v1.3.0 // below go1.18, install latest version of v1.x.x
|
go get github.com/duke-git/lancet@v1.3.1 // below go1.18, install latest version of v1.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@@ -50,7 +50,7 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
Here takes the string function ReverseStr (reverse order string) as an example, and the strutil package needs to be imported.
|
Here takes the string function Reverse (reverse order string) as an example, and the strutil package needs to be imported.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -62,7 +62,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := "hello"
|
s := "hello"
|
||||||
rs := strutil.ReverseStr(s)
|
rs := strutil.Reverse(s)
|
||||||
fmt.Println(rs) //olleh
|
fmt.Println(rs) //olleh
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -435,7 +435,7 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
- [UpperFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperFirst)
|
- [UpperFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperFirst)
|
||||||
- [PadEnd](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadEnd)
|
- [PadEnd](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadEnd)
|
||||||
- [PadStart](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadStart)
|
- [PadStart](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadStart)
|
||||||
- [ReverseStr](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#ReverseStr)
|
- [Reverse](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Reverse)
|
||||||
- [SnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SnakeCase)
|
- [SnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SnakeCase)
|
||||||
- [SplitEx](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SplitEx)
|
- [SplitEx](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SplitEx)
|
||||||
- [Wrap](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap)
|
- [Wrap](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
[](https://pkg.go.dev/github.com/duke-git/lancet/v2)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet/v2)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
@@ -37,7 +37,7 @@ go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
|
|||||||
|
|
||||||
2. <b>使用go1.18以下版本的用户,必须安装v1.x.x。目前最新的v1版本是v1.3.1。</b>
|
2. <b>使用go1.18以下版本的用户,必须安装v1.x.x。目前最新的v1版本是v1.3.1。</b>
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet@v1.3.0 // 使用go1.18以下版本, 必须安装v1.x.x版本
|
go get github.com/duke-git/lancet@v1.3.1 // 使用go1.18以下版本, 必须安装v1.x.x版本
|
||||||
```
|
```
|
||||||
|
|
||||||
## 用法
|
## 用法
|
||||||
@@ -50,7 +50,7 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
|
|
||||||
## 例子
|
## 例子
|
||||||
|
|
||||||
此处以字符串工具函数ReverseStr(逆序字符串)为例,需要导入strutil包:
|
此处以字符串工具函数Reverse(逆序字符串)为例,需要导入strutil包:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -62,7 +62,7 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := "hello"
|
s := "hello"
|
||||||
rs := strutil.ReverseStr(s)
|
rs := strutil.Reverse(s)
|
||||||
fmt.Println(rs) //olleh
|
fmt.Println(rs) //olleh
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -431,7 +431,7 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
- [UpperFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperFirst)
|
- [UpperFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperFirst)
|
||||||
- [PadEnd](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadEnd)
|
- [PadEnd](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadEnd)
|
||||||
- [PadStart](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadStart)
|
- [PadStart](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadStart)
|
||||||
- [ReverseStr](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#ReverseStr)
|
- [Reverse](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Reverse)
|
||||||
- [SnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SnakeCase)
|
- [SnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SnakeCase)
|
||||||
- [SplitEx](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SplitEx)
|
- [SplitEx](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SplitEx)
|
||||||
- [Wrap](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap)
|
- [Wrap](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap)
|
||||||
@@ -489,7 +489,6 @@ import "github.com/duke-git/lancet/v2/validator"
|
|||||||
- [IsUrl](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsUrl)
|
- [IsUrl](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsUrl)
|
||||||
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsWeakPassword)
|
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsWeakPassword)
|
||||||
|
|
||||||
validator.md#IsWeakPassword)
|
|
||||||
### 19. xerror包实现一些错误处理函数
|
### 19. xerror包实现一些错误处理函数
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|||||||
@@ -65,11 +65,15 @@ func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// QuickSort quick sorting for slice, lowIndex is 0 and highIndex is len(slice)-1
|
// QuickSort quick sorting for slice, lowIndex is 0 and highIndex is len(slice)-1
|
||||||
func QuickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
|
func QuickSort[T any](slice []T, comparator lancetconstraints.Comparator) {
|
||||||
|
quickSort(slice, 0, len(slice)-1, comparator)
|
||||||
|
}
|
||||||
|
|
||||||
|
func quickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) {
|
||||||
if lowIndex < highIndex {
|
if lowIndex < highIndex {
|
||||||
p := partition(slice, lowIndex, highIndex, comparator)
|
p := partition(slice, lowIndex, highIndex, comparator)
|
||||||
QuickSort(slice, lowIndex, p-1, comparator)
|
quickSort(slice, lowIndex, p-1, comparator)
|
||||||
QuickSort(slice, p+1, highIndex, comparator)
|
quickSort(slice, p+1, highIndex, comparator)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,14 +30,6 @@ func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// var peoples = []people{
|
|
||||||
// {Name: "a", Age: 20},
|
|
||||||
// {Name: "b", Age: 10},
|
|
||||||
// {Name: "c", Age: 17},
|
|
||||||
// {Name: "d", Age: 8},
|
|
||||||
// {Name: "e", Age: 28},
|
|
||||||
// }
|
|
||||||
|
|
||||||
type intComparator struct{}
|
type intComparator struct{}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
@@ -53,8 +45,6 @@ func (c *intComparator) Compare(v1 any, v2 any) int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// var intSlice = []int{2, 1, 5, 3, 6, 4}
|
|
||||||
|
|
||||||
func TestBubbleSortForStructSlice(t *testing.T) {
|
func TestBubbleSortForStructSlice(t *testing.T) {
|
||||||
asssert := internal.NewAssert(t, "TestBubbleSortForStructSlice")
|
asssert := internal.NewAssert(t, "TestBubbleSortForStructSlice")
|
||||||
|
|
||||||
@@ -151,7 +141,7 @@ func TestQuickSort(t *testing.T) {
|
|||||||
{Name: "e", Age: 28},
|
{Name: "e", Age: 28},
|
||||||
}
|
}
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
QuickSort(peoples, 0, len(peoples)-1, comparator)
|
QuickSort(peoples, comparator)
|
||||||
|
|
||||||
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
expected := "[{d 8} {b 10} {c 17} {a 20} {e 28}]"
|
||||||
actual := fmt.Sprintf("%v", peoples)
|
actual := fmt.Sprintf("%v", peoples)
|
||||||
133
datastructure/hashmap/hashmap.go
Normal file
133
datastructure/hashmap/hashmap.go
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
|
||||||
|
// Use of this source code is governed by MIT license
|
||||||
|
|
||||||
|
// Package datastructure implements some data structure. eg. list, linklist, stack, queue, tree, graph.
|
||||||
|
package datastructure
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"hash/fnv"
|
||||||
|
)
|
||||||
|
|
||||||
|
var defaultMapCapacity uint64 = 1 << 10
|
||||||
|
|
||||||
|
type mapNode struct {
|
||||||
|
key any
|
||||||
|
value any
|
||||||
|
next *mapNode
|
||||||
|
}
|
||||||
|
|
||||||
|
//HashMap implements a hash map
|
||||||
|
type HashMap struct {
|
||||||
|
capacity uint64
|
||||||
|
size uint64
|
||||||
|
table []*mapNode
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHashMap return a HashMap instance
|
||||||
|
func NewHashMap() *HashMap {
|
||||||
|
return &HashMap{
|
||||||
|
capacity: defaultMapCapacity,
|
||||||
|
table: make([]*mapNode, defaultMapCapacity),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewHashMapWithCapacity return a HashMap instance with given size and capacity
|
||||||
|
func NewHashMapWithCapacity(size, capacity uint64) *HashMap {
|
||||||
|
return &HashMap{
|
||||||
|
size: size,
|
||||||
|
capacity: capacity,
|
||||||
|
table: make([]*mapNode, capacity),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get return the value of given key in hashmap
|
||||||
|
func (hm *HashMap) Get(key any) any {
|
||||||
|
hashValue := hm.hash(key)
|
||||||
|
node := hm.table[hashValue]
|
||||||
|
if node != nil {
|
||||||
|
return node.value
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put new key value in hashmap
|
||||||
|
func (hm *HashMap) Put(key any, value any) {
|
||||||
|
hm.putValue(hm.hash(key), key, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hm *HashMap) putValue(hash uint64, key, value any) {
|
||||||
|
if hm.capacity == 0 {
|
||||||
|
hm.capacity = defaultMapCapacity
|
||||||
|
hm.table = make([]*mapNode, defaultMapCapacity)
|
||||||
|
}
|
||||||
|
|
||||||
|
node := hm.table[hash]
|
||||||
|
if node == nil {
|
||||||
|
hm.table[hash] = newMapNode(key, value)
|
||||||
|
} else if node.key == key {
|
||||||
|
hm.table[hash] = newMapNodeWithNext(key, value, node)
|
||||||
|
} else {
|
||||||
|
hm.resize()
|
||||||
|
hm.putValue(hash, value, value)
|
||||||
|
}
|
||||||
|
hm.size++
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete item by given key in hashmap
|
||||||
|
func (hm *HashMap) Delete(key any) {
|
||||||
|
hash := hm.hash(key)
|
||||||
|
node := hm.table[hash]
|
||||||
|
if node == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hm.table = append(hm.table[:hash], hm.table[hash+1:]...)
|
||||||
|
hm.size--
|
||||||
|
}
|
||||||
|
|
||||||
|
// Contains checks if given key is in hashmap or not
|
||||||
|
func (hm *HashMap) Contains(key any) bool {
|
||||||
|
node := hm.table[hm.hash(key)]
|
||||||
|
return node != nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hm *HashMap) resize() {
|
||||||
|
hm.capacity <<= 1
|
||||||
|
|
||||||
|
tempTable := hm.table
|
||||||
|
|
||||||
|
for i := 0; i < len(tempTable); i++ {
|
||||||
|
node := tempTable[i]
|
||||||
|
if node == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
hm.table[hm.hash(node.key)] = node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hm *HashMap) hash(key any) uint64 {
|
||||||
|
h := fnv.New64a()
|
||||||
|
_, _ = h.Write([]byte(fmt.Sprintf("%v", key)))
|
||||||
|
|
||||||
|
hashValue := h.Sum64()
|
||||||
|
|
||||||
|
return (hm.capacity - 1) & (hashValue ^ (hashValue >> 16))
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMapNode(key, value any) *mapNode {
|
||||||
|
return &mapNode{
|
||||||
|
key: key,
|
||||||
|
value: value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMapNodeWithNext(key, value any, next *mapNode) *mapNode {
|
||||||
|
return &mapNode{
|
||||||
|
key: key,
|
||||||
|
value: value,
|
||||||
|
next: next,
|
||||||
|
}
|
||||||
|
}
|
||||||
42
datastructure/hashmap/hashmap_test.go
Normal file
42
datastructure/hashmap/hashmap_test.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package datastructure
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/v2/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHashMap_PutAndGet(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestHashMap_PutAndGet")
|
||||||
|
|
||||||
|
hm := NewHashMap()
|
||||||
|
|
||||||
|
hm.Put("abc", 3)
|
||||||
|
assert.Equal(3, hm.Get("abc"))
|
||||||
|
assert.IsNil(hm.Get("abcd"))
|
||||||
|
|
||||||
|
hm.Put("abc", 4)
|
||||||
|
assert.Equal(4, hm.Get("abc"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHashMap_Delete(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestHashMap_Delete")
|
||||||
|
|
||||||
|
hm := NewHashMap()
|
||||||
|
|
||||||
|
hm.Put("abc", 3)
|
||||||
|
assert.Equal(3, hm.Get("abc"))
|
||||||
|
|
||||||
|
hm.Delete("abc")
|
||||||
|
assert.IsNil(hm.Get("abc"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHashMap_Contains(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestHashMap_Contains")
|
||||||
|
|
||||||
|
hm := NewHashMap()
|
||||||
|
assert.Equal(false, hm.Contains("abc"))
|
||||||
|
|
||||||
|
hm.Put("abc", 3)
|
||||||
|
assert.Equal(true, hm.Contains("abc"))
|
||||||
|
}
|
||||||
@@ -25,6 +25,20 @@ func NewMaxHeap[T any](comparator lancetconstraints.Comparator) *MaxHeap[T] {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BuildMaxHeap builds a MaxHeap instance with data and given comparator.
|
||||||
|
func BuildMaxHeap[T any](data []T, comparator lancetconstraints.Comparator) *MaxHeap[T] {
|
||||||
|
heap := &MaxHeap[T]{
|
||||||
|
data: make([]T, 0, len(data)),
|
||||||
|
comparator: comparator,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, v := range data {
|
||||||
|
heap.Push(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return heap
|
||||||
|
}
|
||||||
|
|
||||||
// Push value into the heap
|
// Push value into the heap
|
||||||
func (h *MaxHeap[T]) Push(value T) {
|
func (h *MaxHeap[T]) Push(value T) {
|
||||||
h.data = append(h.data, value)
|
h.data = append(h.data, value)
|
||||||
|
|||||||
@@ -20,6 +20,20 @@ func (c *intComparator) Compare(v1, v2 any) int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMaxHeap_BuildMaxHeap(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestMaxHeap_BuildMaxHeap")
|
||||||
|
|
||||||
|
values := []int{6, 5, 2, 4, 7, 10, 12, 1, 3, 8, 9, 11}
|
||||||
|
heap := BuildMaxHeap(values, &intComparator{})
|
||||||
|
|
||||||
|
expected := []int{12, 9, 11, 4, 8, 10, 7, 1, 3, 5, 6, 2}
|
||||||
|
assert.Equal(expected, heap.data)
|
||||||
|
|
||||||
|
assert.Equal(12, heap.Size())
|
||||||
|
|
||||||
|
heap.PrintStructure()
|
||||||
|
}
|
||||||
|
|
||||||
func TestMaxHeap_Push(t *testing.T) {
|
func TestMaxHeap_Push(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestMaxHeap_Push")
|
assert := internal.NewAssert(t, "TestMaxHeap_Push")
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package datastructure
|
package datastructure
|
||||||
|
|
||||||
// Set is a data container, like slice, but element of set is not duplicate
|
// Set is a data container, like slice, but element of set is not duplicate
|
||||||
type Set[T comparable] map[T]bool
|
type Set[T comparable] map[T]struct{}
|
||||||
|
|
||||||
// NewSet return a instance of set
|
// NewSet return a instance of set
|
||||||
func NewSet[T comparable](values ...T) Set[T] {
|
func NewSet[T comparable](values ...T) Set[T] {
|
||||||
@@ -13,7 +13,7 @@ func NewSet[T comparable](values ...T) Set[T] {
|
|||||||
// Add value to set
|
// Add value to set
|
||||||
func (s Set[T]) Add(values ...T) {
|
func (s Set[T]) Add(values ...T) {
|
||||||
for _, v := range values {
|
for _, v := range values {
|
||||||
s[v] = true
|
s[v] = struct{}{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ Package algorithm implements some basic algorithm. eg. sort, search.
|
|||||||
|
|
||||||
## Source
|
## Source
|
||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/algorithm/sorter.go](https://github.com/duke-git/lancet/blob/main/algorithm/sorter.go)
|
- [https://github.com/duke-git/lancet/blob/main/algorithm/sort.go](https://github.com/duke-git/lancet/blob/main/algorithm/sort.go)
|
||||||
- [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go)
|
- [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go)
|
||||||
- [https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go)
|
- [https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go)
|
||||||
|
|
||||||
@@ -248,7 +248,7 @@ func main() {
|
|||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func QuickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator)
|
func QuickSort[T any](slice []T comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -278,7 +278,7 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.QuickSort(intSlice, 0, len(intSlice)-1, comparator)
|
algorithm.QuickSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ algorithm算法包实现一些基本算法,sort,search,lrucache。
|
|||||||
|
|
||||||
## 源码
|
## 源码
|
||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/algorithm/sorter.go](https://github.com/duke-git/lancet/blob/main/algorithm/sorter.go)
|
- [https://github.com/duke-git/lancet/blob/main/algorithm/sort.go](https://github.com/duke-git/lancet/blob/main/algorithm/sort.go)
|
||||||
- [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go)
|
- [https://github.com/duke-git/lancet/blob/main/algorithm/search.go](https://github.com/duke-git/lancet/blob/main/algorithm/search.go)
|
||||||
- [https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go)
|
- [https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go](https://github.com/duke-git/lancet/blob/main/algorithm/lru_cache.go)
|
||||||
|
|
||||||
@@ -249,7 +249,7 @@ func main() {
|
|||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func QuickSort[T any](slice []T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) []T
|
func QuickSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -279,7 +279,7 @@ func main() {
|
|||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
intSlice := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.QuickSort(intSlice, 0, len(intSlice)-1, comparator)
|
algorithm.QuickSort(intSlice, comparator)
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
||||||
}
|
}
|
||||||
|
|||||||
208
docs/datastructure/hashmap.md
Normal file
208
docs/datastructure/hashmap.md
Normal file
@@ -0,0 +1,208 @@
|
|||||||
|
# HashMap
|
||||||
|
|
||||||
|
HashMap is a key value map data structure.
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
- [https://github.com/duke-git/lancet/blob/main/datastructure/hashmap/hashmap.go](https://github.com/duke-git/lancet/blob/main/datastructure/hashmap/hashmap.go)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Index
|
||||||
|
|
||||||
|
- [NewHashMap](#NewHashMap)
|
||||||
|
- [NewHashMapWithCapacity](#NewHashMapWithCapacity)
|
||||||
|
- [Get](#Get)
|
||||||
|
- [Put](#Put)
|
||||||
|
- [Delete](#Delete)
|
||||||
|
- [Contains](#Contains)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
### <span id="NewHashMap">NewHashMap</span>
|
||||||
|
|
||||||
|
<p>Make a HashMap instance with default capacity is 1 << 10.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func NewHashMap() *HashMap
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
fmt.Println(hm)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="NewHashMap">NewHashMap</span>
|
||||||
|
|
||||||
|
<p>Make a HashMap instance with given size and capacity.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func NewHashMapWithCapacity(size, capacity uint64) *HashMap
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMapWithCapacity(uint64(100), uint64(1000))
|
||||||
|
fmt.Println(hm)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Get">Get</span>
|
||||||
|
|
||||||
|
<p>Get the value of given key in hashmap</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Get(key any) any
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
val := hm.Get("a")
|
||||||
|
|
||||||
|
fmt.Println(val) //nil
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Put">Put</span>
|
||||||
|
|
||||||
|
<p>Put new key value in hashmap, then return value</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Put(key any, value any) any
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
|
||||||
|
val := hm.Get("a")
|
||||||
|
fmt.Println(val) //1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Delete">Delete</span>
|
||||||
|
|
||||||
|
<p>Delete key-value item by given key in hashmap.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Delete(key any)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
val := hm.Get("a")
|
||||||
|
fmt.Println(val) //1
|
||||||
|
|
||||||
|
hm.Delete("a")
|
||||||
|
val = hm.Get("a")
|
||||||
|
fmt.Println(val) //nil
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Contains">Contains</span>
|
||||||
|
|
||||||
|
<p>Checks if given key is in hashmap or not.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Contains(key any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
|
||||||
|
fmt.Println(hm.Contains("a")) //true
|
||||||
|
fmt.Println(hm.Contains("b")) //false
|
||||||
|
}
|
||||||
|
```
|
||||||
209
docs/datastructure/hashmap_zh-CN.md
Normal file
209
docs/datastructure/hashmap_zh-CN.md
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
# HashMap
|
||||||
|
|
||||||
|
HashMap 数据结构实现
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 源码
|
||||||
|
|
||||||
|
- [https://github.com/duke-git/lancet/blob/main/datastructure/hashmap/hashmap.go](https://github.com/duke-git/lancet/blob/main/datastructure/hashmap/hashmap.go)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 用法
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 目录
|
||||||
|
|
||||||
|
- [HashMap](#hashmap)
|
||||||
|
- [源码](#源码)
|
||||||
|
- [用法](#用法)
|
||||||
|
- [目录](#目录)
|
||||||
|
- [API 文档](#api-文档)
|
||||||
|
- [<span id="NewHashMap">NewHashMap</span>](#newhashmap)
|
||||||
|
- [<span id="NewHashMapWithCapacity">NewHashMapWithCapacity</span>](#newhashmapwithcapacity)
|
||||||
|
- [<span id="Get">Get</span>](#get)
|
||||||
|
- [<span id="Put">Put</span>](#put)
|
||||||
|
- [<span id="Delete">Delete</span>](#delete)
|
||||||
|
- [<span id="Contains">Contains</span>](#contains)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## API 文档
|
||||||
|
|
||||||
|
### <span id="NewHashMap">NewHashMap</span>
|
||||||
|
|
||||||
|
<p>新建默认容量(1 << 10)的HashMap指针实例</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func NewHashMap() *HashMap
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
fmt.Println(hm)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="NewHashMapWithCapacity">NewHashMapWithCapacity</span>
|
||||||
|
|
||||||
|
<p>新建指定容量和长度的HashMap指针实例.</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func NewHashMapWithCapacity(size, capacity uint64) *HashMap
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMapWithCapacity(uint64(100), uint64(1000))
|
||||||
|
fmt.Println(hm)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Get">Get</span>
|
||||||
|
|
||||||
|
<p>在hashmap中根据key获取值</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Get(key any) any
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
val := hm.Get("a")
|
||||||
|
|
||||||
|
fmt.Println(val) //nil
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Put">Put</span>
|
||||||
|
|
||||||
|
<p>将key-value放入hashmap中</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Put(key any, value any) any
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
|
||||||
|
val := hm.Get("a")
|
||||||
|
fmt.Println(val) //1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Delete">Delete</span>
|
||||||
|
|
||||||
|
<p>将指定的key从hashmap中删除</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Delete(key any)
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
val := hm.Get("a")
|
||||||
|
fmt.Println(val) //1
|
||||||
|
|
||||||
|
hm.Delete("a")
|
||||||
|
val = hm.Get("a")
|
||||||
|
fmt.Println(val) //nil
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="Contains">Contains</span>
|
||||||
|
|
||||||
|
<p>判断hashmap中是否包含指定的key</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Contains(key any) bool
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
hm := heap.NewHashMap()
|
||||||
|
hm.Put("a", 1)
|
||||||
|
|
||||||
|
fmt.Println(hm.Contains("a")) //true
|
||||||
|
fmt.Println(hm.Contains("b")) //false
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -32,7 +32,7 @@ import (
|
|||||||
- [UpperFirst](#UpperFirst)
|
- [UpperFirst](#UpperFirst)
|
||||||
- [PadEnd](#PadEnd)
|
- [PadEnd](#PadEnd)
|
||||||
- [PadStart](#PadStart)
|
- [PadStart](#PadStart)
|
||||||
- [ReverseStr](#ReverseStr)
|
- [Reverse](#Reverse)
|
||||||
- [SnakeCase](#SnakeCase)
|
- [SnakeCase](#SnakeCase)
|
||||||
- [SplitEx](#SplitEx)
|
- [SplitEx](#SplitEx)
|
||||||
- [Wrap](#Wrap)
|
- [Wrap](#Wrap)
|
||||||
@@ -430,13 +430,13 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReverseStr">ReverseStr</span>
|
### <span id="Reverse">Reverse</span>
|
||||||
<p>Return string whose char order is reversed to the given string.</p>
|
<p>Return string whose char order is reversed to the given string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ReverseStr(s string) string
|
func Reverse(s string) string
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ import (
|
|||||||
- [UpperFirst](#UpperFirst)
|
- [UpperFirst](#UpperFirst)
|
||||||
- [PadEnd](#PadEnd)
|
- [PadEnd](#PadEnd)
|
||||||
- [PadStart](#PadStart)
|
- [PadStart](#PadStart)
|
||||||
- [ReverseStr](#ReverseStr)
|
- [Reverse](#Reverse)
|
||||||
- [SnakeCase](#SnakeCase)
|
- [SnakeCase](#SnakeCase)
|
||||||
- [SplitEx](#SplitEx)
|
- [SplitEx](#SplitEx)
|
||||||
- [Wrap](#Wrap)
|
- [Wrap](#Wrap)
|
||||||
@@ -431,13 +431,13 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReverseStr">ReverseStr</span>
|
### <span id="Reverse">Reverse</span>
|
||||||
<p>返回字符顺序与给定字符串相反的字符串</p>
|
<p>返回字符顺序与给定字符串相反的字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ReverseStr(s string) string
|
func Reverse(s string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ func TestHttpGet(t *testing.T) {
|
|||||||
resp, err := HttpGet(url, header)
|
resp, err := HttpGet(url, header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
t.FailNow()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
@@ -40,7 +39,6 @@ func TestHttpPost(t *testing.T) {
|
|||||||
resp, err := HttpPost(url, header, nil, bodyParams)
|
resp, err := HttpPost(url, header, nil, bodyParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
t.FailNow()
|
|
||||||
}
|
}
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
t.Log("response: ", resp.StatusCode, string(body))
|
t.Log("response: ", resp.StatusCode, string(body))
|
||||||
@@ -67,7 +65,6 @@ func TestHttpPostFormData(t *testing.T) {
|
|||||||
resp, err := HttpPost(apiUrl, header, postData, nil)
|
resp, err := HttpPost(apiUrl, header, postData, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
t.FailNow()
|
|
||||||
}
|
}
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
t.Log("response: ", resp.StatusCode, string(body))
|
t.Log("response: ", resp.StatusCode, string(body))
|
||||||
@@ -89,7 +86,6 @@ func TestHttpPut(t *testing.T) {
|
|||||||
resp, err := HttpPut(url, header, nil, bodyParams)
|
resp, err := HttpPut(url, header, nil, bodyParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
t.FailNow()
|
|
||||||
}
|
}
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
t.Log("response: ", resp.StatusCode, string(body))
|
t.Log("response: ", resp.StatusCode, string(body))
|
||||||
@@ -111,7 +107,6 @@ func TestHttpPatch(t *testing.T) {
|
|||||||
resp, err := HttpPatch(url, header, nil, bodyParams)
|
resp, err := HttpPatch(url, header, nil, bodyParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
t.FailNow()
|
|
||||||
}
|
}
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
t.Log("response: ", resp.StatusCode, string(body))
|
t.Log("response: ", resp.StatusCode, string(body))
|
||||||
@@ -122,7 +117,6 @@ func TestHttpDelete(t *testing.T) {
|
|||||||
resp, err := HttpDelete(url)
|
resp, err := HttpDelete(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
t.FailNow()
|
|
||||||
}
|
}
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
t.Log("response: ", resp.StatusCode, string(body))
|
t.Log("response: ", resp.StatusCode, string(body))
|
||||||
@@ -148,7 +142,6 @@ func TestParseResponse(t *testing.T) {
|
|||||||
resp, err := HttpGet(url, header)
|
resp, err := HttpGet(url, header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
t.FailNow()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Todo struct {
|
type Todo struct {
|
||||||
@@ -162,7 +155,6 @@ func TestParseResponse(t *testing.T) {
|
|||||||
err = ParseHttpResponse(resp, toDoResp)
|
err = ParseHttpResponse(resp, toDoResp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
t.FailNow()
|
|
||||||
}
|
}
|
||||||
t.Log("response: ", toDoResp)
|
t.Log("response: ", toDoResp)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -208,8 +208,8 @@ func IsString(v any) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReverseStr return string whose char order is reversed to the given string
|
// Reverse return string whose char order is reversed to the given string
|
||||||
func ReverseStr(s string) string {
|
func Reverse(s string) string {
|
||||||
r := []rune(s)
|
r := []rune(s)
|
||||||
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
|
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
|
||||||
r[i], r[j] = r[j], r[i]
|
r[i], r[j] = r[j], r[i]
|
||||||
|
|||||||
@@ -143,11 +143,11 @@ func TestIsString(t *testing.T) {
|
|||||||
assert.Equal(false, IsString([]string{}))
|
assert.Equal(false, IsString([]string{}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestReverseStr(t *testing.T) {
|
func TestReverse(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestReverseStr")
|
assert := internal.NewAssert(t, "TestReverse")
|
||||||
|
|
||||||
assert.Equal("cba", ReverseStr("abc"))
|
assert.Equal("cba", Reverse("abc"))
|
||||||
assert.Equal("54321", ReverseStr("12345"))
|
assert.Equal("54321", Reverse("12345"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWrap(t *testing.T) {
|
func TestWrap(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user