mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
doc: normalize document
This commit is contained in:
191
docs/netutil.md
191
docs/netutil.md
@@ -1,4 +1,5 @@
|
|||||||
# Netutil
|
# Netutil
|
||||||
|
|
||||||
Package netutil contains functions to get net information and send http request.
|
Package netutil contains functions to get net information and send http request.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -14,6 +15,7 @@ Package netutil contains functions to get net information and send http request.
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/v2/netutil"
|
"github.com/duke-git/lancet/v2/netutil"
|
||||||
@@ -23,6 +25,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
||||||
- [EncodeUrl](#EncodeUrl)
|
- [EncodeUrl](#EncodeUrl)
|
||||||
- [GetInternalIp](#GetInternalIp)
|
- [GetInternalIp](#GetInternalIp)
|
||||||
@@ -48,8 +51,8 @@ import (
|
|||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span>
|
### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span>
|
||||||
|
|
||||||
<p>Convert map to url query string.</p>
|
<p>Convert map to url query string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -57,6 +60,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ConvertMapToQueryString(param map[string]any) string
|
func ConvertMapToQueryString(param map[string]any) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -75,13 +79,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
qs := netutil.ConvertMapToQueryString(m)
|
qs := netutil.ConvertMapToQueryString(m)
|
||||||
|
|
||||||
fmt.Println(qs) //a=1&b=2&c=3
|
// Output:
|
||||||
|
// a=1&b=2&c=3
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EncodeUrl">EncodeUrl</span>
|
### <span id="EncodeUrl">EncodeUrl</span>
|
||||||
|
|
||||||
<p>Encode url query string values.</p>
|
<p>Encode url query string values.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -89,6 +93,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EncodeUrl(urlStr string) (string, error)
|
func EncodeUrl(urlStr string) (string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -102,16 +107,20 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
urlAddr := "http://www.lancet.com?a=1&b=[2]"
|
urlAddr := "http://www.lancet.com?a=1&b=[2]"
|
||||||
encodedUrl, err := netutil.EncodeUrl(urlAddr)
|
encodedUrl, err := netutil.EncodeUrl(urlAddr)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
fmt.Println(encodedUrl) //http://www.lancet.com?a=1&b=%5B2%5D
|
|
||||||
|
fmt.Println(encodedUrl)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// http://www.lancet.com?a=1&b=%5B2%5D
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetInternalIp">GetInternalIp</span>
|
### <span id="GetInternalIp">GetInternalIp</span>
|
||||||
|
|
||||||
<p>Get internal ip information.</p>
|
<p>Get internal ip information.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -119,6 +128,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetInternalIp() string
|
func GetInternalIp() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -134,13 +144,15 @@ func main() {
|
|||||||
internalIp := netutil.GetInternalIp()
|
internalIp := netutil.GetInternalIp()
|
||||||
ip := net.ParseIP(internalIp)
|
ip := net.ParseIP(internalIp)
|
||||||
|
|
||||||
fmt.Println(ip) //192.168.1.9
|
fmt.Println(ip)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 192.168.1.9
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetIps">GetIps</span>
|
### <span id="GetIps">GetIps</span>
|
||||||
|
|
||||||
<p>Get all ipv4 list.</p>
|
<p>Get all ipv4 list.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -148,6 +160,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetIps() []string
|
func GetIps() []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -161,13 +174,15 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ips := netutil.GetIps()
|
ips := netutil.GetIps()
|
||||||
fmt.Println(ips) //[192.168.1.9]
|
fmt.Println(ips)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [192.168.1.9]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetMacAddrs">GetMacAddrs</span>
|
### <span id="GetMacAddrs">GetMacAddrs</span>
|
||||||
|
|
||||||
<p>Get all mac addresses list.</p>
|
<p>Get all mac addresses list.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -175,6 +190,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetMacAddrs() []string {
|
func GetMacAddrs() []string {
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -187,14 +203,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
addrs := netutil.GetMacAddrs()
|
macAddrs := netutil.GetMacAddrs()
|
||||||
fmt.Println(addrs)
|
fmt.Println(macAddrs)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [18:31:bf:09:d1:56 76:ee:2a:e6:2e:0f 74:ee:2a:e6:2e:0f 74:ee:2a:e6:2e:0f]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetPublicIpInfo">GetPublicIpInfo</span>
|
### <span id="GetPublicIpInfo">GetPublicIpInfo</span>
|
||||||
|
|
||||||
<p>Get public ip information.</p>
|
<p>Get public ip information.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -216,6 +234,7 @@ type PublicIpInfo struct {
|
|||||||
Ip string `json:"query"`
|
Ip string `json:"query"`
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -236,9 +255,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetRequestPublicIp">GetRequestPublicIp</span>
|
### <span id="GetRequestPublicIp">GetRequestPublicIp</span>
|
||||||
|
|
||||||
<p>Get http request public ip.</p>
|
<p>Get http request public ip.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -246,6 +264,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetRequestPublicIp(req *http.Request) string
|
func GetRequestPublicIp(req *http.Request) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -259,29 +278,23 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
ip := "36.112.24.10"
|
ip := "36.112.24.10"
|
||||||
|
|
||||||
request1 := http.Request{
|
request := http.Request{
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Header: http.Header{
|
Header: http.Header{
|
||||||
"X-Forwarded-For": {ip},
|
"X-Forwarded-For": {ip},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
publicIp1 := netutil.GetRequestPublicIp(&request1)
|
publicIp := netutil.GetRequestPublicIp(&request)
|
||||||
fmt.Println(publicIp1) //36.112.24.10
|
|
||||||
|
|
||||||
request2 := http.Request{
|
fmt.Println(publicIp)
|
||||||
Method: "GET",
|
|
||||||
Header: http.Header{
|
// Output:
|
||||||
"X-Real-Ip": {ip},
|
// 36.112.24.10
|
||||||
},
|
|
||||||
}
|
|
||||||
publicIp2 := netutil.GetRequestPublicIp(&request2)
|
|
||||||
fmt.Println(publicIp2) //36.112.24.10
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsPublicIP">IsPublicIP</span>
|
### <span id="IsPublicIP">IsPublicIP</span>
|
||||||
|
|
||||||
<p>Checks if an ip is public or not.</p>
|
<p>Checks if an ip is public or not.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -289,6 +302,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsPublicIP(IP net.IP) bool
|
func IsPublicIP(IP net.IP) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -301,18 +315,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ip1 := net.ParseIP("192.168.0.1")
|
ip1 := netutil.IsPublicIP(net.ParseIP("127.0.0.1"))
|
||||||
ip2 := net.ParseIP("36.112.24.10")
|
ip2 := netutil.IsPublicIP(net.ParseIP("192.168.0.1"))
|
||||||
|
ip3 := netutil.IsPublicIP(net.ParseIP("36.112.24.10"))
|
||||||
|
|
||||||
fmt.Println(netutil.IsPublicIP(ip1)) //false
|
fmt.Println(ip1)
|
||||||
fmt.Println(netutil.IsPublicIP(ip2)) //true
|
fmt.Println(ip2)
|
||||||
|
fmt.Println(ip3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsInternalIP">IsInternalIP</span>
|
### <span id="IsInternalIP">IsInternalIP</span>
|
||||||
|
|
||||||
<p>Checks if an ip is intranet or not.</p>
|
<p>Checks if an ip is intranet or not.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -320,6 +339,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsInternalIP(IP net.IP) bool
|
func IsInternalIP(IP net.IP) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -332,16 +352,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ip1 := net.ParseIP("127.0.0.1")
|
ip1 := netutil.IsInternalIP(net.ParseIP("127.0.0.1"))
|
||||||
ip2 := net.ParseIP("36.112.24.10")
|
ip2 := netutil.IsInternalIP(net.ParseIP("192.168.0.1"))
|
||||||
|
ip3 := netutil.IsInternalIP(net.ParseIP("36.112.24.10"))
|
||||||
|
|
||||||
fmt.Println(netutil.IsInternalIP(ip1)) //true
|
fmt.Println(ip1)
|
||||||
fmt.Println(netutil.IsInternalIP(ip2)) //false
|
fmt.Println(ip2)
|
||||||
|
fmt.Println(ip3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpRequest">HttpRequest</span>
|
### <span id="HttpRequest">HttpRequest</span>
|
||||||
|
|
||||||
<p>HttpRequest is a struct used to abstract HTTP request entity.</p>
|
<p>HttpRequest is a struct used to abstract HTTP request entity.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -356,6 +383,7 @@ type HttpRequest struct {
|
|||||||
Body []byte
|
Body []byte
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -384,8 +412,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpClient">HttpClient</span>
|
### <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>
|
<p>HttpClient is a struct used to send HTTP request. It can be instanced with some configurations or none config.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -412,6 +440,7 @@ func NewHttpClient() *HttpClient
|
|||||||
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
|
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -433,9 +462,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SendRequest">SendRequest</span>
|
### <span id="SendRequest">SendRequest</span>
|
||||||
|
|
||||||
<p>Use HttpClient to send HTTP request.</p>
|
<p>Use HttpClient to send HTTP request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -443,6 +471,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
|
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -464,7 +493,7 @@ func main() {
|
|||||||
httpClient := netutil.NewHttpClient()
|
httpClient := netutil.NewHttpClient()
|
||||||
resp, err := httpClient.SendRequest(request)
|
resp, err := httpClient.SendRequest(request)
|
||||||
if err != nil || resp.StatusCode != 200 {
|
if err != nil || resp.StatusCode != 200 {
|
||||||
log.Fatal(err)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type Todo struct {
|
type Todo struct {
|
||||||
@@ -475,15 +504,20 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var todo Todo
|
var todo Todo
|
||||||
httpClient.DecodeResponse(resp, &todo)
|
err = httpClient.DecodeResponse(resp, &todo)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println(todo.Id) //1
|
fmt.Println(todo.Id)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DecodeResponse">DecodeResponse</span>
|
### <span id="DecodeResponse">DecodeResponse</span>
|
||||||
|
|
||||||
<p>Decode http response into target object.</p>
|
<p>Decode http response into target object.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -491,6 +525,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error
|
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -512,7 +547,7 @@ func main() {
|
|||||||
httpClient := netutil.NewHttpClient()
|
httpClient := netutil.NewHttpClient()
|
||||||
resp, err := httpClient.SendRequest(request)
|
resp, err := httpClient.SendRequest(request)
|
||||||
if err != nil || resp.StatusCode != 200 {
|
if err != nil || resp.StatusCode != 200 {
|
||||||
log.Fatal(err)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type Todo struct {
|
type Todo struct {
|
||||||
@@ -523,14 +558,20 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var todo Todo
|
var todo Todo
|
||||||
httpClient.DecodeResponse(resp, &todo)
|
err = httpClient.DecodeResponse(resp, &todo)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println(todo.Id) //1
|
fmt.Println(todo.Id)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="StructToUrlValues">StructToUrlValues</span>
|
### <span id="StructToUrlValues">StructToUrlValues</span>
|
||||||
|
|
||||||
<p>Convert struct to url values, only convert the field which is exported and has `json` tag.</p>
|
<p>Convert struct to url values, only convert the field which is exported and has `json` tag.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -538,6 +579,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func StructToUrlValues(targetStruct any) url.Values
|
func StructToUrlValues(targetStruct any) url.Values
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -551,22 +593,25 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
type TodoQuery struct {
|
type TodoQuery struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
UserId int `json:"userId"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
todoQuery := TodoQuery{
|
todoQuery := TodoQuery{
|
||||||
Id: 1,
|
Id: 1,
|
||||||
UserId: 2,
|
Name: "Test",
|
||||||
}
|
}
|
||||||
todoValues := netutil.StructToUrlValues(todoQuery)
|
todoValues := netutil.StructToUrlValues(todoQuery)
|
||||||
|
|
||||||
fmt.Println(todoValues.Get("id")) //1
|
fmt.Println(todoValues.Get("id"))
|
||||||
fmt.Println(todoValues.Get("userId")) //2
|
fmt.Println(todoValues.Get("name"))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// Test
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
### <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>
|
||||||
@@ -578,6 +623,7 @@ func main() {
|
|||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
func HttpGet(url string, params ...any) (*http.Response, error)
|
func HttpGet(url string, params ...any) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -606,9 +652,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</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>
|
||||||
@@ -620,6 +665,7 @@ func main() {
|
|||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
func HttpPost(url string, params ...any) (*http.Response, error)
|
func HttpPost(url string, params ...any) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -655,9 +701,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</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>
|
||||||
@@ -669,6 +714,7 @@ func main() {
|
|||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
func HttpPut(url string, params ...any) (*http.Response, error)
|
func HttpPut(url string, params ...any) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -705,9 +751,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</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>
|
||||||
@@ -719,6 +764,7 @@ func main() {
|
|||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
func HttpDelete(url string, params ...any) (*http.Response, error)
|
func HttpDelete(url string, params ...any) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -744,9 +790,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</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>
|
||||||
@@ -758,6 +803,7 @@ func main() {
|
|||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
func HttpPatch(url string, params ...any) (*http.Response, error)
|
func HttpPatch(url string, params ...any) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -794,9 +840,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ParseHttpResponse">ParseHttpResponse</span>
|
### <span id="ParseHttpResponse">ParseHttpResponse</span>
|
||||||
|
|
||||||
<p>Decode http response to specified interface.</p>
|
<p>Decode http response to specified interface.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -804,6 +849,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ParseHttpResponse(resp *http.Response, obj any) error
|
func ParseHttpResponse(resp *http.Response, obj any) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -844,4 +890,3 @@ func main() {
|
|||||||
fmt.Println(toDoResp)
|
fmt.Println(toDoResp)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
# Netutil
|
# Netutil
|
||||||
netutil网络包支持获取ip地址,发送http请求。
|
|
||||||
|
netutil 网络包支持获取 ip 地址,发送 http 请求。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
- [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_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)
|
||||||
@@ -14,6 +14,7 @@ netutil网络包支持获取ip地址,发送http请求。
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/v2/netutil"
|
"github.com/duke-git/lancet/v2/netutil"
|
||||||
@@ -23,6 +24,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
||||||
- [EncodeUrl](#EncodeUrl)
|
- [EncodeUrl](#EncodeUrl)
|
||||||
- [GetInternalIp](#GetInternalIp)
|
- [GetInternalIp](#GetInternalIp)
|
||||||
@@ -48,8 +50,8 @@ import (
|
|||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span>
|
### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span>
|
||||||
|
|
||||||
<p>将map转换成http查询字符串.</p>
|
<p>将map转换成http查询字符串.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -57,6 +59,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ConvertMapToQueryString(param map[string]any) string
|
func ConvertMapToQueryString(param map[string]any) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -75,13 +78,15 @@ func main() {
|
|||||||
}
|
}
|
||||||
qs := netutil.ConvertMapToQueryString(m)
|
qs := netutil.ConvertMapToQueryString(m)
|
||||||
|
|
||||||
fmt.Println(qs) //a=1&b=2&c=3
|
fmt.Println(qs)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// a=1&b=2&c=3
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EncodeUrl">EncodeUrl</span>
|
### <span id="EncodeUrl">EncodeUrl</span>
|
||||||
|
|
||||||
<p>编码url query string的值</p>
|
<p>编码url query string的值</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -89,6 +94,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EncodeUrl(urlStr string) (string, error)
|
func EncodeUrl(urlStr string) (string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -102,17 +108,20 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
urlAddr := "http://www.lancet.com?a=1&b=[2]"
|
urlAddr := "http://www.lancet.com?a=1&b=[2]"
|
||||||
encodedUrl, err := netutil.EncodeUrl(urlAddr)
|
encodedUrl, err := netutil.EncodeUrl(urlAddr)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
fmt.Println(encodedUrl) //http://www.lancet.com?a=1&b=%5B2%5D
|
|
||||||
|
fmt.Println(encodedUrl)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// http://www.lancet.com?a=1&b=%5B2%5D
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetInternalIp">GetInternalIp</span>
|
### <span id="GetInternalIp">GetInternalIp</span>
|
||||||
|
|
||||||
<p>获取内部ip</p>
|
<p>获取内部ip</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -120,6 +129,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetInternalIp() string
|
func GetInternalIp() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -135,12 +145,15 @@ func main() {
|
|||||||
internalIp := netutil.GetInternalIp()
|
internalIp := netutil.GetInternalIp()
|
||||||
ip := net.ParseIP(internalIp)
|
ip := net.ParseIP(internalIp)
|
||||||
|
|
||||||
fmt.Println(ip) //192.168.1.9
|
fmt.Println(ip)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 192.168.1.9
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetIps">GetIps</span>
|
### <span id="GetIps">GetIps</span>
|
||||||
|
|
||||||
<p>获取ipv4地址列表</p>
|
<p>获取ipv4地址列表</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -148,6 +161,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetIps() []string
|
func GetIps() []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -161,13 +175,15 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ips := netutil.GetIps()
|
ips := netutil.GetIps()
|
||||||
fmt.Println(ips) //[192.168.1.9]
|
fmt.Println(ips)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [192.168.1.9]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetMacAddrs">GetMacAddrs</span>
|
### <span id="GetMacAddrs">GetMacAddrs</span>
|
||||||
|
|
||||||
<p>获取mac地址列</p>
|
<p>获取mac地址列</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -175,6 +191,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetMacAddrs() []string {
|
func GetMacAddrs() []string {
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -187,14 +204,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
addrs := netutil.GetMacAddrs()
|
macAddrs := netutil.GetMacAddrs()
|
||||||
fmt.Println(addrs)
|
fmt.Println(macAddrs)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [18:31:bf:09:d1:56 76:ee:2a:e6:2e:0f 74:ee:2a:e6:2e:0f 74:ee:2a:e6:2e:0f]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetPublicIpInfo">GetPublicIpInfo</span>
|
### <span id="GetPublicIpInfo">GetPublicIpInfo</span>
|
||||||
|
|
||||||
<p>获取公网ip信息</p>
|
<p>获取公网ip信息</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -216,6 +235,7 @@ type PublicIpInfo struct {
|
|||||||
Ip string `json:"query"`
|
Ip string `json:"query"`
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -236,9 +256,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetRequestPublicIp">GetRequestPublicIp</span>
|
### <span id="GetRequestPublicIp">GetRequestPublicIp</span>
|
||||||
|
|
||||||
<p>获取http请求ip</p>
|
<p>获取http请求ip</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -246,6 +265,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetRequestPublicIp(req *http.Request) string
|
func GetRequestPublicIp(req *http.Request) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -259,30 +279,23 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
ip := "36.112.24.10"
|
ip := "36.112.24.10"
|
||||||
|
|
||||||
request1 := http.Request{
|
request := http.Request{
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Header: http.Header{
|
Header: http.Header{
|
||||||
"X-Forwarded-For": {ip},
|
"X-Forwarded-For": {ip},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
publicIp1 := netutil.GetRequestPublicIp(&request1)
|
publicIp := netutil.GetRequestPublicIp(&request)
|
||||||
fmt.Println(publicIp1) //36.112.24.10
|
|
||||||
|
|
||||||
request2 := http.Request{
|
fmt.Println(publicIp)
|
||||||
Method: "GET",
|
|
||||||
Header: http.Header{
|
// Output:
|
||||||
"X-Real-Ip": {ip},
|
// 36.112.24.10
|
||||||
},
|
|
||||||
}
|
|
||||||
publicIp2 := netutil.GetRequestPublicIp(&request2)
|
|
||||||
fmt.Println(publicIp2) //36.112.24.10
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsPublicIP">IsPublicIP</span>
|
### <span id="IsPublicIP">IsPublicIP</span>
|
||||||
|
|
||||||
<p>判断ip是否是公共ip</p>
|
<p>判断ip是否是公共ip</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -290,6 +303,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsPublicIP(IP net.IP) bool
|
func IsPublicIP(IP net.IP) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -302,17 +316,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ip1 := net.ParseIP("192.168.0.1")
|
ip1 := netutil.IsPublicIP(net.ParseIP("127.0.0.1"))
|
||||||
ip2 := net.ParseIP("36.112.24.10")
|
ip2 := netutil.IsPublicIP(net.ParseIP("192.168.0.1"))
|
||||||
|
ip3 := netutil.IsPublicIP(net.ParseIP("36.112.24.10"))
|
||||||
|
|
||||||
fmt.Println(netutil.IsPublicIP(ip1)) //false
|
fmt.Println(ip1)
|
||||||
fmt.Println(netutil.IsPublicIP(ip2)) //true
|
fmt.Println(ip2)
|
||||||
|
fmt.Println(ip3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsInternalIP">IsInternalIP</span>
|
### <span id="IsInternalIP">IsInternalIP</span>
|
||||||
|
|
||||||
<p>判断ip是否是局域网ip.</p>
|
<p>判断ip是否是局域网ip.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -320,6 +340,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsInternalIP(IP net.IP) bool
|
func IsInternalIP(IP net.IP) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -332,16 +353,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ip1 := net.ParseIP("127.0.0.1")
|
ip1 := netutil.IsInternalIP(net.ParseIP("127.0.0.1"))
|
||||||
ip2 := net.ParseIP("36.112.24.10")
|
ip2 := netutil.IsInternalIP(net.ParseIP("192.168.0.1"))
|
||||||
|
ip3 := netutil.IsInternalIP(net.ParseIP("36.112.24.10"))
|
||||||
|
|
||||||
fmt.Println(netutil.IsInternalIP(ip1)) //true
|
fmt.Println(ip1)
|
||||||
fmt.Println(netutil.IsInternalIP(ip2)) //false
|
fmt.Println(ip2)
|
||||||
|
fmt.Println(ip3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpRequest">HttpRequest</span>
|
### <span id="HttpRequest">HttpRequest</span>
|
||||||
|
|
||||||
<p>HttpRequest用于抽象HTTP请求实体的结构</p>
|
<p>HttpRequest用于抽象HTTP请求实体的结构</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -356,6 +384,7 @@ type HttpRequest struct {
|
|||||||
Body []byte
|
Body []byte
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -384,8 +413,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpClient">HttpClient</span>
|
### <span id="HttpClient">HttpClient</span>
|
||||||
|
|
||||||
<p>HttpClient是用于发送HTTP请求的结构体。它可以用一些配置参数或无配置实例化.</p>
|
<p>HttpClient是用于发送HTTP请求的结构体。它可以用一些配置参数或无配置实例化.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -412,6 +441,7 @@ func NewHttpClient() *HttpClient
|
|||||||
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
|
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -433,9 +463,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SendRequest">SendRequest</span>
|
### <span id="SendRequest">SendRequest</span>
|
||||||
|
|
||||||
<p>HttpClient发送http请求</p>
|
<p>HttpClient发送http请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -443,6 +472,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
|
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -464,7 +494,7 @@ func main() {
|
|||||||
httpClient := netutil.NewHttpClient()
|
httpClient := netutil.NewHttpClient()
|
||||||
resp, err := httpClient.SendRequest(request)
|
resp, err := httpClient.SendRequest(request)
|
||||||
if err != nil || resp.StatusCode != 200 {
|
if err != nil || resp.StatusCode != 200 {
|
||||||
log.Fatal(err)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type Todo struct {
|
type Todo struct {
|
||||||
@@ -475,15 +505,20 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var todo Todo
|
var todo Todo
|
||||||
httpClient.DecodeResponse(resp, &todo)
|
err = httpClient.DecodeResponse(resp, &todo)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println(todo.Id) //1
|
fmt.Println(todo.Id)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DecodeResponse">DecodeResponse</span>
|
### <span id="DecodeResponse">DecodeResponse</span>
|
||||||
|
|
||||||
<p>解析http响应体到目标结构体</p>
|
<p>解析http响应体到目标结构体</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -491,6 +526,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error
|
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -512,7 +548,7 @@ func main() {
|
|||||||
httpClient := netutil.NewHttpClient()
|
httpClient := netutil.NewHttpClient()
|
||||||
resp, err := httpClient.SendRequest(request)
|
resp, err := httpClient.SendRequest(request)
|
||||||
if err != nil || resp.StatusCode != 200 {
|
if err != nil || resp.StatusCode != 200 {
|
||||||
log.Fatal(err)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type Todo struct {
|
type Todo struct {
|
||||||
@@ -523,14 +559,20 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var todo Todo
|
var todo Todo
|
||||||
httpClient.DecodeResponse(resp, &todo)
|
err = httpClient.DecodeResponse(resp, &todo)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println(todo.Id) //1
|
fmt.Println(todo.Id)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="StructToUrlValues">StructToUrlValues</span>
|
### <span id="StructToUrlValues">StructToUrlValues</span>
|
||||||
|
|
||||||
<p>将结构体转为url values, 仅转化结构体导出字段并且包含`json` tag</p>
|
<p>将结构体转为url values, 仅转化结构体导出字段并且包含`json` tag</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -538,6 +580,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func StructToUrlValues(targetStruct any) url.Values
|
func StructToUrlValues(targetStruct any) url.Values
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -551,22 +594,25 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
type TodoQuery struct {
|
type TodoQuery struct {
|
||||||
Id int `json:"id"`
|
Id int `json:"id"`
|
||||||
UserId int `json:"userId"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
todoQuery := TodoQuery{
|
todoQuery := TodoQuery{
|
||||||
Id: 1,
|
Id: 1,
|
||||||
UserId: 2,
|
Name: "Test",
|
||||||
}
|
}
|
||||||
todoValues := netutil.StructToUrlValues(todoQuery)
|
todoValues := netutil.StructToUrlValues(todoQuery)
|
||||||
|
|
||||||
fmt.Println(todoValues.Get("id")) //1
|
fmt.Println(todoValues.Get("id"))
|
||||||
fmt.Println(todoValues.Get("userId")) //2
|
fmt.Println(todoValues.Get("name"))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1
|
||||||
|
// Test
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>发送http get请求</p>
|
<p>发送http get请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -578,6 +624,7 @@ func main() {
|
|||||||
// params[3] http client,类型必须是http.Client
|
// params[3] http client,类型必须是http.Client
|
||||||
func HttpGet(url string, params ...any) (*http.Response, error)
|
func HttpGet(url string, params ...any) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -606,9 +653,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>发送http post请求</p>
|
<p>发送http post请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -620,6 +666,7 @@ func main() {
|
|||||||
// params[3] http client,类型必须是http.Client
|
// params[3] http client,类型必须是http.Client
|
||||||
func HttpPost(url string, params ...any) (*http.Response, error)
|
func HttpPost(url string, params ...any) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -655,9 +702,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>发送http put请求</p>
|
<p>发送http put请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -669,6 +715,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>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -705,9 +752,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>发送http delete请求</p>
|
<p>发送http delete请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -719,6 +765,7 @@ func main() {
|
|||||||
// params[3] http client,类型必须是http.Client
|
// params[3] http client,类型必须是http.Client
|
||||||
func HttpDelete(url string, params ...any) (*http.Response, error)
|
func HttpDelete(url string, params ...any) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -744,9 +791,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>发送http patch请求</p>
|
<p>发送http patch请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -758,6 +804,7 @@ func main() {
|
|||||||
// params[3] http client,类型必须是http.Client
|
// params[3] http client,类型必须是http.Client
|
||||||
func HttpPatch(url string, params ...any) (*http.Response, error)
|
func HttpPatch(url string, params ...any) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -794,9 +841,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ParseHttpResponse">ParseHttpResponse</span>
|
### <span id="ParseHttpResponse">ParseHttpResponse</span>
|
||||||
|
|
||||||
<p>将http请求响应解码成特定struct值</p>
|
<p>将http请求响应解码成特定struct值</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -804,6 +850,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ParseHttpResponse(resp *http.Response, obj any) error
|
func ParseHttpResponse(resp *http.Response, obj any) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -844,4 +891,3 @@ func main() {
|
|||||||
fmt.Println(toDoResp)
|
fmt.Println(toDoResp)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Retry
|
# Retry
|
||||||
|
|
||||||
Package retry is for executing a function repeatedly until it was successful or canceled by the context.
|
Package retry is for executing a function repeatedly until it was successful or canceled by the context.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ Package retry is for executing a function repeatedly until it was successful or
|
|||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/retry/retry.go](https://github.com/duke-git/lancet/blob/main/retry/retry.go)
|
- [https://github.com/duke-git/lancet/blob/main/retry/retry.go](https://github.com/duke-git/lancet/blob/main/retry/retry.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/v2/retry"
|
"github.com/duke-git/lancet/v2/retry"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [Context](#Context)
|
- [Context](#Context)
|
||||||
- [Retry](#Retry)
|
- [Retry](#Retry)
|
||||||
- [RetryFunc](#RetryFunc)
|
- [RetryFunc](#RetryFunc)
|
||||||
@@ -30,8 +32,8 @@ import (
|
|||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="Context">Context</span>
|
### <span id="Context">Context</span>
|
||||||
|
|
||||||
<p>Set retry context config, can cancel the retry with context.</p>
|
<p>Set retry context config, can cancel the retry with context.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -39,6 +41,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func Context(ctx context.Context)
|
func Context(ctx context.Context)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -52,7 +55,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, cancel := context.WithCancel(context.TODO())
|
ctx, cancel := context.WithCancel(context.TODO())
|
||||||
var number int
|
|
||||||
|
number := 0
|
||||||
increaseNumber := func() error {
|
increaseNumber := func() error {
|
||||||
number++
|
number++
|
||||||
if number > 3 {
|
if number > 3 {
|
||||||
@@ -61,21 +65,22 @@ func main() {
|
|||||||
return errors.New("error occurs")
|
return errors.New("error occurs")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := retry.Retry(increaseNumber,
|
duration := retry.RetryDuration(time.Microsecond*50)
|
||||||
retry.RetryDuration(time.Microsecond*50),
|
|
||||||
|
retry.Retry(increaseNumber,
|
||||||
|
duration,
|
||||||
retry.Context(ctx),
|
retry.Context(ctx),
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
fmt.Println(number)
|
||||||
fmt.Println(err) //retry is cancelled
|
|
||||||
}
|
// Output:
|
||||||
|
// 4
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryFunc">RetryFunc</span>
|
### <span id="RetryFunc">RetryFunc</span>
|
||||||
|
|
||||||
<p>Function that retry executes.</p>
|
<p>Function that retry executes.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -83,6 +88,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
type RetryFunc func() error
|
type RetryFunc func() error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -96,9 +102,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var number int
|
number := 0
|
||||||
|
var increaseNumber retry.RetryFunc = func() error {
|
||||||
increaseNumber := func() error {
|
|
||||||
number++
|
number++
|
||||||
if number == 3 {
|
if number == 3 {
|
||||||
return nil
|
return nil
|
||||||
@@ -106,18 +111,22 @@ func main() {
|
|||||||
return errors.New("error occurs")
|
return errors.New("error occurs")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50))
|
duration := retry.RetryDuration(time.Microsecond*50)
|
||||||
|
|
||||||
|
err := retry.Retry(increaseNumber, duration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(number) //3
|
fmt.Println(number)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryTimes">RetryTimes</span>
|
### <span id="RetryTimes">RetryTimes</span>
|
||||||
|
|
||||||
<p>Set times of retry. Default times is 5.</p>
|
<p>Set times of retry. Default times is 5.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -125,6 +134,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RetryTimes(n uint)
|
func RetryTimes(n uint)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -138,7 +148,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var number int
|
number := 0
|
||||||
|
|
||||||
increaseNumber := func() error {
|
increaseNumber := func() error {
|
||||||
number++
|
number++
|
||||||
@@ -150,14 +160,16 @@ func main() {
|
|||||||
|
|
||||||
err := retry.Retry(increaseNumber, retry.RetryTimes(2))
|
err := retry.Retry(increaseNumber, retry.RetryTimes(2))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err) //2022/02/01 18:42:25 function main.main.func1 run failed after 2 times retry exit status 1
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// function main.main.func1 run failed after 2 times retry
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryDuration">RetryDuration</span>
|
### <span id="RetryDuration">RetryDuration</span>
|
||||||
|
|
||||||
<p>Set duration of retries. Default duration is 3 second.</p>
|
<p>Set duration of retries. Default duration is 3 second.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -165,6 +177,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RetryDuration(d time.Duration)
|
func RetryDuration(d time.Duration)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -178,7 +191,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var number int
|
number := 0
|
||||||
increaseNumber := func() error {
|
increaseNumber := func() error {
|
||||||
number++
|
number++
|
||||||
if number == 3 {
|
if number == 3 {
|
||||||
@@ -187,17 +200,22 @@ func main() {
|
|||||||
return errors.New("error occurs")
|
return errors.New("error occurs")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50))
|
duration := retry.RetryDuration(time.Microsecond*50)
|
||||||
|
|
||||||
|
err := retry.Retry(increaseNumber, duration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(number) //3
|
fmt.Println(number)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Retry">Retry</span>
|
### <span id="Retry">Retry</span>
|
||||||
|
|
||||||
<p>Executes the retryFunc repeatedly until it was successful or canceled by the context.</p>
|
<p>Executes the retryFunc repeatedly until it was successful or canceled by the context.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -205,6 +223,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Retry(retryFunc RetryFunc, opts ...Option) error
|
func Retry(retryFunc RetryFunc, opts ...Option) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -218,7 +237,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var number int
|
number := 0
|
||||||
increaseNumber := func() error {
|
increaseNumber := func() error {
|
||||||
number++
|
number++
|
||||||
if number == 3 {
|
if number == 3 {
|
||||||
@@ -227,11 +246,16 @@ func main() {
|
|||||||
return errors.New("error occurs")
|
return errors.New("error occurs")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50))
|
duration := retry.RetryDuration(time.Microsecond*50)
|
||||||
|
|
||||||
|
err := retry.Retry(increaseNumber, duration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(number) //3
|
fmt.Println(number)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
# Retry
|
# Retry
|
||||||
retry重试执行函数直到函数运行成功或被context cancel。
|
|
||||||
|
retry 重试执行函数直到函数运行成功或被 context cancel。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -7,10 +8,10 @@ retry重试执行函数直到函数运行成功或被context cancel。
|
|||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/retry/retry.go](https://github.com/duke-git/lancet/blob/main/retry/retry.go)
|
- [https://github.com/duke-git/lancet/blob/main/retry/retry.go](https://github.com/duke-git/lancet/blob/main/retry/retry.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/v2/retry"
|
"github.com/duke-git/lancet/v2/retry"
|
||||||
@@ -20,20 +21,19 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [Context](#Context)
|
- [Context](#Context)
|
||||||
- [Retry](#Retry)
|
- [Retry](#Retry)
|
||||||
- [RetryFunc](#RetryFunc)
|
- [RetryFunc](#RetryFunc)
|
||||||
- [RetryDuration](#RetryDuration)
|
- [RetryDuration](#RetryDuration)
|
||||||
- [RetryTimes](#RetryTimes)
|
- [RetryTimes](#RetryTimes)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Document 文档
|
||||||
## Document文档
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Context">Context</span>
|
### <span id="Context">Context</span>
|
||||||
|
|
||||||
<p>设置重试context参数</p>
|
<p>设置重试context参数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -41,6 +41,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func Context(ctx context.Context)
|
func Context(ctx context.Context)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -54,7 +55,8 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx, cancel := context.WithCancel(context.TODO())
|
ctx, cancel := context.WithCancel(context.TODO())
|
||||||
var number int
|
|
||||||
|
number := 0
|
||||||
increaseNumber := func() error {
|
increaseNumber := func() error {
|
||||||
number++
|
number++
|
||||||
if number > 3 {
|
if number > 3 {
|
||||||
@@ -63,21 +65,22 @@ func main() {
|
|||||||
return errors.New("error occurs")
|
return errors.New("error occurs")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := retry.Retry(increaseNumber,
|
duration := retry.RetryDuration(time.Microsecond*50)
|
||||||
retry.RetryDuration(time.Microsecond*50),
|
|
||||||
|
retry.Retry(increaseNumber,
|
||||||
|
duration,
|
||||||
retry.Context(ctx),
|
retry.Context(ctx),
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
fmt.Println(number)
|
||||||
fmt.Println(err) //retry is cancelled
|
|
||||||
}
|
// Output:
|
||||||
|
// 4
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryFunc">RetryFunc</span>
|
### <span id="RetryFunc">RetryFunc</span>
|
||||||
|
|
||||||
<p>被重试执行的函数</p>
|
<p>被重试执行的函数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -85,6 +88,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
type RetryFunc func() error
|
type RetryFunc func() error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -98,8 +102,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var number int
|
number := 0
|
||||||
increaseNumber := func() error {
|
var increaseNumber retry.RetryFunc = func() error {
|
||||||
number++
|
number++
|
||||||
if number == 3 {
|
if number == 3 {
|
||||||
return nil
|
return nil
|
||||||
@@ -107,18 +111,22 @@ func main() {
|
|||||||
return errors.New("error occurs")
|
return errors.New("error occurs")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50))
|
duration := retry.RetryDuration(time.Microsecond*50)
|
||||||
|
|
||||||
|
err := retry.Retry(increaseNumber, duration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(number) //3
|
fmt.Println(number)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryTimes">RetryTimes</span>
|
### <span id="RetryTimes">RetryTimes</span>
|
||||||
|
|
||||||
<p>设置重试次数,默认5</p>
|
<p>设置重试次数,默认5</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -126,6 +134,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RetryTimes(n uint)
|
func RetryTimes(n uint)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -139,7 +148,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var number int
|
number := 0
|
||||||
|
|
||||||
increaseNumber := func() error {
|
increaseNumber := func() error {
|
||||||
number++
|
number++
|
||||||
if number == 3 {
|
if number == 3 {
|
||||||
@@ -150,14 +160,16 @@ func main() {
|
|||||||
|
|
||||||
err := retry.Retry(increaseNumber, retry.RetryTimes(2))
|
err := retry.Retry(increaseNumber, retry.RetryTimes(2))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err) //2022/02/01 18:42:25 function main.main.func1 run failed after 2 times retry exit status 1
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// function main.main.func1 run failed after 2 times retry
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryDuration">RetryDuration</span>
|
### <span id="RetryDuration">RetryDuration</span>
|
||||||
|
|
||||||
<p>设置重试间隔时间,默认3秒</p>
|
<p>设置重试间隔时间,默认3秒</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -165,6 +177,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RetryDuration(d time.Duration)
|
func RetryDuration(d time.Duration)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -178,7 +191,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var number int
|
number := 0
|
||||||
increaseNumber := func() error {
|
increaseNumber := func() error {
|
||||||
number++
|
number++
|
||||||
if number == 3 {
|
if number == 3 {
|
||||||
@@ -187,17 +200,22 @@ func main() {
|
|||||||
return errors.New("error occurs")
|
return errors.New("error occurs")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50))
|
duration := retry.RetryDuration(time.Microsecond*50)
|
||||||
|
|
||||||
|
err := retry.Retry(increaseNumber, duration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(number) //3
|
fmt.Println(number)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Retry">Retry</span>
|
### <span id="Retry">Retry</span>
|
||||||
|
|
||||||
<p>重试执行函数retryFunc,直到函数运行成功,或被context停止</p>
|
<p>重试执行函数retryFunc,直到函数运行成功,或被context停止</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -205,6 +223,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Retry(retryFunc RetryFunc, opts ...Option) error
|
func Retry(retryFunc RetryFunc, opts ...Option) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -218,7 +237,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var number int
|
number := 0
|
||||||
increaseNumber := func() error {
|
increaseNumber := func() error {
|
||||||
number++
|
number++
|
||||||
if number == 3 {
|
if number == 3 {
|
||||||
@@ -227,11 +246,16 @@ func main() {
|
|||||||
return errors.New("error occurs")
|
return errors.New("error occurs")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50))
|
duration := retry.RetryDuration(time.Microsecond*50)
|
||||||
|
|
||||||
|
err := retry.Retry(increaseNumber, duration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(number) //3
|
fmt.Println(number)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 3
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
685
docs/slice.md
685
docs/slice.md
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -6,7 +6,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func ExampleRandInt() {
|
func ExampleRandInt() {
|
||||||
|
|
||||||
result := RandInt(1, 10)
|
result := RandInt(1, 10)
|
||||||
|
|
||||||
if result >= 1 && result < 10 {
|
if result >= 1 && result < 10 {
|
||||||
|
|||||||
Reference in New Issue
Block a user