mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Compare commits
11 Commits
90ce2705ca
...
v2.1.7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6314889c6a | ||
|
|
294bd5a5ed | ||
|
|
ca44815fd5 | ||
|
|
bcd1cabf80 | ||
|
|
4edefcca67 | ||
|
|
fab24c8d12 | ||
|
|
604acd9b07 | ||
|
|
531cb19fd1 | ||
|
|
4f0161ca53 | ||
|
|
73362b7f69 | ||
|
|
b289f2975b |
26
README.md
26
README.md
@@ -1,10 +1,10 @@
|
|||||||
<div align=center>
|
<div align=center>
|
||||||
<img src="./logo.png" width="200" height="200"/>
|
<a href="https://uvdream.github.io/lancet-docs/en/"><img src="./logo.png" width="200" height="200"/></a>
|
||||||
|
|
||||||
<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)
|
||||||
@@ -35,9 +35,9 @@ English | [简体中文](./README_zh-CN.md)
|
|||||||
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
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.2. </b>
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet@v1.3.1 // below go1.18, install latest version of v1.x.x
|
go get github.com/duke-git/lancet@v1.3.2 // below go1.18, install latest version of v1.x.x
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@@ -68,7 +68,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
## API Documentation
|
## API Documentation
|
||||||
|
## [lancet API doc](https://uvdream.github.io/lancet-docs/) Thanks [@UvDream](https://github.com/UvDream) for contributing.
|
||||||
### 1. Algorithm package implements some basic algorithm. eg. sort, search.
|
### 1. Algorithm package implements some basic algorithm. eg. sort, search.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -351,11 +351,17 @@ import "github.com/duke-git/lancet/v2/netutil"
|
|||||||
- [GetRequestPublicIp](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetRequestPublicIp)
|
- [GetRequestPublicIp](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetRequestPublicIp)
|
||||||
- [IsPublicIP](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsPublicIP)
|
- [IsPublicIP](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsPublicIP)
|
||||||
- [IsInternalIP](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsInternalIP)
|
- [IsInternalIP](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsInternalIP)
|
||||||
- [HttpGet](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpGet)
|
- [HttpRequest](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpRequest)
|
||||||
- [HttpDelete](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpDelete)
|
- [HttpClient](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpClient)
|
||||||
- [HttpPost](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPost)
|
- [SendRequest](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#SendRequest)
|
||||||
- [HttpPut](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPut)
|
- [DecodeResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#DecodeResponse)
|
||||||
- [HttpPatch](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPatch)
|
- [StructToUrlValues](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#StructToUrlValues)
|
||||||
|
|
||||||
|
- [HttpGet<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpGet)
|
||||||
|
- [HttpDelete<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpDelete)
|
||||||
|
- [HttpPost<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPost)
|
||||||
|
- [HttpPut<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPut)
|
||||||
|
- [HttpPatch<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPatch)
|
||||||
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ParseHttpResponse)
|
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ParseHttpResponse)
|
||||||
|
|
||||||
### 14. Random package implements some basic functions to generate random int and string.
|
### 14. Random package implements some basic functions to generate random int and string.
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
<div align=center>
|
<div align=center>
|
||||||
<img src="./logo.png" width="200" height="200"/>
|
<a href="https://uvdream.github.io/lancet-docs/"><img src="./logo.png" width="200" height="200"/><a/>
|
||||||
|
|
||||||
<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)
|
||||||
@@ -35,9 +35,9 @@
|
|||||||
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
|
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.2。</b>
|
||||||
```go
|
```go
|
||||||
go get github.com/duke-git/lancet@v1.3.1 // 使用go1.18以下版本, 必须安装v1.x.x版本
|
go get github.com/duke-git/lancet@v1.3.2 // 使用go1.18以下版本, 必须安装v1.x.x版本
|
||||||
```
|
```
|
||||||
|
|
||||||
## 用法
|
## 用法
|
||||||
@@ -68,7 +68,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
## API文档
|
## API文档
|
||||||
|
## [lancet API doc](https://uvdream.github.io/lancet-docs/) 感谢[@UvDream](https://github.com/UvDream)整理
|
||||||
### 1. algorithm算法包实现一些基本算法。eg. sort, search.
|
### 1. algorithm算法包实现一些基本算法。eg. sort, search.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -348,11 +348,17 @@ import "github.com/duke-git/lancet/v2/netutil"
|
|||||||
- [GetRequestPublicIp](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetRequestPublicIp)
|
- [GetRequestPublicIp](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetRequestPublicIp)
|
||||||
- [IsPublicIP](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsPublicIP)
|
- [IsPublicIP](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsPublicIP)
|
||||||
- [IsInternalIP](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsInternalIP)
|
- [IsInternalIP](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsInternalIP)
|
||||||
- [HttpGet](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpGet)
|
- [HttpRequest](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpRequest)
|
||||||
- [HttpDelete](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpDelete)
|
- [HttpClient](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpClient)
|
||||||
- [HttpPost](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPost)
|
- [SendRequest](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#SendRequest)
|
||||||
- [HttpPut](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPut)
|
- [DecodeResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#DecodeResponse)
|
||||||
- [HttpPatch](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPatch)
|
- [StructToUrlValues](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#StructToUrlValues)
|
||||||
|
|
||||||
|
- [HttpGet<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpGet)
|
||||||
|
- [HttpDelete<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpDelete)
|
||||||
|
- [HttpPost<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPost)
|
||||||
|
- [HttpPut<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPut)
|
||||||
|
- [HttpPatch<sup>Deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPatch)
|
||||||
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ParseHttpResponse)
|
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ParseHttpResponse)
|
||||||
|
|
||||||
### 14. random随机数生成器包,可以生成随机[]bytes, int, string。
|
### 14. random随机数生成器包,可以生成随机[]bytes, int, string。
|
||||||
|
|||||||
@@ -93,6 +93,46 @@ func (hm *HashMap) Contains(key any) bool {
|
|||||||
return node != nil
|
return node != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Iterate executes iteratee funcation for every key and value pair of hashmap (random order)
|
||||||
|
func (hm *HashMap) Iterate(iteratee func(key, value any)) {
|
||||||
|
if hm.size > 0 {
|
||||||
|
for i := 0; i < len(hm.table); i++ {
|
||||||
|
item := hm.table[i]
|
||||||
|
if item != nil {
|
||||||
|
iteratee(item.key, item.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keys returns a slice of the hashmap's keys (random order)
|
||||||
|
func (hm *HashMap) Keys() []any {
|
||||||
|
keys := make([]any, int(hm.size))
|
||||||
|
index := 0
|
||||||
|
if hm.size > 0 {
|
||||||
|
hm.Iterate(func(key, value any) {
|
||||||
|
keys[index] = key
|
||||||
|
index++
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
|
// Values returns a slice of the hashmap's keys (random order)
|
||||||
|
func (hm *HashMap) Values() []any {
|
||||||
|
values := make([]any, int(hm.size))
|
||||||
|
index := 0
|
||||||
|
if hm.size > 0 {
|
||||||
|
hm.Iterate(func(key, value any) {
|
||||||
|
values[index] = value
|
||||||
|
index++
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return values
|
||||||
|
}
|
||||||
|
|
||||||
func (hm *HashMap) resize() {
|
func (hm *HashMap) resize() {
|
||||||
hm.capacity <<= 1
|
hm.capacity <<= 1
|
||||||
|
|
||||||
|
|||||||
@@ -52,3 +52,20 @@ func TestHashMap_Contains(t *testing.T) {
|
|||||||
hm.Put("abc", 3)
|
hm.Put("abc", 3)
|
||||||
assert.Equal(true, hm.Contains("abc"))
|
assert.Equal(true, hm.Contains("abc"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHashMap_KeysValues(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestHashMap_KeysValues")
|
||||||
|
|
||||||
|
hm := NewHashMap()
|
||||||
|
|
||||||
|
hm.Put("a", 1)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
keys := hm.Keys()
|
||||||
|
values := hm.Values()
|
||||||
|
t.Log(keys, values)
|
||||||
|
|
||||||
|
assert.Equal(3, len(values))
|
||||||
|
assert.Equal(3, len(keys))
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,24 +4,24 @@
|
|||||||
// Package datetime implements some functions to format date and time.
|
// Package datetime implements some functions to format date and time.
|
||||||
// Note:
|
// Note:
|
||||||
// 1. `format` param in FormatTimeToStr function should be as flow:
|
// 1. `format` param in FormatTimeToStr function should be as flow:
|
||||||
//"yyyy-mm-dd hh:mm:ss"
|
// "yyyy-mm-dd hh:mm:ss"
|
||||||
//"yyyy-mm-dd hh:mm"
|
// "yyyy-mm-dd hh:mm"
|
||||||
//"yyyy-mm-dd hh"
|
// "yyyy-mm-dd hh"
|
||||||
//"yyyy-mm-dd"
|
// "yyyy-mm-dd"
|
||||||
//"yyyy-mm"
|
// "yyyy-mm"
|
||||||
//"mm-dd"
|
// "mm-dd"
|
||||||
//"dd-mm-yy hh:mm:ss"
|
// "dd-mm-yy hh:mm:ss"
|
||||||
//"yyyy/mm/dd hh:mm:ss"
|
// "yyyy/mm/dd hh:mm:ss"
|
||||||
//"yyyy/mm/dd hh:mm"
|
// "yyyy/mm/dd hh:mm"
|
||||||
//"yyyy/mm/dd hh"
|
// "yyyy/mm/dd hh"
|
||||||
//"yyyy/mm/dd"
|
// "yyyy/mm/dd"
|
||||||
//"yyyy/mm"
|
// "yyyy/mm"
|
||||||
//"mm/dd"
|
// "mm/dd"
|
||||||
//"dd/mm/yy hh:mm:ss"
|
// "dd/mm/yy hh:mm:ss"
|
||||||
//"yyyy"
|
// "yyyy"
|
||||||
//"mm"
|
// "mm"
|
||||||
//"hh:mm:ss"
|
// "hh:mm:ss"
|
||||||
//"mm:ss"
|
// "mm:ss"
|
||||||
package datetime
|
package datetime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -147,16 +147,32 @@ func EndOfDay(t time.Time) time.Time {
|
|||||||
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
|
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeginOfWeek return beginning week, week begin from Sunday
|
// BeginOfWeek return beginning week, default week begin from Sunday
|
||||||
func BeginOfWeek(t time.Time) time.Time {
|
func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time {
|
||||||
y, m, d := t.AddDate(0, 0, 0-int(BeginOfDay(t).Weekday())).Date()
|
var beginFromWeekday = time.Sunday
|
||||||
return time.Date(y, m, d, 0, 0, 0, 0, t.Location())
|
if len(beginFrom) > 0 {
|
||||||
|
beginFromWeekday = beginFrom[0]
|
||||||
|
}
|
||||||
|
y, m, d := t.AddDate(0, 0, int(beginFromWeekday-t.Weekday())).Date()
|
||||||
|
beginOfWeek := time.Date(y, m, d, 0, 0, 0, 0, t.Location())
|
||||||
|
if beginOfWeek.After(t) {
|
||||||
|
return beginOfWeek.AddDate(0, 0, -7)
|
||||||
|
}
|
||||||
|
return beginOfWeek
|
||||||
}
|
}
|
||||||
|
|
||||||
// EndOfWeek return end week time, week end with Saturday
|
// EndOfWeek return end week time, default week end with Saturday
|
||||||
func EndOfWeek(t time.Time) time.Time {
|
func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time {
|
||||||
y, m, d := BeginOfWeek(t).AddDate(0, 0, 7).Add(-time.Nanosecond).Date()
|
var endWithWeekday = time.Saturday
|
||||||
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
|
if len(endWith) > 0 {
|
||||||
|
endWithWeekday = endWith[0]
|
||||||
|
}
|
||||||
|
y, m, d := t.AddDate(0, 0, int(endWithWeekday-t.Weekday())).Date()
|
||||||
|
var endWithWeek = time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
|
||||||
|
if endWithWeek.Before(t) {
|
||||||
|
endWithWeek = endWithWeek.AddDate(0, 0, 7)
|
||||||
|
}
|
||||||
|
return endWithWeek
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeginOfMonth return beginning of month
|
// BeginOfMonth return beginning of month
|
||||||
|
|||||||
@@ -29,6 +29,9 @@ import (
|
|||||||
- [Put](#Put)
|
- [Put](#Put)
|
||||||
- [Delete](#Delete)
|
- [Delete](#Delete)
|
||||||
- [Contains](#Contains)
|
- [Contains](#Contains)
|
||||||
|
- [Iterate](#Iterate)
|
||||||
|
- [Keys](#Keys)
|
||||||
|
- [Values](#Values)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -207,3 +210,105 @@ func main() {
|
|||||||
fmt.Println(hm.Contains("b")) //false
|
fmt.Println(hm.Contains("b")) //false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Iterate">Iterate</span>
|
||||||
|
|
||||||
|
<p>Executes iteratee funcation for every key and value pair of hashmap.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Iterate(iteratee func(key, value 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)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
hm.Iterate(func(key, value any) {
|
||||||
|
fmt.Println(key)
|
||||||
|
fmt.Println(value)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Keys">Keys</span>
|
||||||
|
|
||||||
|
<p>Return a slice of the hashmap's keys (random order).</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Keys() []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)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
keys := hm.Keys()
|
||||||
|
fmt.Println(keys) //[]interface{"a", "b", "c"}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Values">Values</span>
|
||||||
|
|
||||||
|
<p>Return a slice of the hashmap's values (random order).</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Values() []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)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
values := hm.Values()
|
||||||
|
fmt.Println(values) //[]interface{2, 1, 3}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -24,11 +24,13 @@ import (
|
|||||||
|
|
||||||
- [NewHashMap](#NewHashMap)
|
- [NewHashMap](#NewHashMap)
|
||||||
- [NewHashMapWithCapacity](#NewHashMapWithCapacity)
|
- [NewHashMapWithCapacity](#NewHashMapWithCapacity)
|
||||||
|
|
||||||
- [Get](#Get)
|
- [Get](#Get)
|
||||||
- [Put](#Put)
|
- [Put](#Put)
|
||||||
- [Delete](#Delete)
|
- [Delete](#Delete)
|
||||||
- [Contains](#Contains)
|
- [Contains](#Contains)
|
||||||
|
- [Iterate](#Iterate)
|
||||||
|
- [Keys](#Keys)
|
||||||
|
- [Values](#Values)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -203,3 +205,104 @@ func main() {
|
|||||||
fmt.Println(hm.Contains("b")) //false
|
fmt.Println(hm.Contains("b")) //false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Iterate">Iterate</span>
|
||||||
|
|
||||||
|
<p>迭代hashmap,对每个key和value执行iteratee函数</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Iterate(iteratee func(key, value 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)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
hm.Iterate(func(key, value any) {
|
||||||
|
fmt.Println(key)
|
||||||
|
fmt.Println(value)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Keys">Keys</span>
|
||||||
|
|
||||||
|
<p>返回hashmap所有key的切片 (随机顺序)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Keys() []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)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
keys := hm.Keys()
|
||||||
|
fmt.Println(keys) //[]interface{"a", "b", "c"}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Values">Values</span>
|
||||||
|
|
||||||
|
<p>返回hashmap所有值的切片 (随机顺序).</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (hm *HashMap) Values() []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)
|
||||||
|
hm.Put("b", 2)
|
||||||
|
hm.Put("c", 3)
|
||||||
|
|
||||||
|
values := hm.Values()
|
||||||
|
fmt.Println(values) //[]interface{2, 1, 3}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
252
docs/netutil.md
252
docs/netutil.md
@@ -7,6 +7,8 @@ Package netutil contains functions to get net information and send http request.
|
|||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/netutil/net.go](https://github.com/duke-git/lancet/blob/main/netutil/net.go)
|
- [https://github.com/duke-git/lancet/blob/main/netutil/net.go](https://github.com/duke-git/lancet/blob/main/netutil/net.go)
|
||||||
|
|
||||||
|
- [https://github.com/duke-git/lancet/blob/main/netutil/http_client.go](https://github.com/duke-git/lancet/blob/main/netutil/http_client.go)
|
||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/netutil/http.go](https://github.com/duke-git/lancet/blob/main/netutil/http.go)
|
- [https://github.com/duke-git/lancet/blob/main/netutil/http.go](https://github.com/duke-git/lancet/blob/main/netutil/http.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -31,11 +33,17 @@ import (
|
|||||||
- [GetRequestPublicIp](#GetRequestPublicIp)
|
- [GetRequestPublicIp](#GetRequestPublicIp)
|
||||||
- [IsPublicIP](#IsPublicIP)
|
- [IsPublicIP](#IsPublicIP)
|
||||||
- [IsInternalIP](#IsInternalIP)
|
- [IsInternalIP](#IsInternalIP)
|
||||||
- [HttpGet](#HttpGet)
|
- [HttpRequest](#HttpRequest)
|
||||||
- [HttpDelete](#HttpDelete)
|
- [HttpClient](#HttpClient)
|
||||||
- [HttpPost](#HttpPost)
|
- [SendRequest](#SendRequest)
|
||||||
- [HttpPut](#HttpPut)
|
- [DecodeResponse](#DecodeResponse)
|
||||||
- [HttpPatch](#HttpPatch)
|
- [StructToUrlValues](#StructToUrlValues)
|
||||||
|
|
||||||
|
- [HttpGet<sup>Deprecated</sup>](#HttpGet)
|
||||||
|
- [HttpDelete<sup>Deprecated</sup>](#HttpDelete)
|
||||||
|
- [HttpPost<sup>Deprecated</sup>](#HttpPost)
|
||||||
|
- [HttpPut<sup>Deprecated</sup>](#HttpPut)
|
||||||
|
- [HttpPatch<sup>Deprecated</sup>](#HttpPatch)
|
||||||
- [ParseHttpResponse](#ParseHttpResponse)
|
- [ParseHttpResponse](#ParseHttpResponse)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -335,8 +343,232 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="HttpRequest">HttpRequest</span>
|
||||||
|
<p>HttpRequest is a struct used to abstract HTTP request entity.</p>
|
||||||
|
|
||||||
### <span id="HttpGet">HttpGet</span>
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type HttpRequest struct {
|
||||||
|
RawURL string
|
||||||
|
Method string
|
||||||
|
Headers http.Header
|
||||||
|
QueryParams url.Values
|
||||||
|
FormData url.Values
|
||||||
|
Body []byte
|
||||||
|
}
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"github.com/duke-git/lancet/v2/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
header := http.Header{}
|
||||||
|
header.Add("Content-Type", "multipart/form-data")
|
||||||
|
|
||||||
|
postData := url.Values{}
|
||||||
|
postData.Add("userId", "1")
|
||||||
|
postData.Add("title", "testItem")
|
||||||
|
|
||||||
|
request := &netutil.HttpRequest{
|
||||||
|
RawURL: "https://jsonplaceholder.typicode.com/todos",
|
||||||
|
Method: "POST",
|
||||||
|
Headers: header,
|
||||||
|
FormData: postData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="HttpClient">HttpClient</span>
|
||||||
|
<p>HttpClient is a struct used to send HTTP request. It can be instanced with some configurations or none config.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type HttpClient struct {
|
||||||
|
*http.Client
|
||||||
|
TLS *tls.Config
|
||||||
|
Request *http.Request
|
||||||
|
Config HttpClientConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
type HttpClientConfig struct {
|
||||||
|
SSLEnabled bool
|
||||||
|
TLSConfig *tls.Config
|
||||||
|
Compressed bool
|
||||||
|
HandshakeTimeout time.Duration
|
||||||
|
ResponseTimeout time.Duration
|
||||||
|
Verbose bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHttpClient() *HttpClient
|
||||||
|
|
||||||
|
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
|
||||||
|
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/v2/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
httpClientCfg := netutil.HttpClientConfig{
|
||||||
|
SSLEnabled: true,
|
||||||
|
HandshakeTimeout:10 * time.Second
|
||||||
|
}
|
||||||
|
httpClient := netutil.NewHttpClientWithConfig(&httpClientCfg)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="SendRequest">SendRequest</span>
|
||||||
|
<p>Use HttpClient to send HTTP request.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/v2/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
request := &netutil.HttpRequest{
|
||||||
|
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
|
||||||
|
Method: "GET",
|
||||||
|
}
|
||||||
|
|
||||||
|
httpClient := netutil.NewHttpClient()
|
||||||
|
resp, err := httpClient.SendRequest(request)
|
||||||
|
if err != nil || resp.StatusCode != 200 {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Todo struct {
|
||||||
|
UserId int `json:"userId"`
|
||||||
|
Id int `json:"id"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Completed bool `json:"completed"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var todo Todo
|
||||||
|
httpClient.DecodeResponse(resp, &todo)
|
||||||
|
|
||||||
|
fmt.Println(todo.Id) //1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="DecodeResponse">DecodeResponse</span>
|
||||||
|
<p>Decode http response into target object.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/v2/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
request := &netutil.HttpRequest{
|
||||||
|
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
|
||||||
|
Method: "GET",
|
||||||
|
}
|
||||||
|
|
||||||
|
httpClient := netutil.NewHttpClient()
|
||||||
|
resp, err := httpClient.SendRequest(request)
|
||||||
|
if err != nil || resp.StatusCode != 200 {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Todo struct {
|
||||||
|
UserId int `json:"userId"`
|
||||||
|
Id int `json:"id"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Completed bool `json:"completed"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var todo Todo
|
||||||
|
httpClient.DecodeResponse(resp, &todo)
|
||||||
|
|
||||||
|
fmt.Println(todo.Id) //1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="StructToUrlValues">StructToUrlValues</span>
|
||||||
|
<p>Convert struct to url values, only convert the field which is exported and has `json` tag.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func StructToUrlValues(targetStruct any) url.Values
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type TodoQuery struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
UserId int `json:"userId"`
|
||||||
|
}
|
||||||
|
todoQuery := TodoQuery{
|
||||||
|
Id: 1,
|
||||||
|
UserId: 2,
|
||||||
|
}
|
||||||
|
todoValues := netutil.StructToUrlValues(todoQuery)
|
||||||
|
|
||||||
|
fmt.Println(todoValues.Get("id")) //1
|
||||||
|
fmt.Println(todoValues.Get("userId")) //2
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
||||||
<p>Send http get request.</p>
|
<p>Send http get request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -378,7 +610,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPost">HttpPost</span>
|
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
||||||
<p>Send http post request.</p>
|
<p>Send http post request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -427,7 +659,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPut">HttpPut</span>
|
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
||||||
<p>Send http put request.</p>
|
<p>Send http put request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -477,7 +709,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpDelete">HttpDelete</span>
|
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
||||||
<p>Send http delete request.</p>
|
<p>Send http delete request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -516,7 +748,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPatch">HttpPatch</span>
|
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
||||||
<p>Send http patch request.</p>
|
<p>Send http patch request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ netutil网络包支持获取ip地址,发送http请求。
|
|||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/netutil/net.go](https://github.com/duke-git/lancet/blob/main/netutil/net.go)
|
- [https://github.com/duke-git/lancet/blob/main/netutil/net.go](https://github.com/duke-git/lancet/blob/main/netutil/net.go)
|
||||||
|
|
||||||
|
- [https://github.com/duke-git/lancet/blob/main/netutil/http_client.go](https://github.com/duke-git/lancet/blob/main/netutil/http_client.go)
|
||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/netutil/http.go](https://github.com/duke-git/lancet/blob/main/netutil/http.go)
|
- [https://github.com/duke-git/lancet/blob/main/netutil/http.go](https://github.com/duke-git/lancet/blob/main/netutil/http.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -30,11 +33,18 @@ import (
|
|||||||
|
|
||||||
- [IsPublicIP](#IsPublicIP)
|
- [IsPublicIP](#IsPublicIP)
|
||||||
- [IsInternalIP](#IsInternalIP)
|
- [IsInternalIP](#IsInternalIP)
|
||||||
- [HttpGet](#HttpGet)
|
- [HttpRequest](#HttpRequest)
|
||||||
- [HttpDelete](#HttpDelete)
|
- [HttpClient](#HttpClient)
|
||||||
- [HttpPost](#HttpPost)
|
- [SendRequest](#SendRequest)
|
||||||
- [HttpPut](#HttpPut)
|
- [DecodeResponse](#DecodeResponse)
|
||||||
- [HttpPatch](#HttpPatch)
|
- [StructToUrlValues](#StructToUrlValues)
|
||||||
|
|
||||||
|
- [HttpGet<sup>Deprecated</sup>](#HttpGet)
|
||||||
|
- [HttpDelete<sup>Deprecated</sup>](#HttpDelete)
|
||||||
|
- [HttpPost<sup>Deprecated</sup>](#HttpPost)
|
||||||
|
- [HttpPut<sup>Deprecated</sup>](#HttpPut)
|
||||||
|
- [HttpPatch<sup>Deprecated</sup>](#HttpPatch)
|
||||||
|
|
||||||
- [ParseHttpResponse](#ParseHttpResponse)
|
- [ParseHttpResponse](#ParseHttpResponse)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -334,8 +344,232 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="HttpRequest">HttpRequest</span>
|
||||||
|
<p>HttpRequest用于抽象HTTP请求实体的结构</p>
|
||||||
|
|
||||||
### <span id="HttpGet">HttpGet</span>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type HttpRequest struct {
|
||||||
|
RawURL string
|
||||||
|
Method string
|
||||||
|
Headers http.Header
|
||||||
|
QueryParams url.Values
|
||||||
|
FormData url.Values
|
||||||
|
Body []byte
|
||||||
|
}
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"github.com/duke-git/lancet/v2/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
header := http.Header{}
|
||||||
|
header.Add("Content-Type", "multipart/form-data")
|
||||||
|
|
||||||
|
postData := url.Values{}
|
||||||
|
postData.Add("userId", "1")
|
||||||
|
postData.Add("title", "testItem")
|
||||||
|
|
||||||
|
request := &netutil.HttpRequest{
|
||||||
|
RawURL: "https://jsonplaceholder.typicode.com/todos",
|
||||||
|
Method: "POST",
|
||||||
|
Headers: header,
|
||||||
|
FormData: postData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="HttpClient">HttpClient</span>
|
||||||
|
<p>HttpClient是用于发送HTTP请求的结构体。它可以用一些配置参数或无配置实例化.</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type HttpClient struct {
|
||||||
|
*http.Client
|
||||||
|
TLS *tls.Config
|
||||||
|
Request *http.Request
|
||||||
|
Config HttpClientConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
type HttpClientConfig struct {
|
||||||
|
SSLEnabled bool
|
||||||
|
TLSConfig *tls.Config
|
||||||
|
Compressed bool
|
||||||
|
HandshakeTimeout time.Duration
|
||||||
|
ResponseTimeout time.Duration
|
||||||
|
Verbose bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHttpClient() *HttpClient
|
||||||
|
|
||||||
|
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
|
||||||
|
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/v2/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
httpClientCfg := netutil.HttpClientConfig{
|
||||||
|
SSLEnabled: true,
|
||||||
|
HandshakeTimeout:10 * time.Second
|
||||||
|
}
|
||||||
|
httpClient := netutil.NewHttpClientWithConfig(&httpClientCfg)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="SendRequest">SendRequest</span>
|
||||||
|
<p>HttpClient发送http请求</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/v2/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
request := &netutil.HttpRequest{
|
||||||
|
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
|
||||||
|
Method: "GET",
|
||||||
|
}
|
||||||
|
|
||||||
|
httpClient := netutil.NewHttpClient()
|
||||||
|
resp, err := httpClient.SendRequest(request)
|
||||||
|
if err != nil || resp.StatusCode != 200 {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Todo struct {
|
||||||
|
UserId int `json:"userId"`
|
||||||
|
Id int `json:"id"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Completed bool `json:"completed"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var todo Todo
|
||||||
|
httpClient.DecodeResponse(resp, &todo)
|
||||||
|
|
||||||
|
fmt.Println(todo.Id) //1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="DecodeResponse">DecodeResponse</span>
|
||||||
|
<p>解析http响应体到目标结构体</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/v2/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
request := &netutil.HttpRequest{
|
||||||
|
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
|
||||||
|
Method: "GET",
|
||||||
|
}
|
||||||
|
|
||||||
|
httpClient := netutil.NewHttpClient()
|
||||||
|
resp, err := httpClient.SendRequest(request)
|
||||||
|
if err != nil || resp.StatusCode != 200 {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Todo struct {
|
||||||
|
UserId int `json:"userId"`
|
||||||
|
Id int `json:"id"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Completed bool `json:"completed"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var todo Todo
|
||||||
|
httpClient.DecodeResponse(resp, &todo)
|
||||||
|
|
||||||
|
fmt.Println(todo.Id) //1
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="StructToUrlValues">StructToUrlValues</span>
|
||||||
|
<p>将结构体转为url values, 仅转化结构体导出字段并且包含`json` tag.</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func StructToUrlValues(targetStruct any) url.Values
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type TodoQuery struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
UserId int `json:"userId"`
|
||||||
|
}
|
||||||
|
todoQuery := TodoQuery{
|
||||||
|
Id: 1,
|
||||||
|
UserId: 2,
|
||||||
|
}
|
||||||
|
todoValues := netutil.StructToUrlValues(todoQuery)
|
||||||
|
|
||||||
|
fmt.Println(todoValues.Get("id")) //1
|
||||||
|
fmt.Println(todoValues.Get("userId")) //2
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
||||||
<p>发送http get请求</p>
|
<p>发送http get请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -377,7 +611,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPost">HttpPost</span>
|
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
||||||
<p>发送http post请求</p>
|
<p>发送http post请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -426,7 +660,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPut">HttpPut</span>
|
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
||||||
<p>发送http put请求</p>
|
<p>发送http put请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -438,7 +672,7 @@ func main() {
|
|||||||
// params[3] http client,类型必须是http.Client
|
// params[3] http client,类型必须是http.Client
|
||||||
func HttpPut(url string, params ...any) (*http.Response, error)
|
func HttpPut(url string, params ...any) (*http.Response, error)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -476,7 +710,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpDelete">HttpDelete</span>
|
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
||||||
<p>发送http delete请求</p>
|
<p>发送http delete请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -515,7 +749,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPatch">HttpPatch</span>
|
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
||||||
<p>发送http patch请求</p>
|
<p>发送http patch请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|||||||
Reference in New Issue
Block a user