mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-08 06:32:28 +08:00
Compare commits
36 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
39d373d37b | ||
|
|
1aefd6aa12 | ||
|
|
c7aa44b8a4 | ||
|
|
0e3dc68de5 | ||
|
|
4083e75ed4 | ||
|
|
1327eff62f | ||
|
|
eb24c37143 | ||
|
|
b7a6c91064 | ||
|
|
555e185871 | ||
|
|
cb0efc5cc7 | ||
|
|
7ac6816532 | ||
|
|
81c4216699 | ||
|
|
2e30389703 | ||
|
|
0e1d5cf04b | ||
|
|
4e2d84e4fc | ||
|
|
13ee045b99 | ||
|
|
f1fd4c876b | ||
|
|
cd63b2b18f | ||
|
|
bab9b4a6b3 | ||
|
|
b3c34578bf | ||
|
|
f26477904e | ||
|
|
85886cf618 | ||
|
|
a19a861552 | ||
|
|
96d57a6d24 | ||
|
|
b48155c249 | ||
|
|
41685022c0 | ||
|
|
4e7274ce05 | ||
|
|
2384b0e51f | ||
|
|
6b6b938d1e | ||
|
|
29d8987909 | ||
|
|
43fb3f1a06 | ||
|
|
34c28fece3 | ||
|
|
40c2402493 | ||
|
|
c965e79bfc | ||
|
|
eeadb1fecb | ||
|
|
1dd826f050 |
78
README.md
78
README.md
@@ -1,24 +1,30 @@
|
|||||||
# Lancet
|
<div align=center>
|
||||||
<p style="font-size: 18px">
|
<img src="./logo.png" width="200" height="200"/>
|
||||||
Lancet is a comprehensive, efficient, and reusable util function library of go. Inspired by the java apache common package and lodash.js.
|
|
||||||
</p>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
[](https://codecov.io/gh/duke-git/lancet)
|
[](https://codecov.io/gh/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
<p style="font-size: 20px">
|
||||||
|
Lancet is a comprehensive, efficient, and reusable util function library of go. Inspired by the java apache common package and lodash.js.
|
||||||
|
</p>
|
||||||
|
|
||||||
English | [简体中文](./README_zh-CN.md)
|
English | [简体中文](./README_zh-CN.md)
|
||||||
|
|
||||||
|
|
||||||
## Feature
|
## Feature
|
||||||
|
|
||||||
- 👏 Comprehensive, efficient and reusable.
|
- 👏 Comprehensive, efficient and reusable.
|
||||||
- 💪 160+ common go util functions, support string, slice, datetime, net, crypt...
|
- 💪 180+ go util functions, support string, slice, datetime, net, crypt...
|
||||||
- 💅 Only depend on the go standard library.
|
- 💅 Only depend on the go standard library.
|
||||||
- 🌍 Unit test for every exported function.
|
- 🌍 Unit test for every exported function.
|
||||||
|
|
||||||
@@ -122,6 +128,18 @@ import "github.com/duke-git/lancet/datetime"
|
|||||||
- [AddDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddDay)
|
- [AddDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddDay)
|
||||||
- [AddHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddHour)
|
- [AddHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddHour)
|
||||||
- [AddMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddMinute)
|
- [AddMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddMinute)
|
||||||
|
- [BeginOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMinute)
|
||||||
|
- [BeginOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfHour)
|
||||||
|
- [BeginOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfDay)
|
||||||
|
- [BeginOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfWeek)
|
||||||
|
- [BeginOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMonth)
|
||||||
|
- [BeginOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfYear)
|
||||||
|
- [EndOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMinute)
|
||||||
|
- [EndOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfHour)
|
||||||
|
- [EndOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfDay)
|
||||||
|
- [EndOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfWeek)
|
||||||
|
- [EndOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMonth)
|
||||||
|
- [EndOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfYear)
|
||||||
- [GetNowDate](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDate)
|
- [GetNowDate](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDate)
|
||||||
- [GetNowTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowTime)
|
- [GetNowTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowTime)
|
||||||
- [GetNowDateTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDateTime)
|
- [GetNowDateTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDateTime)
|
||||||
@@ -129,6 +147,14 @@ import "github.com/duke-git/lancet/datetime"
|
|||||||
- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNightTimestamp)
|
- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNightTimestamp)
|
||||||
- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatTimeToStr)
|
- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatTimeToStr)
|
||||||
- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatStrToTime)
|
- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatStrToTime)
|
||||||
|
- [NewUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewUnix)
|
||||||
|
- [NewUnixNow](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewUnixNow)
|
||||||
|
- [NewFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewFormat)
|
||||||
|
- [NewISO8601](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewISO8601)
|
||||||
|
- [ToUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToUnix)
|
||||||
|
- [ToFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormat)
|
||||||
|
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormatForTpl)
|
||||||
|
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToIso8601)
|
||||||
|
|
||||||
### Fileutil package implements some basic functions for file operations.
|
### Fileutil package implements some basic functions for file operations.
|
||||||
|
|
||||||
@@ -156,9 +182,7 @@ import "github.com/duke-git/lancet/fileutil"
|
|||||||
### Formatter contains some functions for data formatting.
|
### Formatter contains some functions for data formatting.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import "github.com/duke-git/lancet/formatter"
|
||||||
"github.com/duke-git/lancet/formatter"
|
|
||||||
)
|
|
||||||
```
|
```
|
||||||
#### Function list:
|
#### Function list:
|
||||||
- [Comma](https://github.com/duke-git/lancet/blob/main/docs/formatter.md#Comma)
|
- [Comma](https://github.com/duke-git/lancet/blob/main/docs/formatter.md#Comma)
|
||||||
@@ -176,9 +200,25 @@ import "github.com/duke-git/lancet/function"
|
|||||||
- [Compose](https://github.com/duke-git/lancet/blob/main/docs/function.md#Compose)
|
- [Compose](https://github.com/duke-git/lancet/blob/main/docs/function.md#Compose)
|
||||||
- [Debounced](https://github.com/duke-git/lancet/blob/main/docs/function.md#Debounced)
|
- [Debounced](https://github.com/duke-git/lancet/blob/main/docs/function.md#Debounced)
|
||||||
- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function.md#Delay)
|
- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function.md#Delay)
|
||||||
- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function.md#Delay)
|
|
||||||
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher)
|
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher)
|
||||||
|
|
||||||
|
|
||||||
|
### Mathutil package implements some functions for math calculation.
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "github.com/duke-git/lancet/mathutil"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Function list:
|
||||||
|
- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Exponent)
|
||||||
|
- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Fibonacci)
|
||||||
|
- [Factorial](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Factorial)
|
||||||
|
- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Percent)
|
||||||
|
- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToFloat)
|
||||||
|
- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToString)
|
||||||
|
- [TruncRound](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#TruncRound)
|
||||||
|
|
||||||
|
|
||||||
### Netutil package contains functions to get net information and send http request.
|
### Netutil package contains functions to get net information and send http request.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -188,6 +228,8 @@ import "github.com/duke-git/lancet/netutil"
|
|||||||
#### Function list:
|
#### Function list:
|
||||||
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ConvertMapToQueryString)
|
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ConvertMapToQueryString)
|
||||||
- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetInternalIp)
|
- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetInternalIp)
|
||||||
|
- [GetIps](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetIps)
|
||||||
|
- [GetMacAddrs](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetMacAddrs)
|
||||||
- [GetPublicIpInfo](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetPublicIpInfo)
|
- [GetPublicIpInfo](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetPublicIpInfo)
|
||||||
- [IsPublicIP](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsPublicIP)
|
- [IsPublicIP](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsPublicIP)
|
||||||
- [HttpGet](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpGet)
|
- [HttpGet](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpGet)
|
||||||
@@ -207,6 +249,7 @@ import "github.com/duke-git/lancet/random"
|
|||||||
- [RandBytes](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandBytes)
|
- [RandBytes](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandBytes)
|
||||||
- [RandInt](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandInt)
|
- [RandInt](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandInt)
|
||||||
- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandString)
|
- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandString)
|
||||||
|
- [UUIdV4](https://github.com/duke-git/lancet/blob/main/docs/random.md#UUIdV4)
|
||||||
|
|
||||||
### Retry package is for executing a function repeatedly until it was successful or canceled by the context.
|
### Retry package is for executing a function repeatedly until it was successful or canceled by the context.
|
||||||
|
|
||||||
@@ -334,4 +377,15 @@ import "github.com/duke-git/lancet/validator"
|
|||||||
- [IsIpV6](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsIpV6)
|
- [IsIpV6](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsIpV6)
|
||||||
- [IsStrongPassword](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsStrongPassword)
|
- [IsStrongPassword](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsStrongPassword)
|
||||||
- [IsUrl](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsUrl)
|
- [IsUrl](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsUrl)
|
||||||
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsWeakPassword)
|
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsWeakPassword)
|
||||||
|
|
||||||
|
|
||||||
|
## How to Contribute
|
||||||
|
|
||||||
|
I really appreciate any code commits which make lancet lib powerful. Please follow the rules below to create your pull request.
|
||||||
|
|
||||||
|
1. Fork the repository.
|
||||||
|
2. Create your feature branch.
|
||||||
|
3. Commit your changes.
|
||||||
|
4. Push to the branch
|
||||||
|
5. Create new pull request.
|
||||||
@@ -1,16 +1,22 @@
|
|||||||
# Lancet
|
<div align=center>
|
||||||
<p style="font-size: 18px">
|
<img src="./logo.png" width="200" height="200"/>
|
||||||
lancet(柳叶刀)是一个全面、高效、可复用的go语言工具函数库。 lancet受到了java apache common包和lodash.js的启发。
|
|
||||||
</p>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
||||||
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
[](https://goreportcard.com/report/github.com/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
[](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
|
||||||
[](https://codecov.io/gh/duke-git/lancet)
|
[](https://codecov.io/gh/duke-git/lancet)
|
||||||
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
[](https://github.com/duke-git/lancet/blob/main/LICENSE)
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
<p style="font-size: 18px">
|
||||||
|
lancet(柳叶刀)是一个全面、高效、可复用的go语言工具函数库。 lancet受到了java apache common包和lodash.js的启发。
|
||||||
|
</p>
|
||||||
|
|
||||||
简体中文 | [English](./README.md)
|
简体中文 | [English](./README.md)
|
||||||
|
|
||||||
@@ -18,7 +24,7 @@
|
|||||||
## 特性
|
## 特性
|
||||||
|
|
||||||
- 👏 全面、高效、可复用
|
- 👏 全面、高效、可复用
|
||||||
- 💪 170+常用go工具函数,支持string、slice、datetime、net、crypt...
|
- 💪 200+常用go工具函数,支持string、slice、datetime、net、crypt...
|
||||||
- 💅 只依赖go标准库
|
- 💅 只依赖go标准库
|
||||||
- 🌍 所有导出函数单元测试覆盖率100%
|
- 🌍 所有导出函数单元测试覆盖率100%
|
||||||
|
|
||||||
@@ -122,6 +128,18 @@ import "github.com/duke-git/lancet/datetime"
|
|||||||
- [AddDay](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddDay)
|
- [AddDay](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddDay)
|
||||||
- [AddHour](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddHour)
|
- [AddHour](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddHour)
|
||||||
- [AddMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddMinute)
|
- [AddMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddMinute)
|
||||||
|
- [BeginOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMinute)
|
||||||
|
- [BeginOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfHour)
|
||||||
|
- [BeginOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfDay)
|
||||||
|
- [BeginOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfWeek)
|
||||||
|
- [BeginOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMonth)
|
||||||
|
- [BeginOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfYear)
|
||||||
|
- [EndOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMinute)
|
||||||
|
- [EndOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfHour)
|
||||||
|
- [EndOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfDay)
|
||||||
|
- [EndOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfWeek)
|
||||||
|
- [EndOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMonth)
|
||||||
|
- [EndOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfYear)
|
||||||
- [GetNowDate](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDate)
|
- [GetNowDate](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDate)
|
||||||
- [GetNowTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowTime)
|
- [GetNowTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowTime)
|
||||||
- [GetNowDateTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDateTime)
|
- [GetNowDateTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDateTime)
|
||||||
@@ -129,7 +147,14 @@ import "github.com/duke-git/lancet/datetime"
|
|||||||
- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNightTimestamp)
|
- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNightTimestamp)
|
||||||
- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatTimeToStr)
|
- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatTimeToStr)
|
||||||
- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatStrToTime)
|
- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatStrToTime)
|
||||||
|
- [NewUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnix)
|
||||||
|
- [NewUnixNow](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnixNow)
|
||||||
|
- [NewFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewFormat)
|
||||||
|
- [NewISO8601](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewISO8601)
|
||||||
|
- [ToUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToUnix)
|
||||||
|
- [ToFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormat)
|
||||||
|
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormatForTpl)
|
||||||
|
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToIso8601)
|
||||||
### fileutil包支持文件基本操作。
|
### fileutil包支持文件基本操作。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -156,9 +181,7 @@ import "github.com/duke-git/lancet/fileutil"
|
|||||||
### formatter格式化器包含一些数据格式化处理方法。
|
### formatter格式化器包含一些数据格式化处理方法。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import "github.com/duke-git/lancet/formatter"
|
||||||
"github.com/duke-git/lancet/formatter"
|
|
||||||
)
|
|
||||||
```
|
```
|
||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
- [Comma](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#Comma)
|
- [Comma](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#Comma)
|
||||||
@@ -177,9 +200,22 @@ import "github.com/duke-git/lancet/function"
|
|||||||
- [Compose](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Compose)
|
- [Compose](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Compose)
|
||||||
- [Debounced](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Debounced)
|
- [Debounced](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Debounced)
|
||||||
- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay)
|
- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay)
|
||||||
- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay)
|
|
||||||
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)
|
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)
|
||||||
|
|
||||||
|
### mathutil包实现了一些数学计算的函数。
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "github.com/duke-git/lancet/mathutil"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Function list:
|
||||||
|
- [Exponent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Exponent)
|
||||||
|
- [Fibonacci](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Fibonacci)
|
||||||
|
- [Factorial](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Factorial)
|
||||||
|
- [Percent](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Percent)
|
||||||
|
- [RoundToFloat](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToFloat)
|
||||||
|
- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToString)
|
||||||
|
- [TruncRound](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#TruncRound)
|
||||||
|
|
||||||
### netutil网络包支持获取ip地址,发送http请求。
|
### netutil网络包支持获取ip地址,发送http请求。
|
||||||
|
|
||||||
@@ -190,6 +226,8 @@ import "github.com/duke-git/lancet/netutil"
|
|||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ConvertMapToQueryString)
|
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ConvertMapToQueryString)
|
||||||
- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetInternalIp)
|
- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetInternalIp)
|
||||||
|
- [GetIps](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetIps)
|
||||||
|
- [GetMacAddrs](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetMacAddrs)
|
||||||
- [GetPublicIpInfo](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetPublicIpInfo)
|
- [GetPublicIpInfo](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetPublicIpInfo)
|
||||||
- [IsPublicIP](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsPublicIP)
|
- [IsPublicIP](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsPublicIP)
|
||||||
- [HttpGet](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpGet)
|
- [HttpGet](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpGet)
|
||||||
@@ -209,7 +247,7 @@ import "github.com/duke-git/lancet/random"
|
|||||||
- [RandBytes](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandBytes)
|
- [RandBytes](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandBytes)
|
||||||
- [RandInt](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandInt)
|
- [RandInt](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandInt)
|
||||||
- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandString)
|
- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandString)
|
||||||
|
- [UUIdV4](https://github.com/duke-git/lancet/blob/main/docs/random.md#UUIdV4)
|
||||||
### retry重试执行函数直到函数运行成功或被context cancel。
|
### retry重试执行函数直到函数运行成功或被context cancel。
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -339,4 +377,15 @@ import "github.com/duke-git/lancet/validator"
|
|||||||
- [IsIpV6](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsIpV6)
|
- [IsIpV6](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsIpV6)
|
||||||
- [IsStrongPassword](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsStrongPassword)
|
- [IsStrongPassword](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsStrongPassword)
|
||||||
- [IsUrl](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsUrl)
|
- [IsUrl](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsUrl)
|
||||||
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsWeakPassword)
|
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsWeakPassword)
|
||||||
|
|
||||||
|
|
||||||
|
## 如何贡献代码
|
||||||
|
|
||||||
|
非常感激任何的代码提交以使lancet的功能越来越强大。创建pull request时请遵守以下规则。
|
||||||
|
|
||||||
|
1. Fork lancet仓库。
|
||||||
|
2. 创建自己的特性分支。
|
||||||
|
3. 提交变更。
|
||||||
|
4. Push分支。
|
||||||
|
5. 创建新的pull request。
|
||||||
@@ -6,9 +6,10 @@ package convertor
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/gob"
|
"encoding/binary"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -21,14 +22,44 @@ func ToBool(s string) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ToBytes convert interface to bytes
|
// ToBytes convert interface to bytes
|
||||||
func ToBytes(data interface{}) ([]byte, error) {
|
func ToBytes(value interface{}) ([]byte, error) {
|
||||||
var buf bytes.Buffer
|
v := reflect.ValueOf(value)
|
||||||
enc := gob.NewEncoder(&buf)
|
|
||||||
err := enc.Encode(data)
|
switch value.(type) {
|
||||||
if err != nil {
|
case int, int8, int16, int32, int64:
|
||||||
return nil, err
|
number := v.Int()
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
buf.Reset()
|
||||||
|
err := binary.Write(buf, binary.BigEndian, number)
|
||||||
|
return buf.Bytes(), err
|
||||||
|
case uint, uint8, uint16, uint32, uint64:
|
||||||
|
number := v.Uint()
|
||||||
|
buf := bytes.NewBuffer([]byte{})
|
||||||
|
buf.Reset()
|
||||||
|
err := binary.Write(buf, binary.BigEndian, number)
|
||||||
|
return buf.Bytes(), err
|
||||||
|
case float32:
|
||||||
|
number := float32(v.Float())
|
||||||
|
bits := math.Float32bits(number)
|
||||||
|
bytes := make([]byte, 4)
|
||||||
|
binary.BigEndian.PutUint32(bytes, bits)
|
||||||
|
return bytes, nil
|
||||||
|
case float64:
|
||||||
|
number := v.Float()
|
||||||
|
bits := math.Float64bits(number)
|
||||||
|
bytes := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(bytes, bits)
|
||||||
|
return bytes, nil
|
||||||
|
case bool:
|
||||||
|
return strconv.AppendBool([]byte{}, v.Bool()), nil
|
||||||
|
case string:
|
||||||
|
return []byte(v.String()), nil
|
||||||
|
case []byte:
|
||||||
|
return v.Bytes(), nil
|
||||||
|
default:
|
||||||
|
newValue, err := json.Marshal(value)
|
||||||
|
return newValue, err
|
||||||
}
|
}
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToChar convert string to char slice
|
// ToChar convert string to char slice
|
||||||
|
|||||||
@@ -42,14 +42,21 @@ func TestToBytes(t *testing.T) {
|
|||||||
"1",
|
"1",
|
||||||
}
|
}
|
||||||
expected := [][]byte{
|
expected := [][]byte{
|
||||||
{3, 4, 0, 0},
|
{0, 0, 0, 0, 0, 0, 0, 0},
|
||||||
{3, 2, 0, 0},
|
{102, 97, 108, 115, 101},
|
||||||
{4, 12, 0, 1, 49},
|
{49},
|
||||||
}
|
}
|
||||||
for i := 0; i < len(cases); i++ {
|
for i := 0; i < len(cases); i++ {
|
||||||
actual, _ := ToBytes(cases[i])
|
actual, _ := ToBytes(cases[i])
|
||||||
assert.Equal(expected[i], actual)
|
assert.Equal(expected[i], actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bytesData, err := ToBytes("abc")
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
assert.Equal("abc", ToString(bytesData))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestToInt(t *testing.T) {
|
func TestToInt(t *testing.T) {
|
||||||
|
|||||||
60
datetime/conversion.go
Normal file
60
datetime/conversion.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
|
||||||
|
// Use of this source code is governed by MIT license.
|
||||||
|
|
||||||
|
package datetime
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewUnixNow return unix timestamp of current time
|
||||||
|
func NewUnixNow() *theTime {
|
||||||
|
return &theTime{unix: time.Now().Unix()}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewUnix return unix timestamp of specified time
|
||||||
|
func NewUnix(unix int64) *theTime {
|
||||||
|
return &theTime{unix: unix}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFormat return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss"
|
||||||
|
func NewFormat(t string) (*theTime, error) {
|
||||||
|
timeLayout := "2006-01-02 15:04:05"
|
||||||
|
loc := time.FixedZone("CST", 8*3600)
|
||||||
|
tt, err := time.ParseInLocation(timeLayout, t, loc)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &theTime{unix: tt.Unix()}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewISO8601 return unix timestamp of specified iso8601 time string
|
||||||
|
func NewISO8601(iso8601 string) (*theTime, error) {
|
||||||
|
t, err := time.ParseInLocation(time.RFC3339, iso8601, time.UTC)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &theTime{unix: t.Unix()}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToUnix return unix timestamp
|
||||||
|
func (t *theTime) ToUnix() int64 {
|
||||||
|
return t.unix
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToFormat return the time string 'yyyy-mm-dd hh:mm:ss' of unix time
|
||||||
|
func (t *theTime) ToFormat() string {
|
||||||
|
return time.Unix(t.unix, 0).Format("2006-01-02 15:04:05")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToFormatForTpl return the time string which format is specified tpl
|
||||||
|
func (t *theTime) ToFormatForTpl(tpl string) string {
|
||||||
|
return time.Unix(t.unix, 0).Format(tpl)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToFormatForTpl return iso8601 time string
|
||||||
|
func (t *theTime) ToIso8601() string {
|
||||||
|
return time.Unix(t.unix, 0).Format(time.RFC3339)
|
||||||
|
}
|
||||||
54
datetime/conversion_test.go
Normal file
54
datetime/conversion_test.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package datetime
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestToUnix(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestToUnix")
|
||||||
|
|
||||||
|
tm1 := NewUnixNow()
|
||||||
|
unixTimestamp := tm1.ToUnix()
|
||||||
|
tm2 := NewUnix(unixTimestamp)
|
||||||
|
|
||||||
|
assert.Equal(tm1, tm2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToFormat(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestToFormat")
|
||||||
|
|
||||||
|
tm, err := NewFormat("2022/03/18 17:04:05")
|
||||||
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
|
tm, err = NewFormat("2022-03-18 17:04:05")
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
t.Log("ToFormat -> ", tm.ToFormat())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToFormatForTpl(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestToFormatForTpl")
|
||||||
|
|
||||||
|
tm, err := NewFormat("2022/03/18 17:04:05")
|
||||||
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
|
tm, err = NewFormat("2022-03-18 17:04:05")
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
t.Log("ToFormatForTpl -> ", tm.ToFormatForTpl("2006/01/02 15:04:05"))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToIso8601(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestToIso8601")
|
||||||
|
|
||||||
|
tm, err := NewISO8601("2022-03-18 17:04:05")
|
||||||
|
assert.IsNotNil(err)
|
||||||
|
|
||||||
|
tm, err = NewISO8601("2006-01-02T15:04:05.999Z")
|
||||||
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
t.Log("ToIso8601 -> ", tm.ToIso8601())
|
||||||
|
}
|
||||||
@@ -110,3 +110,73 @@ func FormatStrToTime(str, format string) (time.Time, error) {
|
|||||||
|
|
||||||
return time.Parse(v, str)
|
return time.Parse(v, str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BeginOfMinute return beginning minute time of day
|
||||||
|
func BeginOfMinute(t time.Time) time.Time {
|
||||||
|
y, m, d := t.Date()
|
||||||
|
return time.Date(y, m, d, t.Hour(), t.Minute(), 0, 0, t.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
// EndOfMinute return end minute time of day
|
||||||
|
func EndOfMinute(t time.Time) time.Time {
|
||||||
|
y, m, d := t.Date()
|
||||||
|
return time.Date(y, m, d, t.Hour(), t.Minute(), 59, int(time.Second-time.Nanosecond), t.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
// BeginOfHour return beginning hour time of day
|
||||||
|
func BeginOfHour(t time.Time) time.Time {
|
||||||
|
y, m, d := t.Date()
|
||||||
|
return time.Date(y, m, d, t.Hour(), 0, 0, 0, t.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
// EndOfHour return end hour time of day
|
||||||
|
func EndOfHour(t time.Time) time.Time {
|
||||||
|
y, m, d := t.Date()
|
||||||
|
return time.Date(y, m, d, t.Hour(), 59, 59, int(time.Second-time.Nanosecond), t.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
// BeginOfDay return beginning hour time of day
|
||||||
|
func BeginOfDay(t time.Time) time.Time {
|
||||||
|
y, m, d := t.Date()
|
||||||
|
return time.Date(y, m, d, 0, 0, 0, 0, t.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
// EndOfDay return end time of day
|
||||||
|
func EndOfDay(t time.Time) time.Time {
|
||||||
|
y, m, d := t.Date()
|
||||||
|
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
// BeginOfWeek return beginning week, week begin from Sunday
|
||||||
|
func BeginOfWeek(t time.Time) time.Time {
|
||||||
|
y, m, d := t.AddDate(0, 0, 0-int(BeginOfDay(t).Weekday())).Date()
|
||||||
|
return time.Date(y, m, d, 0, 0, 0, 0, t.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
// EndOfWeek return end week time, week end with Saturday
|
||||||
|
func EndOfWeek(t time.Time) time.Time {
|
||||||
|
y, m, d := BeginOfWeek(t).AddDate(0, 0, 7).Add(-time.Nanosecond).Date()
|
||||||
|
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
// BeginOfMonth return beginning of month
|
||||||
|
func BeginOfMonth(t time.Time) time.Time {
|
||||||
|
y, m, _ := t.Date()
|
||||||
|
return time.Date(y, m, 1, 0, 0, 0, 0, t.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
// EndOfMonth return end of month
|
||||||
|
func EndOfMonth(t time.Time) time.Time {
|
||||||
|
return BeginOfMonth(t).AddDate(0, 1, 0).Add(-time.Nanosecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BeginOfYear return beginning of year
|
||||||
|
func BeginOfYear(t time.Time) time.Time {
|
||||||
|
y, _, _ := t.Date()
|
||||||
|
return time.Date(y, time.January, 1, 0, 0, 0, 0, t.Location())
|
||||||
|
}
|
||||||
|
|
||||||
|
// EndOfYear return end of year
|
||||||
|
func EndOfYear(t time.Time) time.Time {
|
||||||
|
return BeginOfYear(t).AddDate(1, 0, 0).Add(-time.Nanosecond)
|
||||||
|
}
|
||||||
|
|||||||
@@ -64,16 +64,6 @@ func TestGetNowDateTime(t *testing.T) {
|
|||||||
assert.Equal(expected, GetNowDateTime())
|
assert.Equal(expected, GetNowDateTime())
|
||||||
}
|
}
|
||||||
|
|
||||||
//todo
|
|
||||||
//func TestGetZeroHourTimestamp(t *testing.T) {
|
|
||||||
// ts := GetZeroHourTimestamp()
|
|
||||||
// expected := time.Now().UTC().Unix() - 8*3600
|
|
||||||
// if ts != expected {
|
|
||||||
// utils.LogFailedTestInfo(t, "GetZeroHourTimestamp", "", expected, ts)
|
|
||||||
// t.FailNow()
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
func TestFormatTimeToStr(t *testing.T) {
|
func TestFormatTimeToStr(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestFormatTimeToStr")
|
assert := internal.NewAssert(t, "TestFormatTimeToStr")
|
||||||
|
|
||||||
@@ -121,3 +111,123 @@ func TestFormatStrToTime(t *testing.T) {
|
|||||||
assert.Equal(expected, actual)
|
assert.Equal(expected, actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBeginOfMinute(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestBeginOfMinute")
|
||||||
|
|
||||||
|
expected := time.Date(2022, 2, 15, 15, 48, 0, 0, time.Local)
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
actual := BeginOfMinute(td)
|
||||||
|
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEndOfMinute(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestEndOfMinute")
|
||||||
|
|
||||||
|
expected := time.Date(2022, 2, 15, 15, 48, 59, 999999999, time.Local)
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
actual := EndOfMinute(td)
|
||||||
|
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBeginOfHour(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestBeginOfHour")
|
||||||
|
|
||||||
|
expected := time.Date(2022, 2, 15, 15, 0, 0, 0, time.Local)
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
actual := BeginOfHour(td)
|
||||||
|
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEndOfHour(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestEndOfHour")
|
||||||
|
|
||||||
|
expected := time.Date(2022, 2, 15, 15, 59, 59, 999999999, time.Local)
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
actual := EndOfHour(td)
|
||||||
|
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBeginOfDay(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestBeginOfDay")
|
||||||
|
|
||||||
|
expected := time.Date(2022, 2, 15, 0, 0, 0, 0, time.Local)
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
actual := BeginOfDay(td)
|
||||||
|
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEndOfDay(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestEndOfDay")
|
||||||
|
|
||||||
|
expected := time.Date(2022, 2, 15, 23, 59, 59, 999999999, time.Local)
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
actual := EndOfDay(td)
|
||||||
|
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBeginOfWeek(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestBeginOfWeek")
|
||||||
|
|
||||||
|
expected := time.Date(2022, 2, 13, 0, 0, 0, 0, time.Local)
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
actual := BeginOfWeek(td)
|
||||||
|
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEndOfWeek(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestEndOfWeek")
|
||||||
|
|
||||||
|
expected := time.Date(2022, 2, 19, 23, 59, 59, 999999999, time.Local)
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
actual := EndOfWeek(td)
|
||||||
|
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBeginOfMonth(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestBeginOfMonth")
|
||||||
|
|
||||||
|
expected := time.Date(2022, 2, 1, 0, 0, 0, 0, time.Local)
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
actual := BeginOfMonth(td)
|
||||||
|
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEndOfMonth(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestEndOfMonth")
|
||||||
|
|
||||||
|
expected := time.Date(2022, 2, 28, 23, 59, 59, 999999999, time.Local)
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
actual := EndOfMonth(td)
|
||||||
|
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBeginOfYear(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestBeginOfYear")
|
||||||
|
|
||||||
|
expected := time.Date(2022, 1, 1, 0, 0, 0, 0, time.Local)
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
actual := BeginOfYear(td)
|
||||||
|
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEndOfYear(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestEndOfYear")
|
||||||
|
|
||||||
|
expected := time.Date(2022, 12, 31, 23, 59, 59, 999999999, time.Local)
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
actual := EndOfYear(td)
|
||||||
|
|
||||||
|
assert.Equal(expected, actual)
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ Package convertor contains some functions for data type convertion.
|
|||||||
|
|
||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/convertor/convertor.go](https://github.com/duke-git/lancet/blob/main/convertor/convertor.go)
|
[https://github.com/duke-git/lancet/blob/v1/convertor/convertor.go](https://github.com/duke-git/lancet/blob/v1/convertor/convertor.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ convertor转换器包支持一些常见的数据类型转换
|
|||||||
|
|
||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/convertor/convertor.go](https://github.com/duke-git/lancet/blob/main/convertor/convertor.go)
|
[https://github.com/duke-git/lancet/blob/v1/convertor/convertor.go](https://github.com/duke-git/lancet/blob/v1/convertor/convertor.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ Package cryptor contains some functions for data encryption and decryption. Supp
|
|||||||
|
|
||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/cryptor/aes.go](https://github.com/duke-git/lancet/blob/main/cryptor/aes.go)
|
- [https://github.com/duke-git/lancet/blob/v1/cryptor/aes.go](https://github.com/duke-git/lancet/blob/v1/cryptor/aes.go)
|
||||||
- [https://github.com/duke-git/lancet/blob/main/cryptor/des.go](https://github.com/duke-git/lancet/blob/main/cryptor/des.go)
|
- [https://github.com/duke-git/lancet/blob/v1/cryptor/des.go](https://github.com/duke-git/lancet/blob/v1/cryptor/des.go)
|
||||||
- [https://github.com/duke-git/lancet/blob/main/cryptor/basic.go](https://github.com/duke-git/lancet/blob/main/cryptor/basic.go)
|
- [https://github.com/duke-git/lancet/blob/v1/cryptor/basic.go](https://github.com/duke-git/lancet/blob/v1/cryptor/basic.go)
|
||||||
- [https://github.com/duke-git/lancet/blob/main/cryptor/rsa.go](https://github.com/duke-git/lancet/blob/main/cryptor/rsa.go)
|
- [https://github.com/duke-git/lancet/blob/v1/cryptor/rsa.go](https://github.com/duke-git/lancet/blob/v1/cryptor/rsa.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,10 @@ cryptor加密包支持数据加密和解密,获取md5,hash值。支持base64
|
|||||||
|
|
||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
- [https://github.com/duke-git/lancet/blob/main/cryptor/aes.go](https://github.com/duke-git/lancet/blob/main/cryptor/aes.go)
|
- [https://github.com/duke-git/lancet/blob/v1/cryptor/aes.go](https://github.com/duke-git/lancet/blob/v1/cryptor/aes.go)
|
||||||
- [https://github.com/duke-git/lancet/blob/main/cryptor/des.go](https://github.com/duke-git/lancet/blob/main/cryptor/des.go)
|
- [https://github.com/duke-git/lancet/blob/v1/cryptor/des.go](https://github.com/duke-git/lancet/blob/v1/cryptor/des.go)
|
||||||
- [https://github.com/duke-git/lancet/blob/main/cryptor/basic.go](https://github.com/duke-git/lancet/blob/main/cryptor/basic.go)
|
- [https://github.com/duke-git/lancet/blob/v1/cryptor/basic.go](https://github.com/duke-git/lancet/blob/v1/cryptor/basic.go)
|
||||||
- [https://github.com/duke-git/lancet/blob/main/cryptor/rsa.go](https://github.com/duke-git/lancet/blob/main/cryptor/rsa.go)
|
- [https://github.com/duke-git/lancet/blob/v1/cryptor/rsa.go](https://github.com/duke-git/lancet/blob/v1/cryptor/rsa.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
|||||||
566
docs/datetime.md
566
docs/datetime.md
@@ -5,7 +5,7 @@ Package datetime supports date and time format and compare.
|
|||||||
|
|
||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/datetime/datetime.go](https://github.com/duke-git/lancet/blob/main/datetime/datetime.go)
|
[https://github.com/duke-git/lancet/blob/v1/datetime/datetime.go](https://github.com/duke-git/lancet/blob/v1/datetime/datetime.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -22,6 +22,19 @@ import (
|
|||||||
- [AddDay](#AddDay)
|
- [AddDay](#AddDay)
|
||||||
- [AddHour](#AddHour)
|
- [AddHour](#AddHour)
|
||||||
- [AddMinute](#AddMinute)
|
- [AddMinute](#AddMinute)
|
||||||
|
- [BeginOfMinute](#BeginOfMinute)
|
||||||
|
- [BeginOfHour](#BeginOfHour)
|
||||||
|
- [BeginOfDay](#BeginOfDay)
|
||||||
|
- [BeginOfWeek](#BeginOfWeek)
|
||||||
|
- [BeginOfMonth](#BeginOfMonth)
|
||||||
|
- [BeginOfYear](#BeginOfYear)
|
||||||
|
|
||||||
|
- [EndOfMinute](#EndOfMinute)
|
||||||
|
- [EndOfHour](#EndOfHour)
|
||||||
|
- [EndOfDay](#EndOfDay)
|
||||||
|
- [EndOfWeek](#EndOfWeek)
|
||||||
|
- [EndOfMonth](#EndOfMonth)
|
||||||
|
- [EndOfYear](#EndOfYear)
|
||||||
- [GetNowDate](#GetNowDate)
|
- [GetNowDate](#GetNowDate)
|
||||||
- [GetNowTime](#GetNowTime)
|
- [GetNowTime](#GetNowTime)
|
||||||
- [GetNowDateTime](#GetNowDateTime)
|
- [GetNowDateTime](#GetNowDateTime)
|
||||||
@@ -30,6 +43,14 @@ import (
|
|||||||
- [FormatTimeToStr](#FormatTimeToStr)
|
- [FormatTimeToStr](#FormatTimeToStr)
|
||||||
|
|
||||||
- [FormatStrToTime](#FormatStrToTime)
|
- [FormatStrToTime](#FormatStrToTime)
|
||||||
|
- [NewUnixNow](#NewUnixNow)
|
||||||
|
- [NewUnix](#NewUnix)
|
||||||
|
- [NewFormat](#NewFormat)
|
||||||
|
- [NewISO8601](#NewISO8601)
|
||||||
|
- [ToUnix](#ToUnix)
|
||||||
|
- [ToFormat](#ToFormat)
|
||||||
|
- [ToFormatForTpl](#ToFormatForTpl)
|
||||||
|
- [ToIso8601](#ToIso8601)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -142,6 +163,331 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="BeginOfMinute">BeginOfMinute</span>
|
||||||
|
<p>Return beginning minute time of day.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BeginOfMinute(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.BeginOfMinute(td)
|
||||||
|
fmt.Println(bm) //2022-02-15 15:48:00 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="BeginOfHour">BeginOfHour</span>
|
||||||
|
<p>Return zero time of day.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BeginOfHour(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.BeginOfHour(td)
|
||||||
|
fmt.Println(bm) //2022-02-15 15:00:00 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="BeginOfDay">BeginOfDay</span>
|
||||||
|
<p>Return begin time of day.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BeginOfDay(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.BeginOfDay(td)
|
||||||
|
fmt.Println(bm) //2022-02-15 00:00:00 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="BeginOfWeek">BeginOfWeek</span>
|
||||||
|
<p>Return beginning time of week, week begin from Sunday.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BeginOfWeek(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.BeginOfWeek(td)
|
||||||
|
fmt.Println(bm) //2022-02-13 00:00:00 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="BeginOfMonth">BeginOfMonth</span>
|
||||||
|
<p>Return beginning time of month</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BeginOfMonth(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.BeginOfMonth(td)
|
||||||
|
fmt.Println(bm) //2022-02-01 00:00:00 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="BeginOfYear">BeginOfYear</span>
|
||||||
|
<p>Return beginning time of year.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BeginOfYear(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.BeginOfYear(td)
|
||||||
|
fmt.Println(bm) //2022-01-01 00:00:00 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="EndOfMinute">EndOfMinute</span>
|
||||||
|
<p>Return end time minute of day.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EndOfMinute(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.EndOfMinute(td)
|
||||||
|
fmt.Println(bm) //2022-02-15 15:48:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="EndOfHour">EndOfHour</span>
|
||||||
|
<p>Return end time hour of day.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EndOfHour(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.EndOfHour(td)
|
||||||
|
fmt.Println(bm) //2022-02-15 15:59:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="EndOfDay">EndOfDay</span>
|
||||||
|
<p>Return end time hour of day.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EndOfDay(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.EndOfDay(td)
|
||||||
|
fmt.Println(bm) //2022-02-15 23:59:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="EndOfWeek">EndOfWeek</span>
|
||||||
|
<p>Return end time of week, week end with Saturday.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EndOfWeek(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.EndOfWeek(td)
|
||||||
|
fmt.Println(bm) //2022-02-19 23:59:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="EndOfMonth">EndOfMonth</span>
|
||||||
|
<p>Return end time of month</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EndOfMonth(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.EndOfMonth(td)
|
||||||
|
fmt.Println(bm) //2022-02-28 23:59:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="EndOfYear">EndOfYear</span>
|
||||||
|
<p>Return beginning time of year.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EndOfYear(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.EndOfYear(td)
|
||||||
|
fmt.Println(bm) //2022-12-31 23:59:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetNowDate">GetNowDate</span>
|
### <span id="GetNowDate">GetNowDate</span>
|
||||||
<p>Get current date string, format is yyyy-mm-dd.</p>
|
<p>Get current date string, format is yyyy-mm-dd.</p>
|
||||||
|
|
||||||
@@ -329,3 +675,221 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewUnixNow">NewUnixNow</span>
|
||||||
|
<p>Return unix timestamp of current time</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewUnixNow() *theTime
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnixNow()
|
||||||
|
fmt.Println(tm) //&{1647597438}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewUnix">NewUnix</span>
|
||||||
|
<p>Return unix timestamp of specified int64 value.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewUnix(unix int64) *theTime
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnix(1647597438)
|
||||||
|
fmt.Println(tm) //&{1647597438}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewFormat">NewFormat</span>
|
||||||
|
<p>Return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss".</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewFormat(t string) (*theTime, error)
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, err := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
fmt.Println(tm) //&{1647594245}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewISO8601">NewISO8601</span>
|
||||||
|
<p>Return unix timestamp of specified iso8601 time string.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewISO8601(iso8601 string) (*theTime, error)
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, err := datetime.NewISO8601("2006-01-02T15:04:05.999Z")
|
||||||
|
fmt.Println(tm) //&{1136214245}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToUnix">ToUnix</span>
|
||||||
|
<p>Return unix timestamp.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToUnix() int64
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnixNow()
|
||||||
|
fmt.Println(tm.ToUnix()) //1647597438
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToFormat">ToFormat</span>
|
||||||
|
<p>Return time string 'yyyy-mm-dd hh:mm:ss'.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToFormat() string
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
fmt.Println(tm.ToFormat()) //"2022-03-18 17:04:05"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToFormatForTpl">ToFormatForTpl</span>
|
||||||
|
<p>Return the time string which format is specified tpl.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToFormatForTpl(tpl string) string
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
ts := tm.ToFormatForTpl("2006/01/02 15:04:05")
|
||||||
|
fmt.Println(ts) //"2022/03/18 17:04:05"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToIso8601">ToIso8601</span>
|
||||||
|
<p>Return iso8601 time string.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToIso8601() string
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewISO8601("2006-01-02T15:04:05.999Z")
|
||||||
|
ts := tm.ToIso8601()
|
||||||
|
fmt.Println(ts) //"2006-01-02T23:04:05+08:00"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ datetime日期时间处理包,格式化日期,比较日期。
|
|||||||
|
|
||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/datetime/datetime.go](https://github.com/duke-git/lancet/blob/main/datetime/datetime.go)
|
[https://github.com/duke-git/lancet/blob/v1/datetime/datetime.go](https://github.com/duke-git/lancet/blob/v1/datetime/datetime.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -22,14 +22,34 @@ import (
|
|||||||
- [AddDay](#AddDay)
|
- [AddDay](#AddDay)
|
||||||
- [AddHour](#AddHour)
|
- [AddHour](#AddHour)
|
||||||
- [AddMinute](#AddMinute)
|
- [AddMinute](#AddMinute)
|
||||||
|
- [BeginOfMinute](#BeginOfMinute)
|
||||||
|
- [BeginOfHour](#BeginOfHour)
|
||||||
|
- [BeginOfDay](#BeginOfDay)
|
||||||
|
- [BeginOfWeek](#BeginOfWeek)
|
||||||
|
- [BeginOfMonth](#BeginOfMonth)
|
||||||
|
- [BeginOfYear](#BeginOfYear)
|
||||||
|
|
||||||
|
- [EndOfMinute](#EndOfMinute)
|
||||||
|
- [EndOfHour](#EndOfHour)
|
||||||
|
- [EndOfDay](#EndOfDay)
|
||||||
|
- [EndOfWeek](#EndOfWeek)
|
||||||
|
- [EndOfMonth](#EndOfMonth)
|
||||||
|
- [EndOfYear](#EndOfYear)
|
||||||
- [GetNowDate](#GetNowDate)
|
- [GetNowDate](#GetNowDate)
|
||||||
- [GetNowTime](#GetNowTime)
|
- [GetNowTime](#GetNowTime)
|
||||||
- [GetNowDateTime](#GetNowDateTime)
|
- [GetNowDateTime](#GetNowDateTime)
|
||||||
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
|
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
|
||||||
- [GetNightTimestamp](#GetNightTimestamp)
|
- [GetNightTimestamp](#GetNightTimestamp)
|
||||||
|
|
||||||
- [FormatTimeToStr](#FormatTimeToStr)
|
- [FormatTimeToStr](#FormatTimeToStr)
|
||||||
- [FormatStrToTime](#FormatStrToTime)
|
- [FormatStrToTime](#FormatStrToTime)
|
||||||
|
- [NewUnixNow](#NewUnixNow)
|
||||||
|
- [NewUnix](#NewUnix)
|
||||||
|
- [NewFormat](#NewFormat)
|
||||||
|
- [NewISO8601](#NewISO8601)
|
||||||
|
- [ToUnix](#ToUnix)
|
||||||
|
- [ToFormat](#ToFormat)
|
||||||
|
- [ToFormatForTpl](#ToFormatForTpl)
|
||||||
|
- [ToIso8601](#ToIso8601)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -142,6 +162,331 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="BeginOfMinute">BeginOfMinute</span>
|
||||||
|
<p>返回指定时间的分钟开始时间</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BeginOfMinute(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.BeginOfMinute(td)
|
||||||
|
fmt.Println(bm) //2022-02-15 15:48:00 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="BeginOfHour">BeginOfHour</span>
|
||||||
|
<p>返回指定时间的小时开始时间</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BeginOfHour(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.BeginOfHour(td)
|
||||||
|
fmt.Println(bm) //2022-02-15 15:00:00 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="BeginOfDay">BeginOfDay</span>
|
||||||
|
<p>返回指定时间的当天开始时间</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BeginOfDay(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.BeginOfDay(td)
|
||||||
|
fmt.Println(bm) //2022-02-15 00:00:00 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="BeginOfWeek">BeginOfWeek</span>
|
||||||
|
<p>返回指定时间的星期开始时间</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BeginOfWeek(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.BeginOfWeek(td)
|
||||||
|
fmt.Println(bm) //2022-02-13 00:00:00 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="BeginOfMonth">BeginOfMonth</span>
|
||||||
|
<p>返回指定时间的当月开始时间</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BeginOfMonth(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.BeginOfMonth(td)
|
||||||
|
fmt.Println(bm) //2022-02-01 00:00:00 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="BeginOfYear">BeginOfYear</span>
|
||||||
|
<p>返回指定时间的当年开始时间</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func BeginOfYear(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.BeginOfYear(td)
|
||||||
|
fmt.Println(bm) //2022-01-01 00:00:00 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="EndOfMinute">EndOfMinute</span>
|
||||||
|
<p>返回指定时间的分钟结束时间</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EndOfMinute(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.EndOfMinute(td)
|
||||||
|
fmt.Println(bm) //2022-02-15 15:48:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="EndOfHour">EndOfHour</span>
|
||||||
|
<p>返回指定时间的小时结束时间</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EndOfHour(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.EndOfHour(td)
|
||||||
|
fmt.Println(bm) //2022-02-15 15:59:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="EndOfDay">EndOfDay</span>
|
||||||
|
<p>返回指定时间的当天结束时间.</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EndOfDay(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.EndOfDay(td)
|
||||||
|
fmt.Println(bm) //2022-02-15 23:59:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="EndOfWeek">EndOfWeek</span>
|
||||||
|
<p>返回指定时间的星期结束时间</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EndOfWeek(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.EndOfWeek(td)
|
||||||
|
fmt.Println(bm) //2022-02-19 23:59:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="EndOfMonth">EndOfMonth</span>
|
||||||
|
<p>返回指定时间的月份结束时间</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EndOfMonth(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.EndOfMonth(td)
|
||||||
|
fmt.Println(bm) //2022-02-28 23:59:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="EndOfYear">EndOfYear</span>
|
||||||
|
<p>返回指定时间的年份结束时间</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func EndOfYear(t time.Time) time.Time
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
"github.com/duke-git/lancet/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
td := time.Date(2022, 2, 15, 15, 48, 40, 112, time.Local)
|
||||||
|
bm := datetime.EndOfYear(td)
|
||||||
|
fmt.Println(bm) //2022-12-31 23:59:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetNowDate">GetNowDate</span>
|
### <span id="GetNowDate">GetNowDate</span>
|
||||||
<p>获取当天日期,返回格式:yyyy-mm-dd</p>
|
<p>获取当天日期,返回格式:yyyy-mm-dd</p>
|
||||||
|
|
||||||
@@ -329,3 +674,222 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewUnixNow">NewUnixNow</span>
|
||||||
|
<p>创建一个当前时间的unix时间戳</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewUnixNow() *theTime
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnixNow()
|
||||||
|
fmt.Println(tm) //&{1647597438}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewUnix">NewUnix</span>
|
||||||
|
<p>创建一个unix时间戳</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewUnix(unix int64) *theTime
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnix(1647597438)
|
||||||
|
fmt.Println(tm) //&{1647597438}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewFormat">NewFormat</span>
|
||||||
|
<p>创建一个yyyy-mm-dd hh:mm:ss格式时间字符串的unix时间戳</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewFormat(t string) (*theTime, error)
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, err := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
fmt.Println(tm) //&{1647594245}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="NewISO8601">NewISO8601</span>
|
||||||
|
<p>创建一个iso8601格式时间字符串的unix时间戳</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
type theTime struct {
|
||||||
|
unix int64
|
||||||
|
}
|
||||||
|
func NewISO8601(iso8601 string) (*theTime, error)
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, err := datetime.NewISO8601("2006-01-02T15:04:05.999Z")
|
||||||
|
fmt.Println(tm) //&{1136214245}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToUnix">ToUnix</span>
|
||||||
|
<p>返回unix时间戳</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToUnix() int64
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm := datetime.NewUnixNow()
|
||||||
|
fmt.Println(tm.ToUnix()) //1647597438
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToFormat">ToFormat</span>
|
||||||
|
<p>返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToFormat() string
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
fmt.Println(tm.ToFormat()) //"2022-03-18 17:04:05"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToFormatForTpl">ToFormatForTpl</span>
|
||||||
|
<p>返回tpl格式指定的日期字符串</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToFormatForTpl(tpl string) string
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewFormat("2022-03-18 17:04:05")
|
||||||
|
ts := tm.ToFormatForTpl("2006/01/02 15:04:05")
|
||||||
|
fmt.Println(ts) //"2022/03/18 17:04:05"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="ToIso8601">ToIso8601</span>
|
||||||
|
<p>返回iso8601日期字符串</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func (t *theTime) ToIso8601() string
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
tm, _ := datetime.NewISO8601("2006-01-02T15:04:05.999Z")
|
||||||
|
ts := tm.ToIso8601()
|
||||||
|
fmt.Println(ts) //"2006-01-02T23:04:05+08:00"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ Package fileutil implements some basic functions for file operations.
|
|||||||
|
|
||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/fileutil/file.go](https://github.com/duke-git/lancet/blob/main/fileutil/file.go)
|
[https://github.com/duke-git/lancet/blob/v1/fileutil/file.go](https://github.com/duke-git/lancet/blob/v1/fileutil/file.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ fileutil包支持文件基本操作。
|
|||||||
|
|
||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/fileutil/file.go](https://github.com/duke-git/lancet/blob/main/fileutil/file.go)
|
[https://github.com/duke-git/lancet/blob/v1/fileutil/file.go](https://github.com/duke-git/lancet/blob/v1/fileutil/file.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ formatter contains some functions for data formatting.
|
|||||||
|
|
||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/formatter/formatter.go](https://github.com/duke-git/lancet/blob/main/formatter/formatter.go)
|
[https://github.com/duke-git/lancet/blob/v1/formatter/formatter.go](https://github.com/duke-git/lancet/blob/v1/formatter/formatter.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ formatter格式化器包含一些数据格式化处理方法。
|
|||||||
|
|
||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/formatter/formatter.go](https://github.com/duke-git/lancet/blob/main/formatter/formatter.go)
|
[https://github.com/duke-git/lancet/blob/v1/formatter/formatter.go](https://github.com/duke-git/lancet/blob/v1/formatter/formatter.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ Package function can control the flow of function execution and support part of
|
|||||||
|
|
||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/function/function.go](https://github.com/duke-git/lancet/blob/main/function/function.go)
|
[https://github.com/duke-git/lancet/blob/v1/function/function.go](https://github.com/duke-git/lancet/blob/v1/function/function.go)
|
||||||
[https://github.com/duke-git/lancet/blob/main/function/watcher.go](https://github.com/duke-git/lancet/blob/main/function/watcher.go)
|
[https://github.com/duke-git/lancet/blob/v1/function/watcher.go](https://github.com/duke-git/lancet/blob/v1/function/watcher.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -26,7 +26,6 @@ import (
|
|||||||
- [Compose](#Compose)
|
- [Compose](#Compose)
|
||||||
- [Debounced](#Debounced)
|
- [Debounced](#Debounced)
|
||||||
- [Delay](#Delay)
|
- [Delay](#Delay)
|
||||||
- [Delay](#Delay)
|
|
||||||
- [Watcher](#Watcher)
|
- [Watcher](#Watcher)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ function函数包控制函数执行流程,包含部分函数式编程。
|
|||||||
|
|
||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/function/function.go](https://github.com/duke-git/lancet/blob/main/function/function.go)
|
[https://github.com/duke-git/lancet/blob/v1/function/function.go](https://github.com/duke-git/lancet/blob/v1/function/function.go)
|
||||||
[https://github.com/duke-git/lancet/blob/main/function/watcher.go](https://github.com/duke-git/lancet/blob/main/function/watcher.go)
|
[https://github.com/duke-git/lancet/blob/v1/function/watcher.go](https://github.com/duke-git/lancet/blob/v1/function/watcher.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -26,7 +26,6 @@ import (
|
|||||||
- [Compose](#Compose)
|
- [Compose](#Compose)
|
||||||
- [Debounced](#Debounced)
|
- [Debounced](#Debounced)
|
||||||
- [Delay](#Delay)
|
- [Delay](#Delay)
|
||||||
- [Delay](#Delay)
|
|
||||||
- [Watcher](#Watcher)
|
- [Watcher](#Watcher)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|||||||
233
docs/mathutil.md
Normal file
233
docs/mathutil.md
Normal file
@@ -0,0 +1,233 @@
|
|||||||
|
# Mathutil
|
||||||
|
Package mathutil implements some functions for math calculation.
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Source:
|
||||||
|
|
||||||
|
[https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go)
|
||||||
|
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Example:
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Index
|
||||||
|
- [Exponent](#Exponent)
|
||||||
|
- [Fibonacci](#Fibonacci)
|
||||||
|
- [Factorial](#Factorial)
|
||||||
|
- [Percent](#Percent)
|
||||||
|
- [RoundToFloat](#RoundToFloat)
|
||||||
|
- [RoundToString](#RoundToString)
|
||||||
|
- [TruncRound](#TruncRound)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Exponent">Exponent</span>
|
||||||
|
<p>Calculate x to the nth power.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Exponent(x, n int64) int64
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Exponent(10, 0)) //1
|
||||||
|
fmt.Println(mathutil.Exponent(10, 1)) //10
|
||||||
|
fmt.Println(mathutil.Exponent(10, 2)) //100
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Fibonacci">Fibonacci</span>
|
||||||
|
<p>Calculate the nth number of fibonacci sequence.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Fibonacci(first, second, n int) int
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Fibonacci(1, 1, 1)) //1
|
||||||
|
fmt.Println(mathutil.Fibonacci(1, 1, 2)) //1
|
||||||
|
fmt.Println(mathutil.Fibonacci(1, 1, 3)) //2
|
||||||
|
fmt.Println(mathutil.Fibonacci(1, 1, 4)) //3
|
||||||
|
fmt.Println(mathutil.Fibonacci(1, 1, 5)) //5
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Factorial">Factorial</span>
|
||||||
|
<p>Calculate the factorial of x.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Factorial(x uint) uint
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Factorial(0)) //1
|
||||||
|
fmt.Println(mathutil.Factorial(1)) //1
|
||||||
|
fmt.Println(mathutil.Factorial(2)) //2
|
||||||
|
fmt.Println(mathutil.Factorial(3)) //6
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Percent">Percent</span>
|
||||||
|
<p>calculate the percentage of val to total, retain n decimal places.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Percent(val, total float64, n int) float64
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Percent(1, 2, 2)) //1
|
||||||
|
fmt.Println(mathutil.Percent(0.1, 0.3, 2)) //33.33
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="RoundToFloat">RoundToFloat</span>
|
||||||
|
<p>Round float up to n decimal places.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RoundToFloat(x float64, n int) float64
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.RoundToFloat(0, 0)) //0
|
||||||
|
fmt.Println(mathutil.RoundToFloat(0, 1)) //0
|
||||||
|
fmt.Println(mathutil.RoundToFloat(0.124, 2)) //0.12
|
||||||
|
fmt.Println(mathutil.RoundToFloat(0.125, 2)) //0.13
|
||||||
|
fmt.Println(mathutil.RoundToFloat(0.125, 3)) //0.125
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="RoundToString">RoundToString</span>
|
||||||
|
<p>Round float up to n decimal places. will return string.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RoundToString(x float64, n int) string
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.RoundToString(0, 0)) //"0"
|
||||||
|
fmt.Println(mathutil.RoundToString(0, 1)) //"0.0:
|
||||||
|
fmt.Println(mathutil.RoundToString(0.124, 2)) //"0.12"
|
||||||
|
fmt.Println(mathutil.RoundToString(0.125, 2)) //"0.13"
|
||||||
|
fmt.Println(mathutil.RoundToString(0.125, 3)) //"0.125"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="TruncRound">TruncRound</span>
|
||||||
|
<p>Round float off n decimal places.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func TruncRound(x float64, n int) float64
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.TruncRound(0, 0)) //0
|
||||||
|
fmt.Println(mathutil.TruncRound(0, 1)) //0
|
||||||
|
fmt.Println(mathutil.TruncRound(0.124, 2)) //0.12
|
||||||
|
fmt.Println(mathutil.TruncRound(0.125, 2)) //0.12
|
||||||
|
fmt.Println(mathutil.TruncRound(0.125, 3)) //0.125
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
234
docs/mathutil_zh-CN.md
Normal file
234
docs/mathutil_zh-CN.md
Normal file
@@ -0,0 +1,234 @@
|
|||||||
|
# Mathutil
|
||||||
|
mathutil包实现了一些数学计算的函数.
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 源码:
|
||||||
|
|
||||||
|
[https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go)
|
||||||
|
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 用法:
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## 目录
|
||||||
|
- [Exponent](#Exponent)
|
||||||
|
- [Fibonacci](#Fibonacci)
|
||||||
|
- [Factorial](#Factorial)
|
||||||
|
|
||||||
|
- [Percent](#Percent)
|
||||||
|
- [RoundToFloat](#RoundToFloat)
|
||||||
|
- [RoundToString](#RoundToString)
|
||||||
|
- [TruncRound](#TruncRound)
|
||||||
|
|
||||||
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Exponent">Exponent</span>
|
||||||
|
<p>指数计算(x的n次方)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Exponent(x, n int64) int64
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Exponent(10, 0)) //1
|
||||||
|
fmt.Println(mathutil.Exponent(10, 1)) //10
|
||||||
|
fmt.Println(mathutil.Exponent(10, 2)) //100
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Fibonacci">Fibonacci</span>
|
||||||
|
<p>计算斐波那契数列的第n个数</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Fibonacci(first, second, n int) int
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Fibonacci(1, 1, 1)) //1
|
||||||
|
fmt.Println(mathutil.Fibonacci(1, 1, 2)) //1
|
||||||
|
fmt.Println(mathutil.Fibonacci(1, 1, 3)) //2
|
||||||
|
fmt.Println(mathutil.Fibonacci(1, 1, 4)) //3
|
||||||
|
fmt.Println(mathutil.Fibonacci(1, 1, 5)) //5
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Factorial">Factorial</span>
|
||||||
|
<p>计算阶乘</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Factorial(x uint) uint
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Factorial(0)) //1
|
||||||
|
fmt.Println(mathutil.Factorial(1)) //1
|
||||||
|
fmt.Println(mathutil.Factorial(2)) //2
|
||||||
|
fmt.Println(mathutil.Factorial(3)) //6
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Percent">Percent</span>
|
||||||
|
<p>计算百分比,保留n位小数</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Percent(val, total float64, n int) float64
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.Percent(1, 2, 2)) //1
|
||||||
|
fmt.Println(mathutil.Percent(0.1, 0.3, 2)) //33.33
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="RoundToFloat">RoundToFloat</span>
|
||||||
|
<p>四舍五入,保留n位小数</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RoundToFloat(x float64, n int) float64
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.RoundToFloat(0, 0)) //0
|
||||||
|
fmt.Println(mathutil.RoundToFloat(0, 1)) //0
|
||||||
|
fmt.Println(mathutil.RoundToFloat(0.124, 2)) //0.12
|
||||||
|
fmt.Println(mathutil.RoundToFloat(0.125, 2)) //0.13
|
||||||
|
fmt.Println(mathutil.RoundToFloat(0.125, 3)) //0.125
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="RoundToString">RoundToString</span>
|
||||||
|
<p>四舍五入,保留n位小数,返回字符串</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func RoundToString(x float64, n int) string
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.RoundToString(0, 0)) //"0"
|
||||||
|
fmt.Println(mathutil.RoundToString(0, 1)) //"0.0:
|
||||||
|
fmt.Println(mathutil.RoundToString(0.124, 2)) //"0.12"
|
||||||
|
fmt.Println(mathutil.RoundToString(0.125, 2)) //"0.13"
|
||||||
|
fmt.Println(mathutil.RoundToString(0.125, 3)) //"0.125"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="TruncRound">TruncRound</span>
|
||||||
|
<p>截短n位小数(不进行四舍五入)</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func TruncRound(x float64, n int) float64
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/mathutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(mathutil.TruncRound(0, 0)) //0
|
||||||
|
fmt.Println(mathutil.TruncRound(0, 1)) //0
|
||||||
|
fmt.Println(mathutil.TruncRound(0.124, 2)) //0.12
|
||||||
|
fmt.Println(mathutil.TruncRound(0.125, 2)) //0.12
|
||||||
|
fmt.Println(mathutil.TruncRound(0.125, 3)) //0.125
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -5,9 +5,9 @@ Package netutil contains functions to get net information and send http request.
|
|||||||
|
|
||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
[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/v1/netutil/net.go](https://github.com/duke-git/lancet/blob/v1/netutil/net.go)
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/netutil/request.go](https://github.com/duke-git/lancet/blob/main/netutil/request.go)
|
[https://github.com/duke-git/lancet/blob/v1/netutil/http.go](https://github.com/duke-git/lancet/blob/v1/netutil/http.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -23,6 +23,8 @@ import (
|
|||||||
## Index
|
## Index
|
||||||
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
||||||
- [GetInternalIp](#GetInternalIp)
|
- [GetInternalIp](#GetInternalIp)
|
||||||
|
- [GetIps](#GetIps)
|
||||||
|
- [GetMacAddrs](#GetMacAddrs)
|
||||||
- [GetPublicIpInfo](#GetPublicIpInfo)
|
- [GetPublicIpInfo](#GetPublicIpInfo)
|
||||||
- [IsPublicIP](#IsPublicIP)
|
- [IsPublicIP](#IsPublicIP)
|
||||||
- [HttpGet](#HttpGet)
|
- [HttpGet](#HttpGet)
|
||||||
@@ -99,6 +101,60 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="GetIps">GetIps</span>
|
||||||
|
<p>Get all ipv4 list.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GetIps() []string
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"github.com/duke-git/lancet/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ips := netutil.GetIps()
|
||||||
|
fmt.Println(ips) //[192.168.1.9]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="GetMacAddrs">GetMacAddrs</span>
|
||||||
|
<p>Get all mac addresses list.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GetMacAddrs() []string {
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"github.com/duke-git/lancet/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
addrs := netutil.GetMacAddrs()
|
||||||
|
fmt.Println(addrs)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetPublicIpInfo">GetPublicIpInfo</span>
|
### <span id="GetPublicIpInfo">GetPublicIpInfo</span>
|
||||||
<p>Get public ip information.</p>
|
<p>Get public ip information.</p>
|
||||||
|
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ netutil网络包支持获取ip地址,发送http请求。
|
|||||||
|
|
||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/netutil/net.go](https://github.com/duke-git/lancet/blob/main/netutil/net.go)
|
[https://github.com/duke-git/lancet/blob/v1/netutil/net.go](https://github.com/duke-git/lancet/blob/v1/netutil/net.go)
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/netutil/request.go](https://github.com/duke-git/lancet/blob/main/netutil/request.go)
|
[https://github.com/duke-git/lancet/blob/v1/netutil/http.go](https://github.com/duke-git/lancet/blob/v1/netutil/http.go)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -23,6 +23,8 @@ import (
|
|||||||
## 目录
|
## 目录
|
||||||
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
||||||
- [GetInternalIp](#GetInternalIp)
|
- [GetInternalIp](#GetInternalIp)
|
||||||
|
- [GetIps](#GetIps)
|
||||||
|
- [GetMacAddrs](#GetMacAddrs)
|
||||||
- [GetPublicIpInfo](#GetPublicIpInfo)
|
- [GetPublicIpInfo](#GetPublicIpInfo)
|
||||||
- [IsPublicIP](#IsPublicIP)
|
- [IsPublicIP](#IsPublicIP)
|
||||||
- [HttpGet](#HttpGet)
|
- [HttpGet](#HttpGet)
|
||||||
@@ -98,6 +100,59 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="GetIps">GetIps</span>
|
||||||
|
<p>获取ipv4地址列表</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GetIps() []string
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"github.com/duke-git/lancet/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ips := netutil.GetIps()
|
||||||
|
fmt.Println(ips) //[192.168.1.9]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="GetMacAddrs">GetMacAddrs</span>
|
||||||
|
<p>获取mac地址列</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func GetMacAddrs() []string {
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"github.com/duke-git/lancet/netutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
addrs := netutil.GetMacAddrs()
|
||||||
|
fmt.Println(addrs)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetPublicIpInfo">GetPublicIpInfo</span>
|
### <span id="GetPublicIpInfo">GetPublicIpInfo</span>
|
||||||
<p>获取公网ip信息</p>
|
<p>获取公网ip信息</p>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ Package random implements some basic functions to generate random int and string
|
|||||||
|
|
||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/random/random.go](https://github.com/duke-git/lancet/blob/main/random/random.go)
|
[https://github.com/duke-git/lancet/blob/v1/random/random.go](https://github.com/duke-git/lancet/blob/v1/random/random.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -23,6 +23,7 @@ import (
|
|||||||
- [RandBytes](#RandBytes)
|
- [RandBytes](#RandBytes)
|
||||||
- [RandInt](#RandInt)
|
- [RandInt](#RandInt)
|
||||||
- [RandString](#RandString)
|
- [RandString](#RandString)
|
||||||
|
- [UUIdV4](#UUIdV4)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -105,3 +106,33 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="UUIdV4">UUIdV4</span>
|
||||||
|
<p>Generate a random UUID of version 4 according to RFC 4122.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func UUIdV4() (string, error)
|
||||||
|
```
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/random"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
uuid, err := random.UUIdV4()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(uuid)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ random随机数生成器包,可以生成随机[]bytes, int, string。
|
|||||||
|
|
||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/random/random.go](https://github.com/duke-git/lancet/blob/main/random/random.go)
|
[https://github.com/duke-git/lancet/blob/v1/random/random.go](https://github.com/duke-git/lancet/blob/v1/random/random.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -23,6 +23,8 @@ import (
|
|||||||
- [RandBytes](#RandBytes)
|
- [RandBytes](#RandBytes)
|
||||||
- [RandInt](#RandInt)
|
- [RandInt](#RandInt)
|
||||||
- [RandString](#RandString)
|
- [RandString](#RandString)
|
||||||
|
- [UUIdV4](#UUIdV4)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -105,3 +107,32 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="UUIdV4">UUIdV4</span>
|
||||||
|
<p>生成UUID v4字符串</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func UUIdV4() (string, error)
|
||||||
|
```
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/random"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
uuid, err := random.UUIdV4()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println(uuid)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ Package retry is for executing a function repeatedly until it was successful or
|
|||||||
|
|
||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/retry/retry.go](https://github.com/duke-git/lancet/blob/main/retry/retry.go)
|
[https://github.com/duke-git/lancet/blob/v1/retry/retry.go](https://github.com/duke-git/lancet/blob/v1/retry/retry.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ retry重试执行函数直到函数运行成功或被context cancel。
|
|||||||
|
|
||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/retry/retry.go](https://github.com/duke-git/lancet/blob/main/retry/retry.go)
|
[https://github.com/duke-git/lancet/blob/v1/retry/retry.go](https://github.com/duke-git/lancet/blob/v1/retry/retry.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ Package slice implements some functions to manipulate slice.
|
|||||||
|
|
||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/slice/slice.go](https://github.com/duke-git/lancet/blob/main/slice/slice.go)
|
[https://github.com/duke-git/lancet/blob/v1/slice/slice.go](https://github.com/duke-git/lancet/blob/v1/slice/slice.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ slice包包含操作切片的方法集合。
|
|||||||
|
|
||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/slice/slice.go](https://github.com/duke-git/lancet/blob/main/slice/slice.go)
|
[https://github.com/duke-git/lancet/blob/v1/slice/slice.go](https://github.com/duke-git/lancet/blob/v1/slice/slice.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ Package strutil contains some functions to manipulate string.
|
|||||||
|
|
||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/strutil/string.go](https://github.com/duke-git/lancet/blob/main/strutil/string.go)
|
[https://github.com/duke-git/lancet/blob/v1/strutil/string.go](https://github.com/duke-git/lancet/blob/v1/strutil/string.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ strutil包含处理字符串的相关函数。
|
|||||||
|
|
||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/strutil/string.go](https://github.com/duke-git/lancet/blob/main/strutil/string.go)
|
[https://github.com/duke-git/lancet/blob/v1/strutil/string.go](https://github.com/duke-git/lancet/blob/v1/strutil/string.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ Package system contains some functions about os, runtime, shell command.
|
|||||||
|
|
||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/system/os.go](https://github.com/duke-git/lancet/blob/main/system/os.go)
|
[https://github.com/duke-git/lancet/blob/v1/system/os.go](https://github.com/duke-git/lancet/blob/v1/system/os.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ 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)
|
[https://github.com/duke-git/lancet/blob/v1/system/os.go](https://github.com/duke-git/lancet/blob/v1/system/os.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ Package validator contains some functions for data validation.
|
|||||||
|
|
||||||
## Source:
|
## Source:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/validator/validator.go](https://github.com/duke-git/lancet/blob/main/validator/validator.go)
|
[https://github.com/duke-git/lancet/blob/v1/validator/validator.go](https://github.com/duke-git/lancet/blob/v1/validator/validator.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ validator验证器包,包含常用字符串格式验证函数。
|
|||||||
|
|
||||||
## 源码:
|
## 源码:
|
||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/main/validator/validator.go](https://github.com/duke-git/lancet/blob/main/validator/validator.go)
|
[https://github.com/duke-git/lancet/blob/v1/validator/validator.go](https://github.com/duke-git/lancet/blob/v1/validator/validator.go)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ func TestListFileNames(t *testing.T) {
|
|||||||
filesInPath, err := ListFileNames("../datetime/")
|
filesInPath, err := ListFileNames("../datetime/")
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
|
|
||||||
expected := []string{"datetime.go", "datetime_test.go"}
|
expected := []string{"conversion.go", "conversion_test.go", "datetime.go", "datetime_test.go"}
|
||||||
assert.Equal(expected, filesInPath)
|
assert.Equal(expected, filesInPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value {
|
func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value {
|
||||||
// Catch programming error while constructing the closure
|
// Catch programming error while constructing the closure
|
||||||
mustBeFunction(fn)
|
mustBeFunction(fn)
|
||||||
|
|
||||||
return func(args ...interface{}) []reflect.Value {
|
return func(args ...interface{}) []reflect.Value {
|
||||||
n--
|
n--
|
||||||
if n < 1 {
|
if n < 1 {
|
||||||
@@ -66,12 +67,18 @@ func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) in
|
|||||||
|
|
||||||
// Delay make the function execution after delayed time
|
// Delay make the function execution after delayed time
|
||||||
func Delay(delay time.Duration, fn interface{}, args ...interface{}) {
|
func Delay(delay time.Duration, fn interface{}, args ...interface{}) {
|
||||||
|
// Catch programming error while constructing the closure
|
||||||
|
mustBeFunction(fn)
|
||||||
|
|
||||||
time.Sleep(delay)
|
time.Sleep(delay)
|
||||||
invokeFunc(fn, args...)
|
invokeFunc(fn, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debounced creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked.
|
// Debounced creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked.
|
||||||
func Debounced(fn func(), duration time.Duration) func() {
|
func Debounced(fn func(), duration time.Duration) func() {
|
||||||
|
// Catch programming error while constructing the closure
|
||||||
|
mustBeFunction(fn)
|
||||||
|
|
||||||
timer := time.NewTimer(duration)
|
timer := time.NewTimer(duration)
|
||||||
timer.Stop()
|
timer.Stop()
|
||||||
|
|
||||||
@@ -91,6 +98,7 @@ func Debounced(fn func(), duration time.Duration) func() {
|
|||||||
func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool {
|
func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool {
|
||||||
// Catch programming error while constructing the closure
|
// Catch programming error while constructing the closure
|
||||||
mustBeFunction(fn)
|
mustBeFunction(fn)
|
||||||
|
|
||||||
quit := make(chan bool)
|
quit := make(chan bool)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
|
|||||||
92
mathutil/mathutil.go
Normal file
92
mathutil/mathutil.go
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
|
||||||
|
// Use of this source code is governed by MIT license
|
||||||
|
|
||||||
|
// Package mathutil implements some functions for math calculation.
|
||||||
|
package mathutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Exponent calculate x^n
|
||||||
|
func Exponent(x, n int64) int64 {
|
||||||
|
if n == 0 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
t := Exponent(x, n/2)
|
||||||
|
|
||||||
|
if n%2 == 1 {
|
||||||
|
return t * t * x
|
||||||
|
}
|
||||||
|
|
||||||
|
return t * t
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fibonacci calculate fibonacci number before n
|
||||||
|
func Fibonacci(first, second, n int) int {
|
||||||
|
if n <= 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
if n < 3 {
|
||||||
|
return 1
|
||||||
|
} else if n == 3 {
|
||||||
|
return first + second
|
||||||
|
} else {
|
||||||
|
return Fibonacci(second, first+second, n-1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Factorial calculate x!
|
||||||
|
func Factorial(x uint) uint {
|
||||||
|
var f uint = 1
|
||||||
|
for ; x > 1; x-- {
|
||||||
|
f *= x
|
||||||
|
}
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
|
||||||
|
// Percent calculate the percentage of val to total
|
||||||
|
func Percent(val, total float64, n int) float64 {
|
||||||
|
if total == 0 {
|
||||||
|
return float64(0)
|
||||||
|
}
|
||||||
|
tmp := val / total * 100
|
||||||
|
res := RoundToFloat(tmp, n)
|
||||||
|
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoundToString round up to n decimal places
|
||||||
|
func RoundToString(x float64, n int) string {
|
||||||
|
tmp := math.Pow(10.0, float64(n))
|
||||||
|
x *= tmp
|
||||||
|
x = math.Round(x)
|
||||||
|
res := strconv.FormatFloat(x/tmp, 'f', n, 64)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoundToFloat round up to n decimal places
|
||||||
|
func RoundToFloat(x float64, n int) float64 {
|
||||||
|
tmp := math.Pow(10.0, float64(n))
|
||||||
|
x *= tmp
|
||||||
|
x = math.Round(x)
|
||||||
|
return x / tmp
|
||||||
|
}
|
||||||
|
|
||||||
|
// TruncRound round off n decimal places
|
||||||
|
func TruncRound(x float64, n int) float64 {
|
||||||
|
floatStr := fmt.Sprintf("%."+strconv.Itoa(n+1)+"f", x)
|
||||||
|
temp := strings.Split(floatStr, ".")
|
||||||
|
var newFloat string
|
||||||
|
if len(temp) < 2 || n >= len(temp[1]) {
|
||||||
|
newFloat = floatStr
|
||||||
|
} else {
|
||||||
|
newFloat = temp[0] + "." + temp[1][:n]
|
||||||
|
}
|
||||||
|
res, _ := strconv.ParseFloat(newFloat, 64)
|
||||||
|
return res
|
||||||
|
}
|
||||||
72
mathutil/mathutil_test.go
Normal file
72
mathutil/mathutil_test.go
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package mathutil
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/duke-git/lancet/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestExponent(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestExponent")
|
||||||
|
|
||||||
|
assert.Equal(int64(1), Exponent(10, 0))
|
||||||
|
assert.Equal(int64(10), Exponent(10, 1))
|
||||||
|
assert.Equal(int64(100), Exponent(10, 2))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFibonacci(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestFibonacci")
|
||||||
|
|
||||||
|
assert.Equal(0, Fibonacci(1, 1, 0))
|
||||||
|
assert.Equal(1, Fibonacci(1, 1, 1))
|
||||||
|
assert.Equal(1, Fibonacci(1, 1, 2))
|
||||||
|
assert.Equal(5, Fibonacci(1, 1, 5))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFactorial(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestFactorial")
|
||||||
|
|
||||||
|
assert.Equal(uint(1), Factorial(0))
|
||||||
|
assert.Equal(uint(1), Factorial(1))
|
||||||
|
assert.Equal(uint(2), Factorial(2))
|
||||||
|
assert.Equal(uint(6), Factorial(3))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPercent(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestPercent")
|
||||||
|
|
||||||
|
assert.Equal(float64(50), Percent(1, 2, 2))
|
||||||
|
assert.Equal(float64(33.33), Percent(0.1, 0.3, 2))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRoundToFloat(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestRoundToFloat")
|
||||||
|
|
||||||
|
assert.Equal(RoundToFloat(0, 0), float64(0))
|
||||||
|
assert.Equal(RoundToFloat(0, 1), float64(0))
|
||||||
|
assert.Equal(RoundToFloat(0.124, 2), float64(0.12))
|
||||||
|
assert.Equal(RoundToFloat(0.125, 2), float64(0.13))
|
||||||
|
assert.Equal(RoundToFloat(0.125, 3), float64(0.125))
|
||||||
|
assert.Equal(RoundToFloat(33.33333, 2), float64(33.33))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRoundToString(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestRoundToString")
|
||||||
|
|
||||||
|
assert.Equal(RoundToString(0, 0), "0")
|
||||||
|
assert.Equal(RoundToString(0, 1), "0.0")
|
||||||
|
assert.Equal(RoundToString(0.124, 2), "0.12")
|
||||||
|
assert.Equal(RoundToString(0.125, 2), "0.13")
|
||||||
|
assert.Equal(RoundToString(0.125, 3), "0.125")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTruncRound(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestTruncRound")
|
||||||
|
|
||||||
|
assert.Equal(TruncRound(0, 0), float64(0))
|
||||||
|
assert.Equal(TruncRound(0, 1), float64(0))
|
||||||
|
assert.Equal(TruncRound(0.124, 2), float64(0.12))
|
||||||
|
assert.Equal(TruncRound(0.125, 2), float64(0.12))
|
||||||
|
assert.Equal(TruncRound(0.125, 3), float64(0.125))
|
||||||
|
assert.Equal(TruncRound(33.33333, 2), float64(33.33))
|
||||||
|
}
|
||||||
@@ -6,7 +6,8 @@
|
|||||||
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `url` is required.
|
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `url` is required.
|
||||||
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `params` is variable, the order is:
|
// HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `params` is variable, the order is:
|
||||||
// params[0] is header which type should be http.Header or map[string]string,
|
// params[0] is header which type should be http.Header or map[string]string,
|
||||||
// params[1] is query param which type should be url.Values or map[string]interface{},
|
// params[1] is query param which type should be url.Values or map[string]interface{}, when content-type header is
|
||||||
|
// multipart/form-data or application/x-www-form-urlencoded must pass url.Values params
|
||||||
// params[2] is post body which type should be []byte.
|
// params[2] is post body which type should be []byte.
|
||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
package netutil
|
package netutil
|
||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"net/url"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/internal"
|
"github.com/duke-git/lancet/internal"
|
||||||
@@ -46,6 +47,29 @@ func TestHttpPost(t *testing.T) {
|
|||||||
t.Log("response: ", resp.StatusCode, string(body))
|
t.Log("response: ", resp.StatusCode, string(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHttpPostFormData(t *testing.T) {
|
||||||
|
apiUrl := "https://jsonplaceholder.typicode.com/todos"
|
||||||
|
header := map[string]string{
|
||||||
|
// "Content-Type": "application/x-www-form-urlencoded",
|
||||||
|
"Content-Type": "multipart/form-data",
|
||||||
|
}
|
||||||
|
type Todo struct {
|
||||||
|
UserId int `json:"userId"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
}
|
||||||
|
postData := url.Values{}
|
||||||
|
postData.Add("userId", "1")
|
||||||
|
postData.Add("title", "TestAddToDo")
|
||||||
|
|
||||||
|
resp, err := HttpPost(apiUrl, header, postData, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
|
t.Log("response: ", resp.StatusCode, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
func TestHttpPut(t *testing.T) {
|
func TestHttpPut(t *testing.T) {
|
||||||
url := "https://jsonplaceholder.typicode.com/todos/1"
|
url := "https://jsonplaceholder.typicode.com/todos/1"
|
||||||
header := map[string]string{
|
header := map[string]string{
|
||||||
@@ -47,6 +47,46 @@ func GetPublicIpInfo() (*PublicIpInfo, error) {
|
|||||||
return &ip, nil
|
return &ip, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetIps return all ipv4 of system
|
||||||
|
func GetIps() []string {
|
||||||
|
var ips []string
|
||||||
|
|
||||||
|
addrs, err := net.InterfaceAddrs()
|
||||||
|
if err != nil {
|
||||||
|
return ips
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, addr := range addrs {
|
||||||
|
ipNet, isValid := addr.(*net.IPNet)
|
||||||
|
if isValid && !ipNet.IP.IsLoopback() {
|
||||||
|
if ipNet.IP.To4() != nil {
|
||||||
|
ips = append(ips, ipNet.IP.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ips
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMacAddrs get mac address
|
||||||
|
func GetMacAddrs() []string {
|
||||||
|
var macAddrs []string
|
||||||
|
|
||||||
|
nets, err := net.Interfaces()
|
||||||
|
if err != nil {
|
||||||
|
return macAddrs
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, net := range nets {
|
||||||
|
macAddr := net.HardwareAddr.String()
|
||||||
|
if len(macAddr) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
macAddrs = append(macAddrs, macAddr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return macAddrs
|
||||||
|
}
|
||||||
|
|
||||||
// PublicIpInfo public ip info: country, region, isp, city, lat, lon, ip
|
// PublicIpInfo public ip info: country, region, isp, city, lat, lon, ip
|
||||||
type PublicIpInfo struct {
|
type PublicIpInfo struct {
|
||||||
Status string `json:"status"`
|
Status string `json:"status"`
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ func doHttpRequest(method, reqUrl string, params ...interface{}) (*http.Response
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
case 3:
|
case 3:
|
||||||
|
|
||||||
err := setHeaderAndQueryAndBody(req, reqUrl, params[0], params[1], params[2])
|
err := setHeaderAndQueryAndBody(req, reqUrl, params[0], params[1], params[2])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -81,7 +82,12 @@ func setHeaderAndQueryAndBody(req *http.Request, reqUrl string, header, queryPar
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = setBodyByte(req, body)
|
if req.Header.Get("Content-Type") == "multipart/form-data" || req.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
|
||||||
|
formData := queryParam.(url.Values)
|
||||||
|
err = setBodyByte(req, []byte(formData.Encode()))
|
||||||
|
} else {
|
||||||
|
err = setBodyByte(req, body)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -157,16 +163,13 @@ func setQueryParam(req *http.Request, reqUrl string, queryParam interface{}) err
|
|||||||
|
|
||||||
func setBodyByte(req *http.Request, body interface{}) error {
|
func setBodyByte(req *http.Request, body interface{}) error {
|
||||||
if body != nil {
|
if body != nil {
|
||||||
var bodyByte []byte
|
switch b := body.(type) {
|
||||||
if body != nil {
|
case []byte:
|
||||||
switch v := body.(type) {
|
req.Body = ioutil.NopCloser(bytes.NewReader(b))
|
||||||
case []byte:
|
req.ContentLength = int64(len(b))
|
||||||
bodyByte = v
|
default:
|
||||||
default:
|
return errors.New("body type should be []byte")
|
||||||
return errors.New("body type should be []byte")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
req.Body = ioutil.NopCloser(bytes.NewReader(bodyByte))
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -42,3 +42,13 @@ func TestIsPublicIP(t *testing.T) {
|
|||||||
assert.Equal(expected[i], actual)
|
assert.Equal(expected[i], actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetIps(t *testing.T) {
|
||||||
|
ips := GetIps()
|
||||||
|
t.Log(ips)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetMacAddrs(t *testing.T) {
|
||||||
|
macAddrs := GetMacAddrs()
|
||||||
|
t.Log(macAddrs)
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ package random
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
crand "crypto/rand"
|
crand "crypto/rand"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
@@ -48,3 +49,18 @@ func RandBytes(length int) []byte {
|
|||||||
}
|
}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UUIdV4 generate a random UUID of version 4 according to RFC 4122
|
||||||
|
func UUIdV4() (string, error) {
|
||||||
|
uuid := make([]byte, 16)
|
||||||
|
|
||||||
|
n, err := io.ReadFull(crand.Reader, uuid)
|
||||||
|
if n != len(uuid) || err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
uuid[8] = uuid[8]&^0xc0 | 0x80
|
||||||
|
uuid[6] = uuid[6]&^0xf0 | 0x40
|
||||||
|
|
||||||
|
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -47,3 +47,16 @@ func TestRandBytes(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal([]byte{}, RandBytes(0))
|
assert.Equal([]byte{}, RandBytes(0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUUIdV4(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestUUIdV4")
|
||||||
|
|
||||||
|
uuid, err := UUIdV4()
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
isUUiDV4 := regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$`)
|
||||||
|
assert.Equal(true, isUUiDV4.MatchString(uuid))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user