1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 23:22:28 +08:00

Compare commits

...

3 Commits

Author SHA1 Message Date
dudaodong
b98d5edbb5 release v1.3.3 2022-11-08 15:14:34 +08:00
dudaodong
9e39c31087 docs: update doc for random package 2022-11-08 15:03:42 +08:00
dudaodong
26bc40c614 feat: add new random function 2022-11-08 14:54:17 +08:00
6 changed files with 845 additions and 521 deletions

View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-v1.16-9cf) ![Go version](https://img.shields.io/badge/go-v1.16-9cf)
[![Release](https://img.shields.io/badge/release-1.3.2-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-1.3.3-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
@@ -62,12 +62,15 @@ func main() {
``` ```
## API Documentation ## API Documentation
### 1. Convertor package contains some functions for data convertion. ### 1. Convertor package contains some functions for data convertion.
```go ```go
import "github.com/duke-git/lancet/convertor" import "github.com/duke-git/lancet/convertor"
``` ```
#### Function list: #### Function list:
- [ColorHexToRGB](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#ColorHexToRGB) - [ColorHexToRGB](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#ColorHexToRGB)
- [ColorRGBToHex](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#ColorRGBToHex) - [ColorRGBToHex](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#ColorRGBToHex)
- [ToBool](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#ToBool) - [ToBool](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#ToBool)
@@ -88,6 +91,7 @@ import "github.com/duke-git/lancet/cryptor"
``` ```
#### Function list: #### Function list:
- [AesEcbEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#AesEcbEncrypt) - [AesEcbEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#AesEcbEncrypt)
- [AesEcbDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#AesEcbDecrypt) - [AesEcbDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#AesEcbDecrypt)
- [AesCbcEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#AesCbcEncrypt) - [AesCbcEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor.md#AesCbcEncrypt)
@@ -123,11 +127,12 @@ import "github.com/duke-git/lancet/cryptor"
### 3. Datetime package supports date and time format and compare. ### 3. Datetime package supports date and time format and compare.
```go ```go
import "github.com/duke-git/lancet/datetime" import "github.com/duke-git/lancet/datetime"
``` ```
#### Function list: #### Function list:
- [AddDay](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddDay) - [AddDay](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddDay)
- [AddHour](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddHour) - [AddHour](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddHour)
- [AddMinute](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddMinute) - [AddMinute](https://github.com/duke-git/lancet/blob/v1/docs/datetime.md#AddMinute)
@@ -188,7 +193,9 @@ import "github.com/duke-git/lancet/fileutil"
```go ```go
import "github.com/duke-git/lancet/formatter" import "github.com/duke-git/lancet/formatter"
``` ```
#### Function list: #### Function list:
- [Comma](https://github.com/duke-git/lancet/blob/v1/docs/formatter.md#Comma) - [Comma](https://github.com/duke-git/lancet/blob/v1/docs/formatter.md#Comma)
### Function package can control the flow of function execution and support part of functional programming ### Function package can control the flow of function execution and support part of functional programming
@@ -198,6 +205,7 @@ import "github.com/duke-git/lancet/function"
``` ```
#### Function list: #### Function list:
- [After](https://github.com/duke-git/lancet/blob/v1/docs/function.md#After) - [After](https://github.com/duke-git/lancet/blob/v1/docs/function.md#After)
- [Before](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Before) - [Before](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Before)
- [Curry](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Curry) - [Curry](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Curry)
@@ -206,7 +214,6 @@ import "github.com/duke-git/lancet/function"
- [Delay](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Delay) - [Delay](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Delay)
- [Watcher](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Watcher) - [Watcher](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Watcher)
### 6. Mathutil package implements some functions for math calculation. ### 6. Mathutil package implements some functions for math calculation.
```go ```go
@@ -214,6 +221,7 @@ import "github.com/duke-git/lancet/mathutil"
``` ```
#### Function list: #### Function list:
- [Exponent](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#Exponent) - [Exponent](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#Exponent)
- [Fibonacci](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#Fibonacci) - [Fibonacci](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#Fibonacci)
- [Factorial](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#Factorial) - [Factorial](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#Factorial)
@@ -222,7 +230,6 @@ import "github.com/duke-git/lancet/mathutil"
- [RoundToString](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#RoundToString) - [RoundToString](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#RoundToString)
- [TruncRound](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#TruncRound) - [TruncRound](https://github.com/duke-git/lancet/blob/v1/docs/mathutil.md#TruncRound)
### 7. Netutil package contains functions to get net information and send http request. ### 7. Netutil package contains functions to get net information and send http request.
```go ```go
@@ -230,6 +237,7 @@ import "github.com/duke-git/lancet/netutil"
``` ```
#### Function list: #### Function list:
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#ConvertMapToQueryString) - [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#ConvertMapToQueryString)
- [EncodeUrl](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#EncodeUrl) - [EncodeUrl](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#EncodeUrl)
- [GetInternalIp](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#GetInternalIp) - [GetInternalIp](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#GetInternalIp)
@@ -246,7 +254,6 @@ import "github.com/duke-git/lancet/netutil"
- [HttpPatch](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#HttpPatch) - [HttpPatch](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#HttpPatch)
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#ParseHttpResponse) - [ParseHttpResponse](https://github.com/duke-git/lancet/blob/v1/docs/netutil.md#ParseHttpResponse)
### 8. Random package implements some basic functions to generate random int and string. ### 8. Random package implements some basic functions to generate random int and string.
```go ```go
@@ -254,9 +261,14 @@ import "github.com/duke-git/lancet/random"
``` ```
#### Function list: #### Function list:
- [RandBytes](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandBytes) - [RandBytes](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandBytes)
- [RandInt](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandInt) - [RandInt](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandInt)
- [RandString](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandString) - [RandString](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandString)
- [RandUpper](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandUpper)
- [RandLower](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandLower)
- [RandNumeral](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandNumeral)
- [RandNumeralOrLetter](https://github.com/duke-git/lancet/blob/v1/docs/random.md#RandNumeralOrLetter)
- [UUIdV4](https://github.com/duke-git/lancet/blob/v1/docs/random.md#UUIdV4) - [UUIdV4](https://github.com/duke-git/lancet/blob/v1/docs/random.md#UUIdV4)
### 9. Retry package is for executing a function repeatedly until it was successful or canceled by the context. ### 9. Retry package is for executing a function repeatedly until it was successful or canceled by the context.
@@ -266,6 +278,7 @@ import "github.com/duke-git/lancet/retry"
``` ```
#### Function list: #### Function list:
- [Context](https://github.com/duke-git/lancet/blob/v1/docs/retry.md#Context) - [Context](https://github.com/duke-git/lancet/blob/v1/docs/retry.md#Context)
- [Retry](https://github.com/duke-git/lancet/blob/v1/docs/retry.md#Retry) - [Retry](https://github.com/duke-git/lancet/blob/v1/docs/retry.md#Retry)
- [RetryFunc](https://github.com/duke-git/lancet/blob/v1/docs/retry.md#RetryFunc) - [RetryFunc](https://github.com/duke-git/lancet/blob/v1/docs/retry.md#RetryFunc)
@@ -279,6 +292,7 @@ import "github.com/duke-git/lancet/slice"
``` ```
#### Function list: #### Function list:
- [AppendIfAbsent](https://github.com/duke-git/lancet/blob/v1/docs/slice.md#AppendIfAbsent) - [AppendIfAbsent](https://github.com/duke-git/lancet/blob/v1/docs/slice.md#AppendIfAbsent)
- [Contain](https://github.com/duke-git/lancet/blob/v1/docs/slice.md#Contain) - [Contain](https://github.com/duke-git/lancet/blob/v1/docs/slice.md#Contain)
- [ContainSubSlice](https://github.com/duke-git/lancet/blob/v1/docs/slice.md#ContainSubSlice) - [ContainSubSlice](https://github.com/duke-git/lancet/blob/v1/docs/slice.md#ContainSubSlice)
@@ -353,6 +367,7 @@ import "github.com/duke-git/lancet/system"
``` ```
#### Function list: #### Function list:
- [IsWindows](https://github.com/duke-git/lancet/blob/v1/docs/system.md#IsWindows) - [IsWindows](https://github.com/duke-git/lancet/blob/v1/docs/system.md#IsWindows)
- [IsLinux](https://github.com/duke-git/lancet/blob/v1/docs/system.md#IsLinux) - [IsLinux](https://github.com/duke-git/lancet/blob/v1/docs/system.md#IsLinux)
- [IsMac](https://github.com/duke-git/lancet/blob/v1/docs/system.md#IsMac) - [IsMac](https://github.com/duke-git/lancet/blob/v1/docs/system.md#IsMac)
@@ -368,6 +383,7 @@ import "github.com/duke-git/lancet/system"
```go ```go
import "github.com/duke-git/lancet/validator" import "github.com/duke-git/lancet/validator"
``` ```
#### Function list: #### Function list:
- [ContainChinese](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#ContainChinese) - [ContainChinese](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#ContainChinese)
@@ -398,7 +414,6 @@ import "github.com/duke-git/lancet/validator"
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsWeakPassword) - [IsWeakPassword](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsWeakPassword)
- [IsZeroValue](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsZeroValue) - [IsZeroValue](https://github.com/duke-git/lancet/blob/v1/docs/validator.md#IsZeroValue)
## How to Contribute ## How to Contribute
I really appreciate any code commits which make lancet lib powerful. Please follow the rules below to create your pull request. I really appreciate any code commits which make lancet lib powerful. Please follow the rules below to create your pull request.

View File

@@ -4,7 +4,7 @@
<br/> <br/>
![Go version](https://img.shields.io/badge/go-v1.16-9cf) ![Go version](https://img.shields.io/badge/go-v1.16-9cf)
[![Release](https://img.shields.io/badge/release-1.3.2-green.svg)](https://github.com/duke-git/lancet/releases) [![Release](https://img.shields.io/badge/release-1.3.3-green.svg)](https://github.com/duke-git/lancet/releases)
[![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet)
[![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet)
[![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml)
@@ -20,13 +20,12 @@
简体中文 | [English](./README.md) 简体中文 | [English](./README.md)
## 特性 ## 特性
- 👏 全面、高效、可复用 - 👏 全面、高效、可复用
- 💪 200+常用go工具函数支持string、slice、datetime、net、crypt... - 💪 200+常用 go 工具函数,支持 string、slice、datetime、net、crypt...
- 💅 只依赖go标准库 - 💅 只依赖 go 标准库
- 🌍 所有导出函数单元测试覆盖率100% - 🌍 所有导出函数单元测试覆盖率 100%
## 安装 ## 安装
@@ -36,7 +35,7 @@ go get github.com/duke-git/lancet
## 用法 ## 用法
lancet是以包的结构组织代码的使用时需要导入相应的包名。例如如果使用字符串相关函数需要导入strutil包: lancet 是以包的结构组织代码的,使用时需要导入相应的包名。例如:如果使用字符串相关函数,需要导入 strutil 包:
```go ```go
import "github.com/duke-git/lancet/strutil" import "github.com/duke-git/lancet/strutil"
@@ -44,7 +43,7 @@ import "github.com/duke-git/lancet/strutil"
## 例子 ## 例子
此处以字符串工具函数Reverse逆序字符串为例需要导入strutil包: 此处以字符串工具函数 Reverse逆序字符串为例需要导入 strutil 包:
```go ```go
package main package main
@@ -61,13 +60,16 @@ func main() {
} }
``` ```
## API文档 ## API 文档
### 1. convertor转换器包支持一些常见的数据类型转换。
### 1. convertor 转换器包支持一些常见的数据类型转换。
```go ```go
import "github.com/duke-git/lancet/convertor" import "github.com/duke-git/lancet/convertor"
``` ```
#### 函数列表: #### 函数列表:
- [ColorHexToRGB](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#ColorHexToRGB) - [ColorHexToRGB](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#ColorHexToRGB)
- [ColorRGBToHex](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#ColorRGBToHex) - [ColorRGBToHex](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#ColorRGBToHex)
- [ToBool](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#ToBool) - [ToBool](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#ToBool)
@@ -81,13 +83,14 @@ import "github.com/duke-git/lancet/convertor"
- [EncodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#EncodeByte) - [EncodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#EncodeByte)
- [DecodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#DecodeByte) - [DecodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#DecodeByte)
### 2. cryptor加密包支持数据加密和解密获取md5hash值。支持base64, md5, hmac, aes, des, rsa。 ### 2. cryptor 加密包支持数据加密和解密,获取 md5hash 值。支持 base64, md5, hmac, aes, des, rsa。
```go ```go
import "github.com/duke-git/lancet/cryptor" import "github.com/duke-git/lancet/cryptor"
``` ```
#### 函数列表: #### 函数列表:
- [AesEcbEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#AesEcbEncrypt) - [AesEcbEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#AesEcbEncrypt)
- [AesEcbDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#AesEcbDecrypt) - [AesEcbDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#AesEcbDecrypt)
- [AesCbcEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#AesCbcEncrypt) - [AesCbcEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#AesCbcEncrypt)
@@ -121,13 +124,14 @@ import "github.com/duke-git/lancet/cryptor"
- [RsaEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#RsaEncrypt) - [RsaEncrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#RsaEncrypt)
- [RsaDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#RsaDecrypt) - [RsaDecrypt](https://github.com/duke-git/lancet/blob/v1/docs/cryptor_zh-CN.md#RsaDecrypt)
### 3. datetime日期时间处理包格式化日期比较日期。 ### 3. datetime 日期时间处理包,格式化日期,比较日期。
```go ```go
import "github.com/duke-git/lancet/datetime" import "github.com/duke-git/lancet/datetime"
``` ```
#### 函数列表: #### 函数列表:
- [AddDay](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddDay) - [AddDay](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddDay)
- [AddHour](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddHour) - [AddHour](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddHour)
- [AddMinute](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddMinute) - [AddMinute](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#AddMinute)
@@ -158,7 +162,8 @@ import "github.com/duke-git/lancet/datetime"
- [ToFormat](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToFormat) - [ToFormat](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToFormat)
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToFormatForTpl) - [ToFormatForTpl](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToFormatForTpl)
- [ToIso8601](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToIso8601) - [ToIso8601](https://github.com/duke-git/lancet/blob/v1/docs/datetime_zh-CN.md#ToIso8601)
### 4. fileutil包支持文件基本操作。
### 4. fileutil 包支持文件基本操作。
```go ```go
import "github.com/duke-git/lancet/fileutil" import "github.com/duke-git/lancet/fileutil"
@@ -182,22 +187,24 @@ import "github.com/duke-git/lancet/fileutil"
- [Zip](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#Zip) - [Zip](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#Zip)
- [UnZip](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#UnZip) - [UnZip](https://github.com/duke-git/lancet/blob/v1/docs/fileutil_zh-CN.md#UnZip)
### 5. formatter格式化器包含一些数据格式化处理方法。 ### 5. formatter 格式化器包含一些数据格式化处理方法。
```go ```go
import "github.com/duke-git/lancet/formatter" import "github.com/duke-git/lancet/formatter"
``` ```
#### 函数列表: #### 函数列表:
- [Comma](https://github.com/duke-git/lancet/blob/v1/docs/formatter_zh-CN.md#Comma) - [Comma](https://github.com/duke-git/lancet/blob/v1/docs/formatter_zh-CN.md#Comma)
### function 函数包控制函数执行流程,包含部分函数式编程。
### function函数包控制函数执行流程包含部分函数式编程。
```go ```go
import "github.com/duke-git/lancet/function" import "github.com/duke-git/lancet/function"
``` ```
#### 函数列表: #### 函数列表:
- [After](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#After) - [After](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#After)
- [Before](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Before) - [Before](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Before)
- [Curry](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Curry) - [Curry](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Curry)
@@ -206,13 +213,14 @@ import "github.com/duke-git/lancet/function"
- [Delay](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Delay) - [Delay](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Delay)
- [Watcher](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Watcher) - [Watcher](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Watcher)
### 6. mathutil包实现了一些数学计算的函数。 ### 6. mathutil 包实现了一些数学计算的函数。
```go ```go
import "github.com/duke-git/lancet/mathutil" import "github.com/duke-git/lancet/mathutil"
``` ```
#### Function list: #### Function list:
- [Exponent](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#Exponent) - [Exponent](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#Exponent)
- [Fibonacci](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#Fibonacci) - [Fibonacci](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#Fibonacci)
- [Factorial](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#Factorial) - [Factorial](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#Factorial)
@@ -221,13 +229,14 @@ import "github.com/duke-git/lancet/mathutil"
- [RoundToString](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#RoundToString) - [RoundToString](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#RoundToString)
- [TruncRound](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#TruncRound) - [TruncRound](https://github.com/duke-git/lancet/blob/v1/docs/mathutil_zh-CN.md#TruncRound)
### 7. netutil网络包支持获取ip地址发送http请求。 ### 7. netutil 网络包支持获取 ip 地址,发送 http 请求。
```go ```go
import "github.com/duke-git/lancet/netutil" import "github.com/duke-git/lancet/netutil"
``` ```
#### 函数列表: #### 函数列表:
- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#ConvertMapToQueryString) - [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#ConvertMapToQueryString)
- [EncodeUrl](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#EncodeUrl) - [EncodeUrl](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#EncodeUrl)
- [GetInternalIp](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#GetInternalIp) - [GetInternalIp](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#GetInternalIp)
@@ -244,38 +253,45 @@ import "github.com/duke-git/lancet/netutil"
- [HttpPatch](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#HttpPatch) - [HttpPatch](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#HttpPatch)
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#ParseHttpResponse) - [ParseHttpResponse](https://github.com/duke-git/lancet/blob/v1/docs/netutil_zh-CN.md#ParseHttpResponse)
### 8. random随机数生成器包可以生成随机[]bytes, int, string。 ### 8. random 随机数生成器包,可以生成随机[]bytes, int, string。
```go ```go
import "github.com/duke-git/lancet/random" import "github.com/duke-git/lancet/random"
``` ```
#### 函数列表: #### 函数列表:
- [RandBytes](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandBytes) - [RandBytes](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandBytes)
- [RandInt](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandInt) - [RandInt](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandInt)
- [RandString](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandString) - [RandString](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandString)
- [RandUpper](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandUpper)
- [RandLower](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandLower)
- [RandNumeral](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandNumeral)
- [RandNumeralOrLetter](https://github.com/duke-git/lancet/blob/v1/docs/random_zh-CN.md#RandNumeralOrLetter)
- [UUIdV4](https://github.com/duke-git/lancet/blob/v1/docs/random.md#UUIdV4) - [UUIdV4](https://github.com/duke-git/lancet/blob/v1/docs/random.md#UUIdV4)
### 9. retry重试执行函数直到函数运行成功或被context cancel。
### 9. retry 重试执行函数直到函数运行成功或被 context cancel。
```go ```go
import "github.com/duke-git/lancet/retry" import "github.com/duke-git/lancet/retry"
``` ```
#### 函数列表: #### 函数列表:
- [Context](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#Context) - [Context](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#Context)
- [Retry](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#Retry) - [Retry](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#Retry)
- [RetryFunc](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#RetryFunc) - [RetryFunc](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#RetryFunc)
- [RetryDuration](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#RetryDuration) - [RetryDuration](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#RetryDuration)
- [RetryTimes](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#RetryTimes) - [RetryTimes](https://github.com/duke-git/lancet/blob/v1/docs/retry_zh-CN.md#RetryTimes)
### 10. slice 包包含操作切片的方法集合。
### 10. slice包包含操作切片的方法集合。
```go ```go
import "github.com/duke-git/lancet/slice" import "github.com/duke-git/lancet/slice"
``` ```
#### 函数列表: #### 函数列表:
- [AppendIfAbsent](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#AppendIfAbsent) - [AppendIfAbsent](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#AppendIfAbsent)
- [Contain](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#Contain) - [Contain](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#Contain)
- [ContainSubSlice](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#ContainSubSlice) - [ContainSubSlice](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#ContainSubSlice)
@@ -317,8 +333,7 @@ import "github.com/duke-git/lancet/slice"
- [UpdateByIndex](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#UpdateByIndex) - [UpdateByIndex](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#UpdateByIndex)
- [Without](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#Without) - [Without](https://github.com/duke-git/lancet/blob/v1/docs/slice_zh-CN.md#Without)
### 12. strutil 包含处理字符串的相关函数。
### 12. strutil包含处理字符串的相关函数。
```go ```go
import "github.com/duke-git/lancet/strutil" import "github.com/duke-git/lancet/strutil"
@@ -344,14 +359,14 @@ import "github.com/duke-git/lancet/strutil"
- [Wrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Wrap) - [Wrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Wrap)
- [Unwrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Unwrap) - [Unwrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Unwrap)
### 13. system 包含 os, runtime, shell command 相关函数。
### 13. system包含os, runtime, shell command相关函数。
```go ```go
import "github.com/duke-git/lancet/system" import "github.com/duke-git/lancet/system"
``` ```
#### 函数列表: #### 函数列表:
- [IsWindows](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#IsWindows) - [IsWindows](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#IsWindows)
- [IsLinux](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#IsLinux) - [IsLinux](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#IsLinux)
- [IsMac](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#IsMac) - [IsMac](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#IsMac)
@@ -362,11 +377,12 @@ import "github.com/duke-git/lancet/system"
- [ExecCommand](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#ExecCommand) - [ExecCommand](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#ExecCommand)
- [GetOsBits](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#GetOsBits) - [GetOsBits](https://github.com/duke-git/lancet/blob/v1/docs/system_zh-CN.md#GetOsBits)
### 14. validator验证器包包含常用字符串格式验证函数。 ### 14. validator 验证器包,包含常用字符串格式验证函数。
```go ```go
import "github.com/duke-git/lancet/validator" import "github.com/duke-git/lancet/validator"
``` ```
#### 函数列表: #### 函数列表:
- [ContainChinese](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#ContainChinese) - [ContainChinese](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#ContainChinese)
@@ -397,13 +413,12 @@ import "github.com/duke-git/lancet/validator"
- [IsWeakPassword](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsWeakPassword) - [IsWeakPassword](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsWeakPassword)
- [IsZeroValue](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsZeroValue) - [IsZeroValue](https://github.com/duke-git/lancet/blob/v1/docs/validator_zh-CN.md#IsZeroValue)
## 如何贡献代码 ## 如何贡献代码
非常感激任何的代码提交以使lancet的功能越来越强大。创建pull request时请遵守以下规则。 非常感激任何的代码提交以使 lancet 的功能越来越强大。创建 pull request 时请遵守以下规则。
1. Fork lancet仓库。 1. Fork lancet 仓库。
2. 创建自己的特性分支。 2. 创建自己的特性分支。
3. 提交变更。 3. 提交变更。
4. Push分支。 4. Push 分支。
5. 创建新的pull request。 5. 创建新的 pull request。

View File

@@ -1,4 +1,5 @@
# Random # Random
Package random implements some basic functions to generate random int and string. Package random implements some basic functions to generate random int and string.
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -7,10 +8,10 @@ Package random implements some basic functions to generate random int and string
[https://github.com/duke-git/lancet/blob/v1/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](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>
## Usage: ## Usage:
```go ```go
import ( import (
"github.com/duke-git/lancet/random" "github.com/duke-git/lancet/random"
@@ -20,17 +21,23 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Index ## Index
- [RandBytes](#RandBytes) - [RandBytes](#RandBytes)
- [RandInt](#RandInt) - [RandInt](#RandInt)
- [RandString](#RandString) - [RandString](#RandString)
- [RandUpper](#RandUpper)
- [RandLower](#RandLower)
- [RandNumeral](#RandNumeral)
- [RandNumeralOrLetter](#RandNumeralOrLetter)
- [UUIdV4](#UUIdV4) - [UUIdV4](#UUIdV4)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## Documentation ## Documentation
### <span id="RandBytes">RandBytes</span> ### <span id="RandBytes">RandBytes</span>
<p>Generate random byte slice.</p> <p>Generate random byte slice.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -38,6 +45,7 @@ import (
```go ```go
func RandBytes(length int) []byte func RandBytes(length int) []byte
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -54,8 +62,8 @@ func main() {
} }
``` ```
### <span id="RandInt">RandInt</span> ### <span id="RandInt">RandInt</span>
<p>Generate random int between min and max, may contain min, not max.</p> <p>Generate random int between min and max, may contain min, not max.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -63,6 +71,7 @@ func main() {
```go ```go
func RandInt(min, max int) int func RandInt(min, max int) int
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -79,16 +88,16 @@ func main() {
} }
``` ```
### <span id="RandString">RandString</span>
<p>Generate random given length string. only contains letter (a-zA-Z)</p>
### <span id="RandString">RandInt</span>
<p>Generate random given length string.</p>
<b>Signature:</b> <b>Signature:</b>
```go ```go
func RandString(length int) string func RandString(length int) string
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -101,14 +110,116 @@ import (
func main() { func main() {
randStr := random.RandString(6) randStr := random.RandString(6)
fmt.Println(randStr) fmt.Println(randStr) //pGWsze
} }
``` ```
### <span id="RandUpper">RandUpper</span>
<p>Generate a random upper case string</p>
<b>Signature:</b>
```go
func RandUpper(length int) string
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/random"
)
func main() {
randStr := random.RandString(6)
fmt.Println(randStr) //PACWGF
}
```
### <span id="RandLower">RandLower</span>
<p>Generate a random lower case string</p>
<b>Signature:</b>
```go
func RandLower(length int) string
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/random"
)
func main() {
randStr := random.RandLower(6)
fmt.Println(randStr) //siqbew
}
```
### <span id="RandNumeral">RandNumeral</span>
<p>Generate a random numeral string</p>
<b>Signature:</b>
```go
func RandNumeral(length int) string
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/random"
)
func main() {
randStr := random.RandNumeral(6)
fmt.Println(randStr) //035172
}
```
### <span id="RandNumeralOrLetter">RandNumeralOrLetter</span>
<p>generate a random numeral or letter string</p>
<b>Signature:</b>
```go
func RandNumeralOrLetter(length int) string
```
<b>Example:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/random"
)
func main() {
randStr := random.RandNumeralOrLetter(6)
fmt.Println(randStr) //0aW7cQ
}
```
### <span id="UUIdV4">UUIdV4</span> ### <span id="UUIdV4">UUIdV4</span>
<p>Generate a random UUID of version 4 according to RFC 4122.</p> <p>Generate a random UUID of version 4 according to RFC 4122.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -116,6 +227,7 @@ func main() {
```go ```go
func UUIdV4() (string, error) func UUIdV4() (string, error)
``` ```
<b>Example:</b> <b>Example:</b>
```go ```go
@@ -134,5 +246,3 @@ func main() {
fmt.Println(uuid) fmt.Println(uuid)
} }
``` ```

View File

@@ -1,5 +1,6 @@
# Random # Random
random随机数生成器包可以生成随机[]bytes, int, string。
random 随机数生成器包,可以生成随机[]bytes, int, string。
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
@@ -7,10 +8,10 @@ random随机数生成器包可以生成随机[]bytes, int, string。
[https://github.com/duke-git/lancet/blob/v1/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](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>
## 用法: ## 用法:
```go ```go
import ( import (
"github.com/duke-git/lancet/random" "github.com/duke-git/lancet/random"
@@ -20,18 +21,23 @@ import (
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 目录 ## 目录
- [RandBytes](#RandBytes) - [RandBytes](#RandBytes)
- [RandInt](#RandInt) - [RandInt](#RandInt)
- [RandString](#RandString) - [RandString](#RandString)
- [UUIdV4](#UUIdV4) - [RandUpper](#RandUpper)
- [RandLower](#RandLower)
- [RandNumeral](#RandNumeral)
- [RandNumeralOrLetter](#RandNumeralOrLetter)
- [UUIdV4](#UUIdV4)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 文档 ## 文档
### <span id="RandBytes">RandBytes</span> ### <span id="RandBytes">RandBytes</span>
<p>生成随机字节切片</p> <p>生成随机字节切片</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -39,6 +45,7 @@ import (
```go ```go
func RandBytes(length int) []byte func RandBytes(length int) []byte
``` ```
<b>例子:</b> <b>例子:</b>
```go ```go
@@ -55,8 +62,8 @@ func main() {
} }
``` ```
### <span id="RandInt">RandInt</span> ### <span id="RandInt">RandInt</span>
<p>生成随机int, 范围[min, max)</p> <p>生成随机int, 范围[min, max)</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -64,6 +71,7 @@ func main() {
```go ```go
func RandInt(min, max int) int func RandInt(min, max int) int
``` ```
<b>例子:</b> <b>例子:</b>
```go ```go
@@ -80,16 +88,16 @@ func main() {
} }
``` ```
### <span id="RandString">RandString</span>
<p>生成给定长度的随机字符串,只包含字母(a-zA-Z)</p>
### <span id="RandString">RandInt</span>
<p>生成随机给定长度的随机字符串</p>
<b>函数签名:</b> <b>函数签名:</b>
```go ```go
func RandString(length int) string func RandString(length int) string
``` ```
<b>例子:</b> <b>例子:</b>
```go ```go
@@ -102,13 +110,116 @@ import (
func main() { func main() {
randStr := random.RandString(6) randStr := random.RandString(6)
fmt.Println(randStr) fmt.Println(randStr) //pGWsze
} }
``` ```
### <span id="RandUpper">RandUpper</span>
<p>生成给定长度的随机大写字母字符串</p>
<b>函数签名:</b>
```go
func RandUpper(length int) string
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/random"
)
func main() {
randStr := random.RandString(6)
fmt.Println(randStr) //PACWGF
}
```
### <span id="RandLower">RandLower</span>
<p>生成给定长度的随机小写字母字符串</p>
<b>函数签名:</b>
```go
func RandLower(length int) string
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/random"
)
func main() {
randStr := random.RandLower(6)
fmt.Println(randStr) //siqbew
}
```
### <span id="RandNumeral">RandNumeral</span>
<p>生成给定长度的随机数字字符串</p>
<b>函数签名:</b>
```go
func RandNumeral(length int) string
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/random"
)
func main() {
randStr := random.RandNumeral(6)
fmt.Println(randStr) //035172
}
```
### <span id="RandNumeralOrLetter">RandNumeralOrLetter</span>
<p>生成给定长度的随机字符串(数字+字母)</p>
<b>函数签名:</b>
```go
func RandNumeralOrLetter(length int) string
```
<b>例子:</b>
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/random"
)
func main() {
randStr := random.RandNumeralOrLetter(6)
fmt.Println(randStr) //0aW7cQ
}
```
### <span id="UUIdV4">UUIdV4</span> ### <span id="UUIdV4">UUIdV4</span>
<p>生成UUID v4字符串</p> <p>生成UUID v4字符串</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -116,6 +227,7 @@ func main() {
```go ```go
func UUIdV4() (string, error) func UUIdV4() (string, error)
``` ```
<b>例子:</b> <b>例子:</b>
```go ```go
@@ -134,5 +246,3 @@ func main() {
fmt.Println(uuid) fmt.Println(uuid)
} }
``` ```

View File

@@ -12,18 +12,12 @@ import (
"time" "time"
) )
// RandString generate random string const (
// see https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go NUMERAL = "0123456789"
func RandString(length int) string { LOWER_LETTERS = "abcdefghijklmnopqrstuvwxyz"
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" UPPER_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, length) )
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := range b {
b[i] = letters[r.Int63()%int64(len(letters))]
}
return string(b)
}
// RandInt generate random int between min and max, maybe min, not be max // RandInt generate random int between min and max, maybe min, not be max
func RandInt(min, max int) int { func RandInt(min, max int) int {
@@ -50,6 +44,41 @@ func RandBytes(length int) []byte {
return b return b
} }
// RandString generate random string
func RandString(length int) string {
return random(LETTERS, length)
}
// RandUpper generate a random upper case string
func RandUpper(length int) string {
return random(UPPER_LETTERS, length)
}
// RandLower generate a random lower case string
func RandLower(length int) string {
return random(LOWER_LETTERS, length)
}
// RandNumeral generate a random numeral string
func RandNumeral(length int) string {
return random(NUMERAL, length)
}
// RandNumeralOrLetter generate a random numeral or letter string
func RandNumeralOrLetter(length int) string {
return random(NUMERAL+LETTERS, length)
}
// random generate a random string based on given string range
func random(s string, length int) string {
b := make([]byte, length)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := range b {
b[i] = s[r.Int63()%int64(len(s))]
}
return string(b)
}
// UUIdV4 generate a random UUID of version 4 according to RFC 4122 // UUIdV4 generate a random UUID of version 4 according to RFC 4122
func UUIdV4() (string, error) { func UUIdV4() (string, error) {
uuid := make([]byte, 16) uuid := make([]byte, 16)

View File

@@ -19,6 +19,51 @@ func TestRandString(t *testing.T) {
assert.Equal(true, reg.MatchString(randStr)) assert.Equal(true, reg.MatchString(randStr))
} }
func TestRandUpper(t *testing.T) {
pattern := `^[A-Z]+$`
reg := regexp.MustCompile(pattern)
randStr := RandUpper(6)
assert := internal.NewAssert(t, "TestRandUpper")
assert.Equal(6, len(randStr))
assert.Equal(true, reg.MatchString(randStr))
}
func TestRandLower(t *testing.T) {
pattern := `^[a-z]+$`
reg := regexp.MustCompile(pattern)
randStr := RandLower(6)
assert := internal.NewAssert(t, "TestRandLower")
assert.Equal(6, len(randStr))
assert.Equal(true, reg.MatchString(randStr))
}
func TestRandNumeral(t *testing.T) {
pattern := `^[0-9]+$`
reg := regexp.MustCompile(pattern)
randStr := RandNumeral(12)
assert := internal.NewAssert(t, "TestRandNumeral")
assert.Equal(12, len(randStr))
assert.Equal(true, reg.MatchString(randStr))
}
func TestRandNumeralOrLetter(t *testing.T) {
pattern := `^[0-9a-zA-Z]+$`
reg := regexp.MustCompile(pattern)
randStr := RandNumeralOrLetter(10)
t.Log(randStr)
assert := internal.NewAssert(t, "TestRandNumeralOrLetter")
assert.Equal(10, len(randStr))
assert.Equal(true, reg.MatchString(randStr))
}
func TestRandInt(t *testing.T) { func TestRandInt(t *testing.T) {
assert := internal.NewAssert(t, "TestRandInt") assert := internal.NewAssert(t, "TestRandInt")