mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
Compare commits
4 Commits
4c5524354c
...
bc3c080ac3
| Author | SHA1 | Date | |
|---|---|---|---|
| bc3c080ac3 | |||
| d3fab15af3 | |||
| 6e3e411d46 | |||
| f976941e36 |
+118
-75
@@ -1,4 +1,5 @@
|
||||
# Netutil
|
||||
|
||||
Package netutil contains functions to get net information and send http request.
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
@@ -7,13 +8,12 @@ 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/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)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Usage:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/duke-git/lancet/v2/netutil"
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Index
|
||||
|
||||
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
||||
- [EncodeUrl](#EncodeUrl)
|
||||
- [GetInternalIp](#GetInternalIp)
|
||||
@@ -48,8 +49,8 @@ import (
|
||||
|
||||
## Documentation
|
||||
|
||||
|
||||
### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span>
|
||||
|
||||
<p>Convert map to url query string.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -57,6 +58,7 @@ import (
|
||||
```go
|
||||
func ConvertMapToQueryString(param map[string]any) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -75,13 +77,13 @@ func main() {
|
||||
}
|
||||
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>
|
||||
|
||||
<p>Encode url query string values.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -89,6 +91,7 @@ func main() {
|
||||
```go
|
||||
func EncodeUrl(urlStr string) (string, error)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -102,16 +105,20 @@ import (
|
||||
func main() {
|
||||
urlAddr := "http://www.lancet.com?a=1&b=[2]"
|
||||
encodedUrl, err := netutil.EncodeUrl(urlAddr)
|
||||
|
||||
if err != nil {
|
||||
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>
|
||||
|
||||
<p>Get internal ip information.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -119,6 +126,7 @@ func main() {
|
||||
```go
|
||||
func GetInternalIp() string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -134,13 +142,15 @@ func main() {
|
||||
internalIp := netutil.GetInternalIp()
|
||||
ip := net.ParseIP(internalIp)
|
||||
|
||||
fmt.Println(ip) //192.168.1.9
|
||||
fmt.Println(ip)
|
||||
|
||||
// Output:
|
||||
// 192.168.1.9
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="GetIps">GetIps</span>
|
||||
|
||||
<p>Get all ipv4 list.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -148,6 +158,7 @@ func main() {
|
||||
```go
|
||||
func GetIps() []string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -161,13 +172,15 @@ import (
|
||||
|
||||
func main() {
|
||||
ips := netutil.GetIps()
|
||||
fmt.Println(ips) //[192.168.1.9]
|
||||
fmt.Println(ips)
|
||||
|
||||
// Output:
|
||||
// [192.168.1.9]
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="GetMacAddrs">GetMacAddrs</span>
|
||||
|
||||
<p>Get all mac addresses list.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -175,6 +188,7 @@ func main() {
|
||||
```go
|
||||
func GetMacAddrs() []string {
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -187,14 +201,16 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
addrs := netutil.GetMacAddrs()
|
||||
fmt.Println(addrs)
|
||||
macAddrs := netutil.GetMacAddrs()
|
||||
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>
|
||||
|
||||
<p>Get public ip information.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -216,6 +232,7 @@ type PublicIpInfo struct {
|
||||
Ip string `json:"query"`
|
||||
}
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -236,9 +253,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="GetRequestPublicIp">GetRequestPublicIp</span>
|
||||
|
||||
<p>Get http request public ip.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -246,6 +262,7 @@ func main() {
|
||||
```go
|
||||
func GetRequestPublicIp(req *http.Request) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -259,29 +276,23 @@ import (
|
||||
func main() {
|
||||
ip := "36.112.24.10"
|
||||
|
||||
request1 := http.Request{
|
||||
request := http.Request{
|
||||
Method: "GET",
|
||||
Header: http.Header{
|
||||
"X-Forwarded-For": {ip},
|
||||
},
|
||||
}
|
||||
publicIp1 := netutil.GetRequestPublicIp(&request1)
|
||||
fmt.Println(publicIp1) //36.112.24.10
|
||||
publicIp := netutil.GetRequestPublicIp(&request)
|
||||
|
||||
request2 := http.Request{
|
||||
Method: "GET",
|
||||
Header: http.Header{
|
||||
"X-Real-Ip": {ip},
|
||||
},
|
||||
}
|
||||
publicIp2 := netutil.GetRequestPublicIp(&request2)
|
||||
fmt.Println(publicIp2) //36.112.24.10
|
||||
fmt.Println(publicIp)
|
||||
|
||||
// Output:
|
||||
// 36.112.24.10
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="IsPublicIP">IsPublicIP</span>
|
||||
|
||||
<p>Checks if an ip is public or not.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -289,6 +300,7 @@ func main() {
|
||||
```go
|
||||
func IsPublicIP(IP net.IP) bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -301,18 +313,23 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
ip1 := net.ParseIP("192.168.0.1")
|
||||
ip2 := net.ParseIP("36.112.24.10")
|
||||
ip1 := netutil.IsPublicIP(net.ParseIP("127.0.0.1"))
|
||||
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(netutil.IsPublicIP(ip2)) //true
|
||||
fmt.Println(ip1)
|
||||
fmt.Println(ip2)
|
||||
fmt.Println(ip3)
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// false
|
||||
// true
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="IsInternalIP">IsInternalIP</span>
|
||||
|
||||
<p>Checks if an ip is intranet or not.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -320,6 +337,7 @@ func main() {
|
||||
```go
|
||||
func IsInternalIP(IP net.IP) bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -332,16 +350,23 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
ip1 := net.ParseIP("127.0.0.1")
|
||||
ip2 := net.ParseIP("36.112.24.10")
|
||||
ip1 := netutil.IsInternalIP(net.ParseIP("127.0.0.1"))
|
||||
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(netutil.IsInternalIP(ip2)) //false
|
||||
fmt.Println(ip1)
|
||||
fmt.Println(ip2)
|
||||
fmt.Println(ip3)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="HttpRequest">HttpRequest</span>
|
||||
|
||||
<p>HttpRequest is a struct used to abstract HTTP request entity.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -356,6 +381,7 @@ type HttpRequest struct {
|
||||
Body []byte
|
||||
}
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -384,8 +410,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <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>
|
||||
@@ -412,6 +438,7 @@ func NewHttpClient() *HttpClient
|
||||
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
|
||||
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -433,9 +460,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="SendRequest">SendRequest</span>
|
||||
|
||||
<p>Use HttpClient to send HTTP request.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -443,6 +469,7 @@ func main() {
|
||||
```go
|
||||
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -464,7 +491,7 @@ func main() {
|
||||
httpClient := netutil.NewHttpClient()
|
||||
resp, err := httpClient.SendRequest(request)
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
type Todo struct {
|
||||
@@ -475,15 +502,20 @@ func main() {
|
||||
}
|
||||
|
||||
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>
|
||||
|
||||
<p>Decode http response into target object.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -491,6 +523,7 @@ func main() {
|
||||
```go
|
||||
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -512,7 +545,7 @@ func main() {
|
||||
httpClient := netutil.NewHttpClient()
|
||||
resp, err := httpClient.SendRequest(request)
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
type Todo struct {
|
||||
@@ -523,14 +556,20 @@ func main() {
|
||||
}
|
||||
|
||||
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>
|
||||
|
||||
<p>Convert struct to url values, only convert the field which is exported and has `json` tag.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -538,6 +577,7 @@ func main() {
|
||||
```go
|
||||
func StructToUrlValues(targetStruct any) url.Values
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -551,22 +591,25 @@ import (
|
||||
func main() {
|
||||
type TodoQuery struct {
|
||||
Id int `json:"id"`
|
||||
UserId int `json:"userId"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
todoQuery := TodoQuery{
|
||||
Id: 1,
|
||||
UserId: 2,
|
||||
Name: "Test",
|
||||
}
|
||||
todoValues := netutil.StructToUrlValues(todoQuery)
|
||||
|
||||
fmt.Println(todoValues.Get("id")) //1
|
||||
fmt.Println(todoValues.Get("userId")) //2
|
||||
fmt.Println(todoValues.Get("id"))
|
||||
fmt.Println(todoValues.Get("name"))
|
||||
|
||||
// Output:
|
||||
// 1
|
||||
// Test
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
||||
|
||||
<p>Send http get request.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -578,6 +621,7 @@ func main() {
|
||||
// params[3] is http client which type should be http.Client.
|
||||
func HttpGet(url string, params ...any) (*http.Response, error)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -606,9 +650,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
||||
|
||||
<p>Send http post request.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -620,6 +663,7 @@ func main() {
|
||||
// params[3] is http client which type should be http.Client.
|
||||
func HttpPost(url string, params ...any) (*http.Response, error)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -655,9 +699,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
||||
|
||||
<p>Send http put request.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -669,6 +712,7 @@ func main() {
|
||||
// params[3] is http client which type should be http.Client.
|
||||
func HttpPut(url string, params ...any) (*http.Response, error)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -705,9 +749,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
||||
|
||||
<p>Send http delete request.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -719,6 +762,7 @@ func main() {
|
||||
// params[3] is http client which type should be http.Client.
|
||||
func HttpDelete(url string, params ...any) (*http.Response, error)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -744,9 +788,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
||||
|
||||
<p>Send http patch request.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -758,6 +801,7 @@ func main() {
|
||||
// params[3] is http client which type should be http.Client.
|
||||
func HttpPatch(url string, params ...any) (*http.Response, error)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -794,9 +838,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="ParseHttpResponse">ParseHttpResponse</span>
|
||||
|
||||
<p>Decode http response to specified interface.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -804,6 +847,7 @@ func main() {
|
||||
```go
|
||||
func ParseHttpResponse(resp *http.Response, obj any) error
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -844,4 +888,3 @@ func main() {
|
||||
fmt.Println(toDoResp)
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
+120
-75
@@ -1,4 +1,5 @@
|
||||
# Netutil
|
||||
|
||||
netutil 网络包支持获取 ip 地址,发送 http 请求。
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
@@ -7,13 +8,12 @@ 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/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)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## 用法:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/duke-git/lancet/v2/netutil"
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## 目录
|
||||
|
||||
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
||||
- [EncodeUrl](#EncodeUrl)
|
||||
- [GetInternalIp](#GetInternalIp)
|
||||
@@ -48,8 +49,8 @@ import (
|
||||
|
||||
## 文档
|
||||
|
||||
|
||||
### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span>
|
||||
|
||||
<p>将map转换成http查询字符串.</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -57,6 +58,7 @@ import (
|
||||
```go
|
||||
func ConvertMapToQueryString(param map[string]any) string
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -75,13 +77,15 @@ func main() {
|
||||
}
|
||||
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>
|
||||
|
||||
<p>编码url query string的值</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -89,6 +93,7 @@ func main() {
|
||||
```go
|
||||
func EncodeUrl(urlStr string) (string, error)
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -102,17 +107,20 @@ import (
|
||||
func main() {
|
||||
urlAddr := "http://www.lancet.com?a=1&b=[2]"
|
||||
encodedUrl, err := netutil.EncodeUrl(urlAddr)
|
||||
|
||||
if err != nil {
|
||||
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>
|
||||
|
||||
<p>获取内部ip</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -120,6 +128,7 @@ func main() {
|
||||
```go
|
||||
func GetInternalIp() string
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -135,12 +144,15 @@ func main() {
|
||||
internalIp := netutil.GetInternalIp()
|
||||
ip := net.ParseIP(internalIp)
|
||||
|
||||
fmt.Println(ip) //192.168.1.9
|
||||
fmt.Println(ip)
|
||||
|
||||
// Output:
|
||||
// 192.168.1.9
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="GetIps">GetIps</span>
|
||||
|
||||
<p>获取ipv4地址列表</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -148,6 +160,7 @@ func main() {
|
||||
```go
|
||||
func GetIps() []string
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -161,13 +174,15 @@ import (
|
||||
|
||||
func main() {
|
||||
ips := netutil.GetIps()
|
||||
fmt.Println(ips) //[192.168.1.9]
|
||||
fmt.Println(ips)
|
||||
|
||||
// Output:
|
||||
// [192.168.1.9]
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="GetMacAddrs">GetMacAddrs</span>
|
||||
|
||||
<p>获取mac地址列</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -175,6 +190,7 @@ func main() {
|
||||
```go
|
||||
func GetMacAddrs() []string {
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -187,14 +203,16 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
addrs := netutil.GetMacAddrs()
|
||||
fmt.Println(addrs)
|
||||
macAddrs := netutil.GetMacAddrs()
|
||||
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>
|
||||
|
||||
<p>获取公网ip信息</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -216,6 +234,7 @@ type PublicIpInfo struct {
|
||||
Ip string `json:"query"`
|
||||
}
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -236,9 +255,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="GetRequestPublicIp">GetRequestPublicIp</span>
|
||||
|
||||
<p>获取http请求ip</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -246,6 +264,7 @@ func main() {
|
||||
```go
|
||||
func GetRequestPublicIp(req *http.Request) string
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -259,30 +278,23 @@ import (
|
||||
func main() {
|
||||
ip := "36.112.24.10"
|
||||
|
||||
request1 := http.Request{
|
||||
request := http.Request{
|
||||
Method: "GET",
|
||||
Header: http.Header{
|
||||
"X-Forwarded-For": {ip},
|
||||
},
|
||||
}
|
||||
publicIp1 := netutil.GetRequestPublicIp(&request1)
|
||||
fmt.Println(publicIp1) //36.112.24.10
|
||||
publicIp := netutil.GetRequestPublicIp(&request)
|
||||
|
||||
request2 := http.Request{
|
||||
Method: "GET",
|
||||
Header: http.Header{
|
||||
"X-Real-Ip": {ip},
|
||||
},
|
||||
}
|
||||
publicIp2 := netutil.GetRequestPublicIp(&request2)
|
||||
fmt.Println(publicIp2) //36.112.24.10
|
||||
fmt.Println(publicIp)
|
||||
|
||||
// Output:
|
||||
// 36.112.24.10
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="IsPublicIP">IsPublicIP</span>
|
||||
|
||||
<p>判断ip是否是公共ip</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -290,6 +302,7 @@ func main() {
|
||||
```go
|
||||
func IsPublicIP(IP net.IP) bool
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -302,17 +315,23 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
ip1 := net.ParseIP("192.168.0.1")
|
||||
ip2 := net.ParseIP("36.112.24.10")
|
||||
ip1 := netutil.IsPublicIP(net.ParseIP("127.0.0.1"))
|
||||
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(netutil.IsPublicIP(ip2)) //true
|
||||
fmt.Println(ip1)
|
||||
fmt.Println(ip2)
|
||||
fmt.Println(ip3)
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// false
|
||||
// true
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="IsInternalIP">IsInternalIP</span>
|
||||
|
||||
<p>判断ip是否是局域网ip.</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -320,6 +339,7 @@ func main() {
|
||||
```go
|
||||
func IsInternalIP(IP net.IP) bool
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -332,16 +352,23 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
ip1 := net.ParseIP("127.0.0.1")
|
||||
ip2 := net.ParseIP("36.112.24.10")
|
||||
ip1 := netutil.IsInternalIP(net.ParseIP("127.0.0.1"))
|
||||
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(netutil.IsInternalIP(ip2)) //false
|
||||
fmt.Println(ip1)
|
||||
fmt.Println(ip2)
|
||||
fmt.Println(ip3)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="HttpRequest">HttpRequest</span>
|
||||
|
||||
<p>HttpRequest用于抽象HTTP请求实体的结构</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -356,6 +383,7 @@ type HttpRequest struct {
|
||||
Body []byte
|
||||
}
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -384,8 +412,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="HttpClient">HttpClient</span>
|
||||
|
||||
<p>HttpClient是用于发送HTTP请求的结构体。它可以用一些配置参数或无配置实例化.</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -412,6 +440,7 @@ func NewHttpClient() *HttpClient
|
||||
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
|
||||
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -433,9 +462,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="SendRequest">SendRequest</span>
|
||||
|
||||
<p>HttpClient发送http请求</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -443,6 +471,7 @@ func main() {
|
||||
```go
|
||||
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -464,7 +493,7 @@ func main() {
|
||||
httpClient := netutil.NewHttpClient()
|
||||
resp, err := httpClient.SendRequest(request)
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
type Todo struct {
|
||||
@@ -475,15 +504,20 @@ func main() {
|
||||
}
|
||||
|
||||
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>
|
||||
|
||||
<p>解析http响应体到目标结构体</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -491,6 +525,7 @@ func main() {
|
||||
```go
|
||||
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -512,7 +547,7 @@ func main() {
|
||||
httpClient := netutil.NewHttpClient()
|
||||
resp, err := httpClient.SendRequest(request)
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
type Todo struct {
|
||||
@@ -523,14 +558,20 @@ func main() {
|
||||
}
|
||||
|
||||
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>
|
||||
|
||||
<p>将结构体转为url values, 仅转化结构体导出字段并且包含`json` tag</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -538,6 +579,7 @@ func main() {
|
||||
```go
|
||||
func StructToUrlValues(targetStruct any) url.Values
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -551,22 +593,25 @@ import (
|
||||
func main() {
|
||||
type TodoQuery struct {
|
||||
Id int `json:"id"`
|
||||
UserId int `json:"userId"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
todoQuery := TodoQuery{
|
||||
Id: 1,
|
||||
UserId: 2,
|
||||
Name: "Test",
|
||||
}
|
||||
todoValues := netutil.StructToUrlValues(todoQuery)
|
||||
|
||||
fmt.Println(todoValues.Get("id")) //1
|
||||
fmt.Println(todoValues.Get("userId")) //2
|
||||
fmt.Println(todoValues.Get("id"))
|
||||
fmt.Println(todoValues.Get("name"))
|
||||
|
||||
// Output:
|
||||
// 1
|
||||
// Test
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
||||
|
||||
<p>发送http get请求</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -578,6 +623,7 @@ func main() {
|
||||
// params[3] http client,类型必须是http.Client
|
||||
func HttpGet(url string, params ...any) (*http.Response, error)
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -606,9 +652,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
||||
|
||||
<p>发送http post请求</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -620,6 +665,7 @@ func main() {
|
||||
// params[3] http client,类型必须是http.Client
|
||||
func HttpPost(url string, params ...any) (*http.Response, error)
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -655,9 +701,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
||||
|
||||
<p>发送http put请求</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -669,6 +714,7 @@ func main() {
|
||||
// params[3] http client,类型必须是http.Client
|
||||
func HttpPut(url string, params ...any) (*http.Response, error)
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -705,9 +751,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
||||
|
||||
<p>发送http delete请求</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -719,6 +764,7 @@ func main() {
|
||||
// params[3] http client,类型必须是http.Client
|
||||
func HttpDelete(url string, params ...any) (*http.Response, error)
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -744,9 +790,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
||||
|
||||
<p>发送http patch请求</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -758,6 +803,7 @@ func main() {
|
||||
// params[3] http client,类型必须是http.Client
|
||||
func HttpPatch(url string, params ...any) (*http.Response, error)
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -794,9 +840,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="ParseHttpResponse">ParseHttpResponse</span>
|
||||
|
||||
<p>将http请求响应解码成特定struct值</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -804,6 +849,7 @@ func main() {
|
||||
```go
|
||||
func ParseHttpResponse(resp *http.Response, obj any) error
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -844,4 +890,3 @@ func main() {
|
||||
fmt.Println(toDoResp)
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
+56
-32
@@ -1,4 +1,5 @@
|
||||
# Retry
|
||||
|
||||
Package retry is for executing a function repeatedly until it was successful or canceled by the context.
|
||||
|
||||
<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)
|
||||
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Usage:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/duke-git/lancet/v2/retry"
|
||||
@@ -20,6 +21,7 @@ import (
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Index
|
||||
|
||||
- [Context](#Context)
|
||||
- [Retry](#Retry)
|
||||
- [RetryFunc](#RetryFunc)
|
||||
@@ -30,8 +32,8 @@ import (
|
||||
|
||||
## Documentation
|
||||
|
||||
|
||||
### <span id="Context">Context</span>
|
||||
|
||||
<p>Set retry context config, can cancel the retry with context.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -39,6 +41,7 @@ import (
|
||||
```go
|
||||
func Context(ctx context.Context)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -52,7 +55,8 @@ import (
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithCancel(context.TODO())
|
||||
var number int
|
||||
|
||||
number := 0
|
||||
increaseNumber := func() error {
|
||||
number++
|
||||
if number > 3 {
|
||||
@@ -61,21 +65,22 @@ func main() {
|
||||
return errors.New("error occurs")
|
||||
}
|
||||
|
||||
err := retry.Retry(increaseNumber,
|
||||
retry.RetryDuration(time.Microsecond*50),
|
||||
duration := retry.RetryDuration(time.Microsecond*50)
|
||||
|
||||
retry.Retry(increaseNumber,
|
||||
duration,
|
||||
retry.Context(ctx),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err) //retry is cancelled
|
||||
}
|
||||
fmt.Println(number)
|
||||
|
||||
// Output:
|
||||
// 4
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="RetryFunc">RetryFunc</span>
|
||||
|
||||
<p>Function that retry executes.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -83,6 +88,7 @@ func main() {
|
||||
```go
|
||||
type RetryFunc func() error
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -96,9 +102,8 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
var number int
|
||||
|
||||
increaseNumber := func() error {
|
||||
number := 0
|
||||
var increaseNumber retry.RetryFunc = func() error {
|
||||
number++
|
||||
if number == 3 {
|
||||
return nil
|
||||
@@ -106,18 +111,22 @@ func main() {
|
||||
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 {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(number) //3
|
||||
fmt.Println(number)
|
||||
|
||||
// Output:
|
||||
// 3
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="RetryTimes">RetryTimes</span>
|
||||
|
||||
<p>Set times of retry. Default times is 5.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -125,6 +134,7 @@ func main() {
|
||||
```go
|
||||
func RetryTimes(n uint)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -138,7 +148,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
var number int
|
||||
number := 0
|
||||
|
||||
increaseNumber := func() error {
|
||||
number++
|
||||
@@ -150,14 +160,16 @@ func main() {
|
||||
|
||||
err := retry.Retry(increaseNumber, retry.RetryTimes(2))
|
||||
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>
|
||||
|
||||
<p>Set duration of retries. Default duration is 3 second.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -165,6 +177,7 @@ func main() {
|
||||
```go
|
||||
func RetryDuration(d time.Duration)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -178,7 +191,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
var number int
|
||||
number := 0
|
||||
increaseNumber := func() error {
|
||||
number++
|
||||
if number == 3 {
|
||||
@@ -187,17 +200,22 @@ func main() {
|
||||
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 {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(number) //3
|
||||
fmt.Println(number)
|
||||
|
||||
// Output:
|
||||
// 3
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="Retry">Retry</span>
|
||||
|
||||
<p>Executes the retryFunc repeatedly until it was successful or canceled by the context.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -205,6 +223,7 @@ func main() {
|
||||
```go
|
||||
func Retry(retryFunc RetryFunc, opts ...Option) error
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -218,7 +237,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
var number int
|
||||
number := 0
|
||||
increaseNumber := func() error {
|
||||
number++
|
||||
if number == 3 {
|
||||
@@ -227,11 +246,16 @@ func main() {
|
||||
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 {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(number) //3
|
||||
fmt.Println(number)
|
||||
|
||||
// Output:
|
||||
// 3
|
||||
}
|
||||
```
|
||||
|
||||
+57
-33
@@ -1,4 +1,5 @@
|
||||
# Retry
|
||||
|
||||
retry 重试执行函数直到函数运行成功或被 context cancel。
|
||||
|
||||
<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)
|
||||
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## 用法:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/duke-git/lancet/v2/retry"
|
||||
@@ -20,20 +21,19 @@ import (
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## 目录
|
||||
|
||||
- [Context](#Context)
|
||||
- [Retry](#Retry)
|
||||
- [RetryFunc](#RetryFunc)
|
||||
- [RetryDuration](#RetryDuration)
|
||||
- [RetryTimes](#RetryTimes)
|
||||
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
|
||||
## Document 文档
|
||||
|
||||
|
||||
### <span id="Context">Context</span>
|
||||
|
||||
<p>设置重试context参数</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -41,6 +41,7 @@ import (
|
||||
```go
|
||||
func Context(ctx context.Context)
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -54,7 +55,8 @@ import (
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithCancel(context.TODO())
|
||||
var number int
|
||||
|
||||
number := 0
|
||||
increaseNumber := func() error {
|
||||
number++
|
||||
if number > 3 {
|
||||
@@ -63,21 +65,22 @@ func main() {
|
||||
return errors.New("error occurs")
|
||||
}
|
||||
|
||||
err := retry.Retry(increaseNumber,
|
||||
retry.RetryDuration(time.Microsecond*50),
|
||||
duration := retry.RetryDuration(time.Microsecond*50)
|
||||
|
||||
retry.Retry(increaseNumber,
|
||||
duration,
|
||||
retry.Context(ctx),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err) //retry is cancelled
|
||||
}
|
||||
fmt.Println(number)
|
||||
|
||||
// Output:
|
||||
// 4
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="RetryFunc">RetryFunc</span>
|
||||
|
||||
<p>被重试执行的函数</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -85,6 +88,7 @@ func main() {
|
||||
```go
|
||||
type RetryFunc func() error
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -98,8 +102,8 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
var number int
|
||||
increaseNumber := func() error {
|
||||
number := 0
|
||||
var increaseNumber retry.RetryFunc = func() error {
|
||||
number++
|
||||
if number == 3 {
|
||||
return nil
|
||||
@@ -107,18 +111,22 @@ func main() {
|
||||
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 {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(number) //3
|
||||
fmt.Println(number)
|
||||
|
||||
// Output:
|
||||
// 3
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="RetryTimes">RetryTimes</span>
|
||||
|
||||
<p>设置重试次数,默认5</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -126,6 +134,7 @@ func main() {
|
||||
```go
|
||||
func RetryTimes(n uint)
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -139,7 +148,8 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
var number int
|
||||
number := 0
|
||||
|
||||
increaseNumber := func() error {
|
||||
number++
|
||||
if number == 3 {
|
||||
@@ -150,14 +160,16 @@ func main() {
|
||||
|
||||
err := retry.Retry(increaseNumber, retry.RetryTimes(2))
|
||||
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>
|
||||
|
||||
<p>设置重试间隔时间,默认3秒</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -165,6 +177,7 @@ func main() {
|
||||
```go
|
||||
func RetryDuration(d time.Duration)
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -178,7 +191,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
var number int
|
||||
number := 0
|
||||
increaseNumber := func() error {
|
||||
number++
|
||||
if number == 3 {
|
||||
@@ -187,17 +200,22 @@ func main() {
|
||||
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 {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(number) //3
|
||||
fmt.Println(number)
|
||||
|
||||
// Output:
|
||||
// 3
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="Retry">Retry</span>
|
||||
|
||||
<p>重试执行函数retryFunc,直到函数运行成功,或被context停止</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -205,6 +223,7 @@ func main() {
|
||||
```go
|
||||
func Retry(retryFunc RetryFunc, opts ...Option) error
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -218,7 +237,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
var number int
|
||||
number := 0
|
||||
increaseNumber := func() error {
|
||||
number++
|
||||
if number == 3 {
|
||||
@@ -227,11 +246,16 @@ func main() {
|
||||
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 {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(number) //3
|
||||
fmt.Println(number)
|
||||
|
||||
// Output:
|
||||
// 3
|
||||
}
|
||||
```
|
||||
|
||||
+481
-204
File diff suppressed because it is too large
Load Diff
+483
-204
File diff suppressed because it is too large
Load Diff
+43
-13
@@ -1,4 +1,5 @@
|
||||
# Strutil
|
||||
|
||||
Package strutil contains some functions to manipulate string.
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
@@ -7,10 +8,10 @@ Package strutil contains some functions to manipulate string.
|
||||
|
||||
- [https://github.com/duke-git/lancet/blob/main/strutil/string.go](https://github.com/duke-git/lancet/blob/main/strutil/string.go)
|
||||
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Usage:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/duke-git/lancet/v2/strutil"
|
||||
@@ -20,6 +21,7 @@ import (
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Index
|
||||
|
||||
- [After](#After)
|
||||
- [AfterLast](#AfterLast)
|
||||
- [Before](#Before)
|
||||
@@ -41,13 +43,12 @@ import (
|
||||
- [Wrap](#Wrap)
|
||||
- [Unwrap](#Unwrap)
|
||||
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Documentation
|
||||
|
||||
|
||||
### <span id="After">After</span>
|
||||
|
||||
<p>Returns the substring after the first occurrence of a specified string in the source string.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -55,6 +56,7 @@ import (
|
||||
```go
|
||||
func After(s, char string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -86,6 +88,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="AfterLast">AfterLast</span>
|
||||
|
||||
<p>Returns the substring after the last occurrence of a specified string in the source string.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -93,6 +96,7 @@ func main() {
|
||||
```go
|
||||
func AfterLast(s, char string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -124,6 +128,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="Before">Before</span>
|
||||
|
||||
<p>Returns the substring of the source string up to the first occurrence of the specified string.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -131,6 +136,7 @@ func main() {
|
||||
```go
|
||||
func Before(s, char string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -159,6 +165,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="BeforeLast">BeforeLast</span>
|
||||
|
||||
<p>Returns the substring of the source string up to the last occurrence of the specified string.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -166,6 +173,7 @@ func main() {
|
||||
```go
|
||||
func BeforeLast(s, char string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -194,6 +202,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="CamelCase">CamelCase</span>
|
||||
|
||||
<p>Coverts string to camelCase string, non letters and numbers will be ignored.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -201,6 +210,7 @@ func main() {
|
||||
```go
|
||||
func CamelCase(s string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -227,6 +237,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="KebabCase">KebabCase</span>
|
||||
|
||||
<p>KebabCase covert string to kebab-case, non letters and numbers will be ignored.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -234,6 +245,7 @@ func main() {
|
||||
```go
|
||||
func KebabCase(s string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -260,6 +272,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="UpperKebabCase">UpperKebabCase</span>
|
||||
|
||||
<p>UpperKebabCase covert string to upper KEBAB-CASE, non letters and numbers will be ignored.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -267,6 +280,7 @@ func main() {
|
||||
```go
|
||||
func UpperKebabCase(s string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -293,6 +307,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="Capitalize">Capitalize</span>
|
||||
|
||||
<p>Convert the first character of a string to upper case.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -300,6 +315,7 @@ func main() {
|
||||
```go
|
||||
func Capitalize(s string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -326,6 +342,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="IsString">IsString</span>
|
||||
|
||||
<p>Check if the value's data type is string.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -333,6 +350,7 @@ func main() {
|
||||
```go
|
||||
func IsString(v any) bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -364,6 +382,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="LowerFirst">LowerFirst</span>
|
||||
|
||||
<p>Convert the first character of string to lower case.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -371,6 +390,7 @@ func main() {
|
||||
```go
|
||||
func LowerFirst(s string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -396,6 +416,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="UpperFirst">UpperFirst</span>
|
||||
|
||||
<p>Convert the first character of string to upper case.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -403,6 +424,7 @@ func main() {
|
||||
```go
|
||||
func UpperFirst(s string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -428,6 +450,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="PadEnd">PadEnd</span>
|
||||
|
||||
<p>Pads string on the right side if it's shorter than size.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -435,6 +458,7 @@ func main() {
|
||||
```go
|
||||
func PadEnd(source string, size int, padStr string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -472,6 +496,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="PadStart">PadStart</span>
|
||||
|
||||
<p>Pads string on the left side if it's shorter than size.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -479,6 +504,7 @@ func main() {
|
||||
```go
|
||||
func PadStart(source string, size int, padStr string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -516,6 +542,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="Reverse">Reverse</span>
|
||||
|
||||
<p>Return string whose char order is reversed to the given string.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -523,6 +550,7 @@ func main() {
|
||||
```go
|
||||
func Reverse(s string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -545,6 +573,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="SnakeCase">SnakeCase</span>
|
||||
|
||||
<p>Coverts string to snake_case, non letters and numbers will be ignored.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -552,6 +581,7 @@ func main() {
|
||||
```go
|
||||
func SnakeCase(s string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -578,6 +608,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="UpperSnakeCase">UpperSnakeCase</span>
|
||||
|
||||
<p>Coverts string to upper KEBAB-CASE, non letters and numbers will be ignored.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -585,6 +616,7 @@ func main() {
|
||||
```go
|
||||
func SnakeCase(s string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -611,6 +643,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="SplitEx">SplitEx</span>
|
||||
|
||||
<p>Split a given string whether the result contains empty string.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -618,6 +651,7 @@ func main() {
|
||||
```go
|
||||
func SplitEx(s, sep string, removeEmptyString bool) []string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -650,6 +684,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="Substring">Substring</span>
|
||||
|
||||
<p>Returns a substring of the specified length starting at the specified offset position.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -657,6 +692,7 @@ func main() {
|
||||
```go
|
||||
func Substring(s string, offset int, length uint) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -691,6 +727,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="Wrap">Wrap</span>
|
||||
|
||||
<p>Wrap a string with given string.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -698,6 +735,7 @@ func main() {
|
||||
```go
|
||||
func Wrap(str string, wrapWith string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -725,8 +763,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="Wrap">Wrap</span>
|
||||
|
||||
<p>Unwrap a given string from anther string. will change source string.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -734,6 +772,7 @@ func main() {
|
||||
```go
|
||||
func Unwrap(str string, wrapToken string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -763,12 +802,3 @@ func main() {
|
||||
// *foo*
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+43
-5
@@ -1,4 +1,5 @@
|
||||
# Strutil
|
||||
|
||||
strutil 包含处理字符串的相关函数。
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
@@ -7,10 +8,10 @@ strutil包含处理字符串的相关函数。
|
||||
|
||||
- [https://github.com/duke-git/lancet/blob/main/strutil/string.go](https://github.com/duke-git/lancet/blob/main/strutil/string.go)
|
||||
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## 用法:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/duke-git/lancet/v2/strutil"
|
||||
@@ -20,6 +21,7 @@ import (
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## 目录
|
||||
|
||||
- [After](#After)
|
||||
- [AfterLast](#AfterLast)
|
||||
- [Before](#Before)
|
||||
@@ -41,14 +43,12 @@ import (
|
||||
- [Wrap](#Wrap)
|
||||
- [Unwrap](#Unwrap)
|
||||
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
|
||||
## Documentation 文档
|
||||
|
||||
|
||||
### <span id="After">After</span>
|
||||
|
||||
<p>返回源字符串中特定字符串首次出现时的位置之后的子字符串。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -56,6 +56,7 @@ import (
|
||||
```go
|
||||
func After(s, char string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -87,6 +88,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="AfterLast">AfterLast</span>
|
||||
|
||||
<p>返回源字符串中指定字符串最后一次出现时的位置之后的子字符串。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -94,6 +96,7 @@ func main() {
|
||||
```go
|
||||
func AfterLast(s, char string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -125,6 +128,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="Before">Before</span>
|
||||
|
||||
<p>返回源字符串中指定字符串第一次出现时的位置之前的子字符串。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -132,6 +136,7 @@ func main() {
|
||||
```go
|
||||
func Before(s, char string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -160,6 +165,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="BeforeLast">BeforeLast</span>
|
||||
|
||||
<p>返回源字符串中指定字符串最后一次出现时的位置之前的子字符串。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -167,6 +173,7 @@ func main() {
|
||||
```go
|
||||
func BeforeLast(s, char string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -195,6 +202,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="CamelCase">CamelCase</span>
|
||||
|
||||
<p>将字符串转换为驼峰式字符串, 非字母和数字会被忽略。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -202,6 +210,7 @@ func main() {
|
||||
```go
|
||||
func CamelCase(s string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -228,6 +237,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="KebabCase">KebabCase</span>
|
||||
|
||||
<p>将字符串转换为kebab-case, 非字母和数字会被忽略。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -235,6 +245,7 @@ func main() {
|
||||
```go
|
||||
func KebabCase(s string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -261,6 +272,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="UpperKebabCase">UpperKebabCase</span>
|
||||
|
||||
<p>将字符串转换为大写KEBAB-CASE, 非字母和数字会被忽略。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -268,6 +280,7 @@ func main() {
|
||||
```go
|
||||
func UpperKebabCase(s string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -294,6 +307,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="Capitalize">Capitalize</span>
|
||||
|
||||
<p>将字符串的第一个字符转换为大写。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -301,6 +315,7 @@ func main() {
|
||||
```go
|
||||
func Capitalize(s string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -327,6 +342,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="IsString">IsString</span>
|
||||
|
||||
<p>判断传入参数的数据类型是否为字符串。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -334,6 +350,7 @@ func main() {
|
||||
```go
|
||||
func IsString(v any) bool
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -365,6 +382,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="LowerFirst">LowerFirst</span>
|
||||
|
||||
<p>将字符串的第一个字符转换为小写。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -372,6 +390,7 @@ func main() {
|
||||
```go
|
||||
func LowerFirst(s string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -397,6 +416,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="UpperFirst">UpperFirst</span>
|
||||
|
||||
<p>将字符串的第一个字符转换为大写形式。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -404,6 +424,7 @@ func main() {
|
||||
```go
|
||||
func UpperFirst(s string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -429,6 +450,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="PadEnd">PadEnd</span>
|
||||
|
||||
<p>如果字符串长度短于size,则在右侧填充字符串。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -436,6 +458,7 @@ func main() {
|
||||
```go
|
||||
func PadEnd(source string, size int, padStr string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -473,6 +496,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="PadStart">PadStart</span>
|
||||
|
||||
<p>如果字符串长度短于size,则在左侧填充字符串。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -480,6 +504,7 @@ func main() {
|
||||
```go
|
||||
func PadStart(source string, size int, padStr string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -517,6 +542,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="Reverse">Reverse</span>
|
||||
|
||||
<p>返回字符顺序与给定字符串相反的字符串。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -524,6 +550,7 @@ func main() {
|
||||
```go
|
||||
func Reverse(s string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -546,6 +573,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="SnakeCase">SnakeCase</span>
|
||||
|
||||
<p>将字符串转换为snake_case形式, 非字母和数字会被忽略。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -553,6 +581,7 @@ func main() {
|
||||
```go
|
||||
func SnakeCase(s string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -579,6 +608,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="UpperSnakeCase">UpperSnakeCase</span>
|
||||
|
||||
<p>将字符串转换为大写SNAKE_CASE形式, 非字母和数字会被忽略。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -586,6 +616,7 @@ func main() {
|
||||
```go
|
||||
func SnakeCase(s string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -612,6 +643,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="SplitEx">SplitEx</span>
|
||||
|
||||
<p>分割字符串为切片,removeEmptyString参数指定是否去除空字符串。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -619,6 +651,7 @@ func main() {
|
||||
```go
|
||||
func SplitEx(s, sep string, removeEmptyString bool) []string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -651,6 +684,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="Substring">Substring</span>
|
||||
|
||||
<p>根据指定的位置和长度截取字符串。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -658,6 +692,7 @@ func main() {
|
||||
```go
|
||||
func Substring(s string, offset int, length uint) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -692,6 +727,7 @@ func main() {
|
||||
```
|
||||
|
||||
### <span id="Wrap">Wrap</span>
|
||||
|
||||
<p>用另一个字符串包裹一个字符串。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -699,6 +735,7 @@ func main() {
|
||||
```go
|
||||
func Wrap(str string, wrapWith string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
@@ -726,8 +763,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### <span id="Wrap">Wrap</span>
|
||||
|
||||
<p>用另一个字符串解开包裹一个字符串。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -735,6 +772,7 @@ func main() {
|
||||
```go
|
||||
func Unwrap(str string, wrapToken string) string
|
||||
```
|
||||
|
||||
<b>示例:</b>
|
||||
|
||||
```go
|
||||
|
||||
+64
-37
@@ -1,4 +1,5 @@
|
||||
# System
|
||||
|
||||
Package system contains some functions about os, runtime, shell command.
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
@@ -7,10 +8,10 @@ Package system contains some functions about os, runtime, shell command.
|
||||
|
||||
- [https://github.com/duke-git/lancet/blob/main/system/os.go](https://github.com/duke-git/lancet/blob/main/system/os.go)
|
||||
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Usage:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/duke-git/lancet/v2/system"
|
||||
@@ -20,6 +21,7 @@ import (
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Index
|
||||
|
||||
- [IsWindows](#IsWindows)
|
||||
- [IsLinux](#IsLinux)
|
||||
- [IsMac](#IsMac)
|
||||
@@ -30,13 +32,12 @@ import (
|
||||
- [ExecCommand](#ExecCommand)
|
||||
- [GetOsBits](#GetOsBits)
|
||||
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Documentation
|
||||
|
||||
|
||||
### <span id="IsWindows">IsWindows</span>
|
||||
|
||||
<p>Check if current os is windows.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -44,6 +45,7 @@ import (
|
||||
```go
|
||||
func IsWindows() bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -58,10 +60,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="IsLinux">IsLinux</span>
|
||||
|
||||
<p>Check if current os is linux.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -69,6 +69,7 @@ func main() {
|
||||
```go
|
||||
func IsLinux() bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -83,9 +84,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="IsMac">IsMac</span>
|
||||
|
||||
<p>Check if current os is macos.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -93,6 +93,7 @@ func main() {
|
||||
```go
|
||||
func IsMac() bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -107,9 +108,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="GetOsEnv">GetOsEnv</span>
|
||||
|
||||
<p>Gets the value of the environment variable named by the key.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -117,6 +117,7 @@ func main() {
|
||||
```go
|
||||
func GetOsEnv(key string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -126,14 +127,19 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
fooEnv := system.GetOsEnv("foo")
|
||||
fmt.Println(fooEnv)
|
||||
err := system.SetOsEnv("foo", "abc")
|
||||
result := system.GetOsEnv("foo")
|
||||
|
||||
fmt.Println(err)
|
||||
fmt.Println(result)
|
||||
// Output:
|
||||
// <nil>
|
||||
// abc
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="SetOsEnv">SetOsEnv</span>
|
||||
|
||||
<p>Sets the value of the environment variable named by the key.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -141,6 +147,7 @@ func main() {
|
||||
```go
|
||||
func SetOsEnv(key, value string) error
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -150,15 +157,19 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := system.SetOsEnv("foo", "foo_value")
|
||||
err := system.SetOsEnv("foo", "abc")
|
||||
result := system.GetOsEnv("foo")
|
||||
|
||||
fmt.Println(err)
|
||||
fmt.Println(result)
|
||||
// Output:
|
||||
// <nil>
|
||||
// abc
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="RemoveOsEnv">RemoveOsEnv</span>
|
||||
|
||||
<p>Remove a single environment variable.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -166,6 +177,7 @@ func main() {
|
||||
```go
|
||||
func RemoveOsEnv(key string) error
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -175,16 +187,27 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := system.RemoveOsEnv("foo")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
err1 := system.SetOsEnv("foo", "abc")
|
||||
result1 := GetOsEnv("foo")
|
||||
|
||||
err2 := system.RemoveOsEnv("foo")
|
||||
result2 := GetOsEnv("foo")
|
||||
|
||||
fmt.Println(err1)
|
||||
fmt.Println(err2)
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
|
||||
// Output:
|
||||
// <nil>
|
||||
// <nil>
|
||||
// abc
|
||||
//
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="CompareOsEnv">CompareOsEnv</span>
|
||||
|
||||
<p>Get env named by the key and compare it with comparedEnv.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -192,6 +215,7 @@ func main() {
|
||||
```go
|
||||
func CompareOsEnv(key, comparedEnv string) bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -201,16 +225,22 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
system.SetOsEnv("foo", "foo_value")
|
||||
res := system.CompareOsEnv("foo", "foo_value")
|
||||
fmt.Println(res) //true
|
||||
err := system.SetOsEnv("foo", "abc")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
result := system.CompareOsEnv("foo", "abc")
|
||||
|
||||
fmt.Println(result)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="ExecCommand">CompareOsEnv</span>
|
||||
|
||||
<p>Execute shell command, return the stdout and stderr string of command, and error if error occur. param `command` is a complete command string, like, ls -a (linux), dir(windows), ping 127.0.0.1. In linux, use /bin/bash -c to execute command, In windows, use powershell.exe to execute command.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -218,6 +248,7 @@ func main() {
|
||||
```go
|
||||
func ExecCommand(command string) (stdout, stderr string, err error)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -248,10 +279,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="GetOsBits">GetOsBits</span>
|
||||
|
||||
<p>Get current os bits, 32bit or 64bit. return 32 or 64</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -259,6 +288,7 @@ func main() {
|
||||
```go
|
||||
func GetOsBits() int
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -269,9 +299,6 @@ import (
|
||||
|
||||
func main() {
|
||||
osBit := system.GetOsBits()
|
||||
fmt.Println(osBit)
|
||||
fmt.Println(osBit) // 32 or 64
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
+64
-41
@@ -1,4 +1,5 @@
|
||||
# System
|
||||
|
||||
system 包含 os, runtime, shell command 相关函数。
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
@@ -7,10 +8,10 @@ system包含os, runtime, shell command相关函数。
|
||||
|
||||
- [https://github.com/duke-git/lancet/blob/main/system/os.go](https://github.com/duke-git/lancet/blob/main/system/os.go)
|
||||
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## 用法:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/duke-git/lancet/v2/system"
|
||||
@@ -20,6 +21,7 @@ import (
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## 目录
|
||||
|
||||
- [IsWindows](#IsWindows)
|
||||
- [IsLinux](#IsLinux)
|
||||
- [IsMac](#IsMac)
|
||||
@@ -30,13 +32,12 @@ import (
|
||||
- [ExecCommand](#ExecCommand)
|
||||
- [GetOsBits](#GetOsBits)
|
||||
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Documentation 文档
|
||||
|
||||
|
||||
### <span id="IsWindows">IsWindows</span>
|
||||
|
||||
<p>检查当前操作系统是否是windows</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -44,6 +45,7 @@ import (
|
||||
```go
|
||||
func IsWindows() bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -58,10 +60,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="IsLinux">IsLinux</span>
|
||||
|
||||
<p>检查当前操作系统是否是linux</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -69,6 +69,7 @@ func main() {
|
||||
```go
|
||||
func IsLinux() bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -83,9 +84,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="IsMac">IsMac</span>
|
||||
|
||||
<p>检查当前操作系统是否是macos</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -93,6 +93,7 @@ func main() {
|
||||
```go
|
||||
func IsMac() bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -107,9 +108,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="GetOsEnv">GetOsEnv</span>
|
||||
|
||||
<p>获取key命名的环境变量的值</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -117,6 +117,7 @@ func main() {
|
||||
```go
|
||||
func GetOsEnv(key string) string
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -126,14 +127,19 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
fooEnv := system.GetOsEnv("foo")
|
||||
fmt.Println(fooEnv)
|
||||
err := system.SetOsEnv("foo", "abc")
|
||||
result := system.GetOsEnv("foo")
|
||||
|
||||
fmt.Println(err)
|
||||
fmt.Println(result)
|
||||
// Output:
|
||||
// <nil>
|
||||
// abc
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="SetOsEnv">SetOsEnv</span>
|
||||
|
||||
<p>设置由key命名的环境变量的值</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -141,6 +147,7 @@ func main() {
|
||||
```go
|
||||
func SetOsEnv(key, value string) error
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -150,15 +157,19 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := system.SetOsEnv("foo", "foo_value")
|
||||
err := system.SetOsEnv("foo", "abc")
|
||||
result := system.GetOsEnv("foo")
|
||||
|
||||
fmt.Println(err)
|
||||
fmt.Println(result)
|
||||
// Output:
|
||||
// <nil>
|
||||
// abc
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="RemoveOsEnv">RemoveOsEnv</span>
|
||||
|
||||
<p>删除单个环境变量</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -166,6 +177,7 @@ func main() {
|
||||
```go
|
||||
func RemoveOsEnv(key string) error
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -175,16 +187,27 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := system.RemoveOsEnv("foo")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
err1 := system.SetOsEnv("foo", "abc")
|
||||
result1 := GetOsEnv("foo")
|
||||
|
||||
err2 := system.RemoveOsEnv("foo")
|
||||
result2 := GetOsEnv("foo")
|
||||
|
||||
fmt.Println(err1)
|
||||
fmt.Println(err2)
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
|
||||
// Output:
|
||||
// <nil>
|
||||
// <nil>
|
||||
// abc
|
||||
//
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### <span id="CompareOsEnv">CompareOsEnv</span>
|
||||
|
||||
<p>获取key命名的环境变量值并与compareEnv进行比较</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -192,6 +215,7 @@ func main() {
|
||||
```go
|
||||
func CompareOsEnv(key, comparedEnv string) bool
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -201,16 +225,22 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
system.SetOsEnv("foo", "foo_value")
|
||||
res := system.CompareOsEnv("foo", "foo_value")
|
||||
fmt.Println(res) //true
|
||||
err := system.SetOsEnv("foo", "abc")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
result := system.CompareOsEnv("foo", "abc")
|
||||
|
||||
fmt.Println(result)
|
||||
|
||||
// Output:
|
||||
// true
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="ExecCommand">ExecCommand</span>
|
||||
|
||||
<p>执行shell命令,返回命令的stdout和stderr字符串,如果出现错误,则返回错误。参数`command`是一个完整的命令字符串,如ls-a(linux),dir(windows),ping 127.0.0.1。在linux中,使用/bin/bash-c执行命令,在windows中,使用powershell.exe执行命令。</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -218,6 +248,7 @@ func main() {
|
||||
```go
|
||||
func ExecCommand(command string) (stdout, stderr string, err error)
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
@@ -248,10 +279,8 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### <span id="GetOsBits">GetOsBits</span>
|
||||
|
||||
<p>获取当前操作系统位数,返回32或64</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -259,6 +288,7 @@ func main() {
|
||||
```go
|
||||
func GetOsBits() int
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
@@ -269,13 +299,6 @@ import (
|
||||
|
||||
func main() {
|
||||
osBit := system.GetOsBits()
|
||||
fmt.Println(osBit)
|
||||
fmt.Println(osBit) // 32 or 64
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+355
-220
File diff suppressed because it is too large
Load Diff
+355
-208
File diff suppressed because it is too large
Load Diff
+5
-2
@@ -1,4 +1,5 @@
|
||||
# Xerror
|
||||
|
||||
Package xerror implements helpers for errors.
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
@@ -10,6 +11,7 @@ Package xerror implements helpers for errors.
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Usage:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/duke-git/lancet/v2/xerror"
|
||||
@@ -19,15 +21,15 @@ import (
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Index
|
||||
|
||||
- [Unwrap](#Unwrap)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## Documentation
|
||||
|
||||
|
||||
|
||||
### <span id="Unwrap">Unwrap</span>
|
||||
|
||||
<p>Unwrap if err is nil then it returns a valid value. If err is not nil, Unwrap panics with err.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
@@ -35,6 +37,7 @@ import (
|
||||
```go
|
||||
func Unwrap[T any](val T, err error) T
|
||||
```
|
||||
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
# Xerror
|
||||
|
||||
xerror 错误处理逻辑封装
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
@@ -10,6 +11,7 @@ xerror错误处理逻辑封装
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## 用法:
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/duke-git/lancet/v2/xerror"
|
||||
@@ -19,15 +21,15 @@ import (
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## 目录
|
||||
|
||||
- [Unwrap](#Unwrap)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
## 文档
|
||||
|
||||
|
||||
|
||||
### <span id="Unwrap">Unwrap</span>
|
||||
|
||||
<p>检查error, 如果err为nil则展开,则它返回一个有效值,如果err不是nil则Unwrap使用err发生panic。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
@@ -35,6 +37,7 @@ import (
|
||||
```go
|
||||
func Unwrap[T any](val T, err error) T
|
||||
```
|
||||
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
|
||||
+226
@@ -13,12 +13,21 @@
|
||||
package netutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/duke-git/lancet/v2/slice"
|
||||
)
|
||||
|
||||
// HttpGet send get http request.
|
||||
@@ -78,3 +87,220 @@ func ConvertMapToQueryString(param map[string]any) string {
|
||||
}
|
||||
return build.String()
|
||||
}
|
||||
|
||||
// HttpRequest struct is a composed http request
|
||||
type HttpRequest struct {
|
||||
RawURL string
|
||||
Method string
|
||||
Headers http.Header
|
||||
QueryParams url.Values
|
||||
FormData url.Values
|
||||
Body []byte
|
||||
}
|
||||
|
||||
// HttpClientConfig contains some configurations for http client
|
||||
type HttpClientConfig struct {
|
||||
SSLEnabled bool
|
||||
TLSConfig *tls.Config
|
||||
Compressed bool
|
||||
HandshakeTimeout time.Duration
|
||||
ResponseTimeout time.Duration
|
||||
Verbose bool
|
||||
}
|
||||
|
||||
// defaultHttpClientConfig defalut client config
|
||||
var defaultHttpClientConfig = &HttpClientConfig{
|
||||
Compressed: false,
|
||||
HandshakeTimeout: 20 * time.Second,
|
||||
ResponseTimeout: 40 * time.Second,
|
||||
}
|
||||
|
||||
// HttpClient is used for sending http request
|
||||
type HttpClient struct {
|
||||
*http.Client
|
||||
TLS *tls.Config
|
||||
Request *http.Request
|
||||
Config HttpClientConfig
|
||||
}
|
||||
|
||||
// NewHttpClient make a HttpClient instance
|
||||
func NewHttpClient() *HttpClient {
|
||||
client := &HttpClient{
|
||||
Client: &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSHandshakeTimeout: defaultHttpClientConfig.HandshakeTimeout,
|
||||
ResponseHeaderTimeout: defaultHttpClientConfig.ResponseTimeout,
|
||||
DisableCompression: !defaultHttpClientConfig.Compressed,
|
||||
},
|
||||
},
|
||||
Config: *defaultHttpClientConfig,
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
// NewHttpClientWithConfig make a HttpClient instance with pass config
|
||||
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient {
|
||||
if config == nil {
|
||||
config = defaultHttpClientConfig
|
||||
}
|
||||
|
||||
client := &HttpClient{
|
||||
Client: &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSHandshakeTimeout: config.HandshakeTimeout,
|
||||
ResponseHeaderTimeout: config.ResponseTimeout,
|
||||
DisableCompression: !config.Compressed,
|
||||
},
|
||||
},
|
||||
Config: *config,
|
||||
}
|
||||
|
||||
if config.SSLEnabled {
|
||||
client.TLS = config.TLSConfig
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
// SendRequest send http request.
|
||||
// Play: https://go.dev/play/p/jUSgynekH7G
|
||||
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error) {
|
||||
err := validateRequest(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rawUrl := request.RawURL
|
||||
|
||||
req, err := http.NewRequest(request.Method, rawUrl, bytes.NewBuffer(request.Body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client.setTLS(rawUrl)
|
||||
client.setHeader(req, request.Headers)
|
||||
|
||||
err = client.setQueryParam(req, rawUrl, request.QueryParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if request.FormData != nil {
|
||||
client.setFormData(req, request.FormData)
|
||||
}
|
||||
|
||||
client.Request = req
|
||||
|
||||
resp, err := client.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// DecodeResponse decode response into target object.
|
||||
// Play: https://go.dev/play/p/jUSgynekH7G
|
||||
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error {
|
||||
if resp == nil {
|
||||
return errors.New("invalid target param")
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return json.NewDecoder(resp.Body).Decode(target)
|
||||
}
|
||||
|
||||
// setTLS set http client transport TLSClientConfig
|
||||
func (client *HttpClient) setTLS(rawUrl string) {
|
||||
if strings.HasPrefix(rawUrl, "https") {
|
||||
if transport, ok := client.Client.Transport.(*http.Transport); ok {
|
||||
transport.TLSClientConfig = client.TLS
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// setHeader set http rquest header
|
||||
func (client *HttpClient) setHeader(req *http.Request, headers http.Header) {
|
||||
if headers == nil {
|
||||
headers = make(http.Header)
|
||||
}
|
||||
|
||||
if _, ok := headers["Accept"]; !ok {
|
||||
headers["Accept"] = []string{"*/*"}
|
||||
}
|
||||
if _, ok := headers["Accept-Encoding"]; !ok && client.Config.Compressed {
|
||||
headers["Accept-Encoding"] = []string{"deflate, gzip"}
|
||||
}
|
||||
|
||||
req.Header = headers
|
||||
}
|
||||
|
||||
// setQueryParam set http request query string param
|
||||
func (client *HttpClient) setQueryParam(req *http.Request, reqUrl string, queryParam url.Values) error {
|
||||
if queryParam != nil {
|
||||
if !strings.Contains(reqUrl, "?") {
|
||||
reqUrl = reqUrl + "?" + queryParam.Encode()
|
||||
} else {
|
||||
reqUrl = reqUrl + "&" + queryParam.Encode()
|
||||
}
|
||||
u, err := url.Parse(reqUrl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.URL = u
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (client *HttpClient) setFormData(req *http.Request, values url.Values) {
|
||||
formData := []byte(values.Encode())
|
||||
req.Body = io.NopCloser(bytes.NewReader(formData))
|
||||
req.ContentLength = int64(len(formData))
|
||||
}
|
||||
|
||||
// validateRequest check if a request has url, and valid method.
|
||||
func validateRequest(req *HttpRequest) error {
|
||||
if req.RawURL == "" {
|
||||
return errors.New("invalid request url")
|
||||
}
|
||||
|
||||
// common HTTP methods
|
||||
methods := []string{"GET", "POST", "PUT", "DELETE", "PATCH",
|
||||
"HEAD", "CONNECT", "OPTIONS", "TRACE"}
|
||||
|
||||
if !slice.Contain(methods, strings.ToUpper(req.Method)) {
|
||||
return errors.New("invalid request method")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// StructToUrlValues convert struct to url valuse,
|
||||
// only convert the field which is exported and has `json` tag.
|
||||
// Play: https://go.dev/play/p/pFqMkM40w9z
|
||||
func StructToUrlValues(targetStruct any) url.Values {
|
||||
rv := reflect.ValueOf(targetStruct)
|
||||
rt := reflect.TypeOf(targetStruct)
|
||||
|
||||
if rt.Kind() == reflect.Ptr {
|
||||
rt = rt.Elem()
|
||||
}
|
||||
if rt.Kind() != reflect.Struct {
|
||||
panic(fmt.Errorf("data type %T not support, shuld be struct or pointer to struct", targetStruct))
|
||||
}
|
||||
|
||||
result := url.Values{}
|
||||
|
||||
fieldNum := rt.NumField()
|
||||
pattern := `^[A-Z]`
|
||||
regex := regexp.MustCompile(pattern)
|
||||
for i := 0; i < fieldNum; i++ {
|
||||
name := rt.Field(i).Name
|
||||
tag := rt.Field(i).Tag.Get("json")
|
||||
if regex.MatchString(name) && tag != "" {
|
||||
result.Add(tag, fmt.Sprintf("%v", rv.Field(i).Interface()))
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -1,235 +0,0 @@
|
||||
package netutil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/duke-git/lancet/v2/slice"
|
||||
)
|
||||
|
||||
// HttpRequest struct is a composed http request
|
||||
type HttpRequest struct {
|
||||
RawURL string
|
||||
Method string
|
||||
Headers http.Header
|
||||
QueryParams url.Values
|
||||
FormData url.Values
|
||||
Body []byte
|
||||
}
|
||||
|
||||
// HttpClientConfig contains some configurations for http client
|
||||
type HttpClientConfig struct {
|
||||
SSLEnabled bool
|
||||
TLSConfig *tls.Config
|
||||
Compressed bool
|
||||
HandshakeTimeout time.Duration
|
||||
ResponseTimeout time.Duration
|
||||
Verbose bool
|
||||
}
|
||||
|
||||
// defaultHttpClientConfig defalut client config
|
||||
var defaultHttpClientConfig = &HttpClientConfig{
|
||||
Compressed: false,
|
||||
HandshakeTimeout: 20 * time.Second,
|
||||
ResponseTimeout: 40 * time.Second,
|
||||
}
|
||||
|
||||
// HttpClient is used for sending http request
|
||||
type HttpClient struct {
|
||||
*http.Client
|
||||
TLS *tls.Config
|
||||
Request *http.Request
|
||||
Config HttpClientConfig
|
||||
}
|
||||
|
||||
// NewHttpClient make a HttpClient instance
|
||||
func NewHttpClient() *HttpClient {
|
||||
client := &HttpClient{
|
||||
Client: &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSHandshakeTimeout: defaultHttpClientConfig.HandshakeTimeout,
|
||||
ResponseHeaderTimeout: defaultHttpClientConfig.ResponseTimeout,
|
||||
DisableCompression: !defaultHttpClientConfig.Compressed,
|
||||
},
|
||||
},
|
||||
Config: *defaultHttpClientConfig,
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
// NewHttpClientWithConfig make a HttpClient instance with pass config
|
||||
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient {
|
||||
if config == nil {
|
||||
config = defaultHttpClientConfig
|
||||
}
|
||||
|
||||
client := &HttpClient{
|
||||
Client: &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSHandshakeTimeout: config.HandshakeTimeout,
|
||||
ResponseHeaderTimeout: config.ResponseTimeout,
|
||||
DisableCompression: !config.Compressed,
|
||||
},
|
||||
},
|
||||
Config: *config,
|
||||
}
|
||||
|
||||
if config.SSLEnabled {
|
||||
client.TLS = config.TLSConfig
|
||||
}
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
// SendRequest send http request.
|
||||
// Play: https://go.dev/play/p/jUSgynekH7G
|
||||
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error) {
|
||||
err := validateRequest(request)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rawUrl := request.RawURL
|
||||
|
||||
req, err := http.NewRequest(request.Method, rawUrl, bytes.NewBuffer(request.Body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client.setTLS(rawUrl)
|
||||
client.setHeader(req, request.Headers)
|
||||
|
||||
err = client.setQueryParam(req, rawUrl, request.QueryParams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if request.FormData != nil {
|
||||
client.setFormData(req, request.FormData)
|
||||
}
|
||||
|
||||
client.Request = req
|
||||
|
||||
resp, err := client.Client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// DecodeResponse decode response into target object.
|
||||
// Play: https://go.dev/play/p/jUSgynekH7G
|
||||
func (client *HttpClient) DecodeResponse(resp *http.Response, target any) error {
|
||||
if resp == nil {
|
||||
return errors.New("invalid target param")
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return json.NewDecoder(resp.Body).Decode(target)
|
||||
}
|
||||
|
||||
// setTLS set http client transport TLSClientConfig
|
||||
func (client *HttpClient) setTLS(rawUrl string) {
|
||||
if strings.HasPrefix(rawUrl, "https") {
|
||||
if transport, ok := client.Client.Transport.(*http.Transport); ok {
|
||||
transport.TLSClientConfig = client.TLS
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// setHeader set http rquest header
|
||||
func (client *HttpClient) setHeader(req *http.Request, headers http.Header) {
|
||||
if headers == nil {
|
||||
headers = make(http.Header)
|
||||
}
|
||||
|
||||
if _, ok := headers["Accept"]; !ok {
|
||||
headers["Accept"] = []string{"*/*"}
|
||||
}
|
||||
if _, ok := headers["Accept-Encoding"]; !ok && client.Config.Compressed {
|
||||
headers["Accept-Encoding"] = []string{"deflate, gzip"}
|
||||
}
|
||||
|
||||
req.Header = headers
|
||||
}
|
||||
|
||||
// setQueryParam set http request query string param
|
||||
func (client *HttpClient) setQueryParam(req *http.Request, reqUrl string, queryParam url.Values) error {
|
||||
if queryParam != nil {
|
||||
if !strings.Contains(reqUrl, "?") {
|
||||
reqUrl = reqUrl + "?" + queryParam.Encode()
|
||||
} else {
|
||||
reqUrl = reqUrl + "&" + queryParam.Encode()
|
||||
}
|
||||
u, err := url.Parse(reqUrl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.URL = u
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (client *HttpClient) setFormData(req *http.Request, values url.Values) {
|
||||
formData := []byte(values.Encode())
|
||||
req.Body = io.NopCloser(bytes.NewReader(formData))
|
||||
req.ContentLength = int64(len(formData))
|
||||
}
|
||||
|
||||
// validateRequest check if a request has url, and valid method.
|
||||
func validateRequest(req *HttpRequest) error {
|
||||
if req.RawURL == "" {
|
||||
return errors.New("invalid request url")
|
||||
}
|
||||
|
||||
// common HTTP methods
|
||||
methods := []string{"GET", "POST", "PUT", "DELETE", "PATCH",
|
||||
"HEAD", "CONNECT", "OPTIONS", "TRACE"}
|
||||
|
||||
if !slice.Contain(methods, strings.ToUpper(req.Method)) {
|
||||
return errors.New("invalid request method")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// StructToUrlValues convert struct to url valuse,
|
||||
// only convert the field which is exported and has `json` tag.
|
||||
// Play: https://go.dev/play/p/pFqMkM40w9z
|
||||
func StructToUrlValues(targetStruct any) url.Values {
|
||||
rv := reflect.ValueOf(targetStruct)
|
||||
rt := reflect.TypeOf(targetStruct)
|
||||
|
||||
if rt.Kind() == reflect.Ptr {
|
||||
rt = rt.Elem()
|
||||
}
|
||||
if rt.Kind() != reflect.Struct {
|
||||
panic(fmt.Errorf("data type %T not support, shuld be struct or pointer to struct", targetStruct))
|
||||
}
|
||||
|
||||
result := url.Values{}
|
||||
|
||||
fieldNum := rt.NumField()
|
||||
pattern := `^[A-Z]`
|
||||
regex := regexp.MustCompile(pattern)
|
||||
for i := 0; i < fieldNum; i++ {
|
||||
name := rt.Field(i).Name
|
||||
tag := rt.Field(i).Tag.Get("json")
|
||||
if regex.MatchString(name) && tag != "" {
|
||||
result.Add(tag, fmt.Sprintf("%v", rv.Field(i).Interface()))
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
package netutil
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/duke-git/lancet/v2/internal"
|
||||
)
|
||||
|
||||
func TestHttpClient_Get(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestHttpClient_Get")
|
||||
|
||||
request := &HttpRequest{
|
||||
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
|
||||
Method: "GET",
|
||||
}
|
||||
|
||||
httpClient := 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
|
||||
err = httpClient.DecodeResponse(resp, &todo)
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
|
||||
assert.Equal(1, todo.Id)
|
||||
}
|
||||
|
||||
func TestHttpClent_Post(t *testing.T) {
|
||||
header := http.Header{}
|
||||
header.Add("Content-Type", "multipart/form-data")
|
||||
|
||||
postData := url.Values{}
|
||||
postData.Add("userId", "1")
|
||||
postData.Add("title", "testItem")
|
||||
|
||||
request := &HttpRequest{
|
||||
RawURL: "https://jsonplaceholder.typicode.com/todos",
|
||||
Method: "POST",
|
||||
Headers: header,
|
||||
FormData: postData,
|
||||
}
|
||||
|
||||
httpClient := NewHttpClient()
|
||||
resp, err := httpClient.SendRequest(request)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
t.Log("response: ", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
func TestStructToUrlValues(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestStructToUrlValues")
|
||||
|
||||
type TodoQuery struct {
|
||||
Id int `json:"id"`
|
||||
UserId int `json:"userId"`
|
||||
}
|
||||
todoQuery := TodoQuery{
|
||||
Id: 1,
|
||||
UserId: 1,
|
||||
}
|
||||
todoValues := StructToUrlValues(todoQuery)
|
||||
|
||||
assert.Equal("1", todoValues.Get("id"))
|
||||
assert.Equal("1", todoValues.Get("userId"))
|
||||
|
||||
request := &HttpRequest{
|
||||
RawURL: "https://jsonplaceholder.typicode.com/todos",
|
||||
Method: "GET",
|
||||
QueryParams: todoValues,
|
||||
}
|
||||
|
||||
httpClient := NewHttpClient()
|
||||
resp, err := httpClient.SendRequest(request)
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
t.Log("response: ", string(body))
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
package netutil
|
||||
|
||||
import "fmt"
|
||||
|
||||
func ExampleHttpClient_SendRequest() {
|
||||
request := &HttpRequest{
|
||||
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
|
||||
Method: "GET",
|
||||
}
|
||||
|
||||
httpClient := NewHttpClient()
|
||||
resp, err := httpClient.SendRequest(request)
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
return
|
||||
}
|
||||
|
||||
type Todo struct {
|
||||
UserId int `json:"userId"`
|
||||
Id int `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Completed bool `json:"completed"`
|
||||
}
|
||||
|
||||
var todo Todo
|
||||
err = httpClient.DecodeResponse(resp, &todo)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(todo.Id)
|
||||
|
||||
// Output:
|
||||
// 1
|
||||
}
|
||||
|
||||
func ExampleHttpClient_DecodeResponse() {
|
||||
request := &HttpRequest{
|
||||
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
|
||||
Method: "GET",
|
||||
}
|
||||
|
||||
httpClient := NewHttpClient()
|
||||
resp, err := httpClient.SendRequest(request)
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
return
|
||||
}
|
||||
|
||||
type Todo struct {
|
||||
UserId int `json:"userId"`
|
||||
Id int `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Completed bool `json:"completed"`
|
||||
}
|
||||
|
||||
var todo Todo
|
||||
err = httpClient.DecodeResponse(resp, &todo)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(todo.Id)
|
||||
|
||||
// Output:
|
||||
// 1
|
||||
}
|
||||
|
||||
func ExampleStructToUrlValues() {
|
||||
type TodoQuery struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
todoQuery := TodoQuery{
|
||||
Id: 1,
|
||||
Name: "Test",
|
||||
}
|
||||
todoValues := StructToUrlValues(todoQuery)
|
||||
|
||||
fmt.Println(todoValues.Get("id"))
|
||||
fmt.Println(todoValues.Get("name"))
|
||||
|
||||
// Output:
|
||||
// 1
|
||||
// Test
|
||||
}
|
||||
|
||||
func ExampleConvertMapToQueryString() {
|
||||
var m = map[string]any{
|
||||
"c": 3,
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
}
|
||||
|
||||
qs := ConvertMapToQueryString(m)
|
||||
|
||||
fmt.Println(qs)
|
||||
|
||||
// Output:
|
||||
// a=1&b=2&c=3
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
@@ -156,3 +157,90 @@ func TestParseResponse(t *testing.T) {
|
||||
}
|
||||
t.Log("response: ", toDoResp)
|
||||
}
|
||||
|
||||
func TestHttpClient_Get(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestHttpClient_Get")
|
||||
|
||||
request := &HttpRequest{
|
||||
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
|
||||
Method: "GET",
|
||||
}
|
||||
|
||||
httpClient := 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
|
||||
err = httpClient.DecodeResponse(resp, &todo)
|
||||
if err != nil {
|
||||
t.Log(err)
|
||||
}
|
||||
|
||||
assert.Equal(1, todo.Id)
|
||||
}
|
||||
|
||||
func TestHttpClent_Post(t *testing.T) {
|
||||
header := http.Header{}
|
||||
header.Add("Content-Type", "multipart/form-data")
|
||||
|
||||
postData := url.Values{}
|
||||
postData.Add("userId", "1")
|
||||
postData.Add("title", "testItem")
|
||||
|
||||
request := &HttpRequest{
|
||||
RawURL: "https://jsonplaceholder.typicode.com/todos",
|
||||
Method: "POST",
|
||||
Headers: header,
|
||||
FormData: postData,
|
||||
}
|
||||
|
||||
httpClient := NewHttpClient()
|
||||
resp, err := httpClient.SendRequest(request)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
t.Log("response: ", resp.StatusCode, string(body))
|
||||
}
|
||||
|
||||
func TestStructToUrlValues(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestStructToUrlValues")
|
||||
|
||||
type TodoQuery struct {
|
||||
Id int `json:"id"`
|
||||
UserId int `json:"userId"`
|
||||
}
|
||||
todoQuery := TodoQuery{
|
||||
Id: 1,
|
||||
UserId: 1,
|
||||
}
|
||||
todoValues := StructToUrlValues(todoQuery)
|
||||
|
||||
assert.Equal("1", todoValues.Get("id"))
|
||||
assert.Equal("1", todoValues.Get("userId"))
|
||||
|
||||
request := &HttpRequest{
|
||||
RawURL: "https://jsonplaceholder.typicode.com/todos",
|
||||
Method: "GET",
|
||||
QueryParams: todoValues,
|
||||
}
|
||||
|
||||
httpClient := NewHttpClient()
|
||||
resp, err := httpClient.SendRequest(request)
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
t.Log("response: ", string(body))
|
||||
}
|
||||
|
||||
@@ -89,3 +89,68 @@ func ExampleEncodeUrl() {
|
||||
// Output:
|
||||
// http://www.lancet.com?a=1&b=%5B2%5D
|
||||
}
|
||||
|
||||
func ExampleHttpClient_DecodeResponse() {
|
||||
request := &HttpRequest{
|
||||
RawURL: "https://jsonplaceholder.typicode.com/todos/1",
|
||||
Method: "GET",
|
||||
}
|
||||
|
||||
httpClient := NewHttpClient()
|
||||
resp, err := httpClient.SendRequest(request)
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
return
|
||||
}
|
||||
|
||||
type Todo struct {
|
||||
UserId int `json:"userId"`
|
||||
Id int `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Completed bool `json:"completed"`
|
||||
}
|
||||
|
||||
var todo Todo
|
||||
err = httpClient.DecodeResponse(resp, &todo)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(todo.Id)
|
||||
|
||||
// Output:
|
||||
// 1
|
||||
}
|
||||
|
||||
func ExampleStructToUrlValues() {
|
||||
type TodoQuery struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
todoQuery := TodoQuery{
|
||||
Id: 1,
|
||||
Name: "Test",
|
||||
}
|
||||
todoValues := StructToUrlValues(todoQuery)
|
||||
|
||||
fmt.Println(todoValues.Get("id"))
|
||||
fmt.Println(todoValues.Get("name"))
|
||||
|
||||
// Output:
|
||||
// 1
|
||||
// Test
|
||||
}
|
||||
|
||||
func ExampleConvertMapToQueryString() {
|
||||
var m = map[string]any{
|
||||
"c": 3,
|
||||
"a": 1,
|
||||
"b": 2,
|
||||
}
|
||||
|
||||
qs := ConvertMapToQueryString(m)
|
||||
|
||||
fmt.Println(qs)
|
||||
|
||||
// Output:
|
||||
// a=1&b=2&c=3
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
)
|
||||
|
||||
func ExampleRandInt() {
|
||||
|
||||
result := RandInt(1, 10)
|
||||
|
||||
if result >= 1 && result < 10 {
|
||||
|
||||
@@ -3,10 +3,10 @@ package system
|
||||
import "fmt"
|
||||
|
||||
func ExampleSetOsEnv() {
|
||||
ok := SetOsEnv("foo", "abc")
|
||||
err := SetOsEnv("foo", "abc")
|
||||
result := GetOsEnv("foo")
|
||||
|
||||
fmt.Println(ok)
|
||||
fmt.Println(err)
|
||||
fmt.Println(result)
|
||||
// Output:
|
||||
// <nil>
|
||||
@@ -25,14 +25,14 @@ func ExampleGetOsEnv() {
|
||||
}
|
||||
|
||||
func ExampleRemoveOsEnv() {
|
||||
ok1 := SetOsEnv("foo", "abc")
|
||||
err1 := SetOsEnv("foo", "abc")
|
||||
result1 := GetOsEnv("foo")
|
||||
|
||||
ok2 := RemoveOsEnv("foo")
|
||||
err2 := RemoveOsEnv("foo")
|
||||
result2 := GetOsEnv("foo")
|
||||
|
||||
fmt.Println(ok1)
|
||||
fmt.Println(ok2)
|
||||
fmt.Println(err1)
|
||||
fmt.Println(err2)
|
||||
fmt.Println(result1)
|
||||
fmt.Println(result2)
|
||||
|
||||
@@ -49,9 +49,10 @@ func ExampleCompareOsEnv() {
|
||||
return
|
||||
}
|
||||
|
||||
result1 := CompareOsEnv("foo", "abc")
|
||||
result := CompareOsEnv("foo", "abc")
|
||||
|
||||
fmt.Println(result)
|
||||
|
||||
fmt.Println(result1)
|
||||
// Output:
|
||||
// true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user