mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-10 07:42:27 +08:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a74038466f | ||
|
|
02daa7f6cb | ||
|
|
fef6fd7b9d | ||
|
|
f6cd98086f | ||
|
|
24eb2bbacd | ||
|
|
15c1537bf0 | ||
|
|
c02654559a | ||
|
|
634ca09e8c | ||
|
|
f2e743dcf4 | ||
|
|
f8f58cae10 | ||
|
|
215b79140d | ||
|
|
0bd675340f |
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
[](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)
|
||||||
@@ -83,6 +83,8 @@ import "github.com/duke-git/lancet/convertor"
|
|||||||
- [StructToMap](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#StructToMap)
|
- [StructToMap](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#StructToMap)
|
||||||
- [EncodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#EncodeByte)
|
- [EncodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#EncodeByte)
|
||||||
- [DecodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#DecodeByte)
|
- [DecodeByte](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#DecodeByte)
|
||||||
|
- [DeepClone](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#DeepClone)
|
||||||
|
- [CopyProperties](https://github.com/duke-git/lancet/blob/v1/docs/convertor.md#CopyProperties)
|
||||||
|
|
||||||
### 2. Cryptor package is for data encryption and decryption.
|
### 2. Cryptor package is for data encryption and decryption.
|
||||||
|
|
||||||
@@ -212,6 +214,8 @@ import "github.com/duke-git/lancet/function"
|
|||||||
- [Compose](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Compose)
|
- [Compose](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Compose)
|
||||||
- [Debounced](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Debounced)
|
- [Debounced](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Debounced)
|
||||||
- [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)
|
||||||
|
- [Pipeline](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Pipeline)
|
||||||
|
- [Schedule](https://github.com/duke-git/lancet/blob/v1/docs/function.md#Schedule)
|
||||||
- [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.
|
||||||
@@ -353,6 +357,7 @@ import "github.com/duke-git/lancet/strutil"
|
|||||||
- [UpperKebabCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#UpperKebabCase)
|
- [UpperKebabCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#UpperKebabCase)
|
||||||
- [LowerFirst](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#LowerFirst)
|
- [LowerFirst](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#LowerFirst)
|
||||||
- [UpperFirst](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#UpperFirst)
|
- [UpperFirst](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#UpperFirst)
|
||||||
|
- [Pad](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#Pad)
|
||||||
- [PadEnd](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#PadEnd)
|
- [PadEnd](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#PadEnd)
|
||||||
- [PadStart](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#PadStart)
|
- [PadStart](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#PadStart)
|
||||||
- [Reverse](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#Reverse)
|
- [Reverse](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#Reverse)
|
||||||
@@ -361,6 +366,8 @@ import "github.com/duke-git/lancet/strutil"
|
|||||||
- [SplitEx](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#SplitEx)
|
- [SplitEx](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#SplitEx)
|
||||||
- [Wrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#Wrap)
|
- [Wrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#Wrap)
|
||||||
- [Unwrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#Unwrap)
|
- [Unwrap](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#Unwrap)
|
||||||
|
- [SplitWords](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#SplitWords)
|
||||||
|
- [WordCount](https://github.com/duke-git/lancet/blob/v1/docs/strutil.md#WordCount)
|
||||||
|
|
||||||
### 12. System package contain some functions about os, runtime, shell command.
|
### 12. System package contain some functions about os, runtime, shell command.
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<br/>
|
<br/>
|
||||||
|
|
||||||

|

|
||||||
[](https://github.com/duke-git/lancet/releases)
|
[](https://github.com/duke-git/lancet/releases)
|
||||||
[](https://pkg.go.dev/github.com/duke-git/lancet)
|
[](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)
|
||||||
@@ -82,6 +82,8 @@ import "github.com/duke-git/lancet/convertor"
|
|||||||
- [StructToMap](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#StructToMap)
|
- [StructToMap](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#StructToMap)
|
||||||
- [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)
|
||||||
|
- [DeepClone](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#DeepClone)
|
||||||
|
- [CopyProperties](https://github.com/duke-git/lancet/blob/v1/docs/convertor_zh-CN.md#CopyProperties)
|
||||||
|
|
||||||
### 2. cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。
|
### 2. cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。
|
||||||
|
|
||||||
@@ -211,6 +213,8 @@ import "github.com/duke-git/lancet/function"
|
|||||||
- [Compose](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Compose)
|
- [Compose](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Compose)
|
||||||
- [Debounced](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Debounced)
|
- [Debounced](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Debounced)
|
||||||
- [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)
|
||||||
|
- [Pipeline](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Pipeline)
|
||||||
|
- [Schedule](https://github.com/duke-git/lancet/blob/v1/docs/function_zh-CN.md#Schedule)
|
||||||
- [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 包实现了一些数学计算的函数。
|
||||||
@@ -352,6 +356,7 @@ import "github.com/duke-git/lancet/strutil"
|
|||||||
- [UpperKebabCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#UpperKebabCase)
|
- [UpperKebabCase](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#UpperKebabCase)
|
||||||
- [LowerFirst](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#LowerFirst)
|
- [LowerFirst](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#LowerFirst)
|
||||||
- [UpperFirst](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#UpperFirst)
|
- [UpperFirst](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#UpperFirst)
|
||||||
|
- [Pad](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Pad)
|
||||||
- [PadEnd](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#PadEnd)
|
- [PadEnd](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#PadEnd)
|
||||||
- [PadStart](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#PadStart)
|
- [PadStart](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#PadStart)
|
||||||
- [Reverse](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Reverse)
|
- [Reverse](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#Reverse)
|
||||||
@@ -360,6 +365,8 @@ import "github.com/duke-git/lancet/strutil"
|
|||||||
- [SplitEx](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#SplitEx)
|
- [SplitEx](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#SplitEx)
|
||||||
- [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)
|
||||||
|
- [SplitWords](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#SplitWords)
|
||||||
|
- [WordCount](https://github.com/duke-git/lancet/blob/v1/docs/strutil_zh-CN.md#WordCount)
|
||||||
|
|
||||||
### 13. system 包含 os, runtime, shell command 相关函数。
|
### 13. system 包含 os, runtime, shell command 相关函数。
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -262,3 +263,59 @@ func DecodeByte(data []byte, target interface{}) error {
|
|||||||
decoder := gob.NewDecoder(buffer)
|
decoder := gob.NewDecoder(buffer)
|
||||||
return decoder.Decode(target)
|
return decoder.Decode(target)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeepClone creates a deep copy of passed item.
|
||||||
|
// can't clone unexported field of struct
|
||||||
|
func DeepClone(src interface{}) interface{} {
|
||||||
|
c := cloner{
|
||||||
|
ptrs: map[reflect.Type]map[uintptr]reflect.Value{},
|
||||||
|
}
|
||||||
|
result := c.clone(reflect.ValueOf(src))
|
||||||
|
if result.Kind() == reflect.Invalid {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.Interface()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CopyProperties copies each field from the source into the destination. It recursively copies struct pointers and interfaces that contain struct pointers.
|
||||||
|
func CopyProperties(dst, src interface{}) (err error) {
|
||||||
|
defer func() {
|
||||||
|
if e := recover(); e != nil {
|
||||||
|
err = errors.New(fmt.Sprintf("%v", e))
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
dstType, dstValue := reflect.TypeOf(dst), reflect.ValueOf(dst)
|
||||||
|
srcType, srcValue := reflect.TypeOf(src), reflect.ValueOf(src)
|
||||||
|
|
||||||
|
if dstType.Kind() != reflect.Ptr || dstType.Elem().Kind() != reflect.Struct {
|
||||||
|
return errors.New("CopyProperties: param dst should be struct pointer")
|
||||||
|
}
|
||||||
|
|
||||||
|
if srcType.Kind() == reflect.Ptr {
|
||||||
|
srcType, srcValue = srcType.Elem(), srcValue.Elem()
|
||||||
|
}
|
||||||
|
if srcType.Kind() != reflect.Struct {
|
||||||
|
return errors.New("CopyProperties: param src should be a struct or struct pointer")
|
||||||
|
}
|
||||||
|
|
||||||
|
dstType, dstValue = dstType.Elem(), dstValue.Elem()
|
||||||
|
|
||||||
|
propertyNums := dstType.NumField()
|
||||||
|
|
||||||
|
for i := 0; i < propertyNums; i++ {
|
||||||
|
property := dstType.Field(i)
|
||||||
|
propertyValue := srcValue.FieldByName(property.Name)
|
||||||
|
|
||||||
|
if !propertyValue.IsValid() || property.Type != propertyValue.Type() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if dstValue.Field(i).CanSet() {
|
||||||
|
dstValue.Field(i).Set(propertyValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
216
convertor/convertor_internal.go
Normal file
216
convertor/convertor_internal.go
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
// Copyright 2023 dudaodong@gmail.com. All rights reserved.
|
||||||
|
// Use of this source code is governed by MIT license
|
||||||
|
|
||||||
|
// Package convertor implements some functions to convert data.
|
||||||
|
package convertor
|
||||||
|
|
||||||
|
import "reflect"
|
||||||
|
|
||||||
|
type cloner struct {
|
||||||
|
ptrs map[reflect.Type]map[uintptr]reflect.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
// clone return a duplicate of passed item.
|
||||||
|
func (c *cloner) clone(v reflect.Value) reflect.Value {
|
||||||
|
switch v.Kind() {
|
||||||
|
case reflect.Invalid:
|
||||||
|
return reflect.ValueOf(nil)
|
||||||
|
|
||||||
|
// bool
|
||||||
|
case reflect.Bool:
|
||||||
|
return reflect.ValueOf(v.Bool())
|
||||||
|
|
||||||
|
//int
|
||||||
|
case reflect.Int:
|
||||||
|
return reflect.ValueOf(int(v.Int()))
|
||||||
|
case reflect.Int8:
|
||||||
|
return reflect.ValueOf(int8(v.Int()))
|
||||||
|
case reflect.Int16:
|
||||||
|
return reflect.ValueOf(int16(v.Int()))
|
||||||
|
case reflect.Int32:
|
||||||
|
return reflect.ValueOf(int32(v.Int()))
|
||||||
|
case reflect.Int64:
|
||||||
|
return reflect.ValueOf(v.Int())
|
||||||
|
|
||||||
|
// uint
|
||||||
|
case reflect.Uint:
|
||||||
|
return reflect.ValueOf(uint(v.Uint()))
|
||||||
|
case reflect.Uint8:
|
||||||
|
return reflect.ValueOf(uint8(v.Uint()))
|
||||||
|
case reflect.Uint16:
|
||||||
|
return reflect.ValueOf(uint16(v.Uint()))
|
||||||
|
case reflect.Uint32:
|
||||||
|
return reflect.ValueOf(uint32(v.Uint()))
|
||||||
|
case reflect.Uint64:
|
||||||
|
return reflect.ValueOf(v.Uint())
|
||||||
|
|
||||||
|
// float
|
||||||
|
case reflect.Float32:
|
||||||
|
return reflect.ValueOf(float32(v.Float()))
|
||||||
|
case reflect.Float64:
|
||||||
|
return reflect.ValueOf(v.Float())
|
||||||
|
|
||||||
|
// complex
|
||||||
|
case reflect.Complex64:
|
||||||
|
return reflect.ValueOf(complex64(v.Complex()))
|
||||||
|
case reflect.Complex128:
|
||||||
|
return reflect.ValueOf(v.Complex())
|
||||||
|
|
||||||
|
// string
|
||||||
|
case reflect.String:
|
||||||
|
return reflect.ValueOf(v.String())
|
||||||
|
|
||||||
|
// array
|
||||||
|
case reflect.Array, reflect.Slice:
|
||||||
|
return c.cloneArray(v)
|
||||||
|
|
||||||
|
// map
|
||||||
|
case reflect.Map:
|
||||||
|
return c.cloneMap(v)
|
||||||
|
|
||||||
|
// Ptr
|
||||||
|
case reflect.Ptr:
|
||||||
|
return c.clonePtr(v)
|
||||||
|
|
||||||
|
// struct
|
||||||
|
case reflect.Struct:
|
||||||
|
return c.cloneStruct(v)
|
||||||
|
|
||||||
|
// func
|
||||||
|
case reflect.Func:
|
||||||
|
return v
|
||||||
|
|
||||||
|
// interface
|
||||||
|
case reflect.Interface:
|
||||||
|
return c.clone(v.Elem())
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return reflect.Zero(v.Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cloner) cloneArray(v reflect.Value) reflect.Value {
|
||||||
|
if v.IsNil() {
|
||||||
|
return reflect.Zero(v.Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
arr := reflect.MakeSlice(v.Type(), v.Len(), v.Len())
|
||||||
|
|
||||||
|
for i := 0; i < v.Len(); i++ {
|
||||||
|
val := c.clone(v.Index(i))
|
||||||
|
|
||||||
|
if val.IsValid() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
item := arr.Index(i)
|
||||||
|
if !item.CanSet() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
item.Set(val.Convert(item.Type()))
|
||||||
|
}
|
||||||
|
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cloner) cloneMap(v reflect.Value) reflect.Value {
|
||||||
|
if v.IsNil() {
|
||||||
|
return reflect.Zero(v.Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
clonedMap := reflect.MakeMap(v.Type())
|
||||||
|
|
||||||
|
for _, key := range v.MapKeys() {
|
||||||
|
value := v.MapIndex(key)
|
||||||
|
clonedKey := c.clone(key)
|
||||||
|
clonedValue := c.clone(value)
|
||||||
|
|
||||||
|
if !isNillable(clonedKey) || !clonedKey.IsNil() {
|
||||||
|
clonedKey = clonedKey.Convert(key.Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isNillable(clonedValue) || !clonedValue.IsNil()) && clonedValue.IsValid() {
|
||||||
|
clonedValue = clonedValue.Convert(value.Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
if !clonedValue.IsValid() {
|
||||||
|
clonedValue = reflect.Zero(clonedMap.Type().Elem())
|
||||||
|
}
|
||||||
|
|
||||||
|
clonedMap.SetMapIndex(clonedKey, clonedValue)
|
||||||
|
}
|
||||||
|
|
||||||
|
return clonedMap
|
||||||
|
}
|
||||||
|
|
||||||
|
func isNillable(v reflect.Value) bool {
|
||||||
|
switch v.Kind() {
|
||||||
|
case reflect.Chan, reflect.Interface, reflect.Ptr, reflect.Func:
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cloner) clonePtr(v reflect.Value) reflect.Value {
|
||||||
|
if v.IsNil() {
|
||||||
|
return reflect.Zero(v.Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
var newVal reflect.Value
|
||||||
|
|
||||||
|
if v.Elem().CanAddr() {
|
||||||
|
ptrs, exists := c.ptrs[v.Type()]
|
||||||
|
if exists {
|
||||||
|
if newVal, exists := ptrs[v.Elem().UnsafeAddr()]; exists {
|
||||||
|
return newVal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
newVal = c.clone(v.Elem())
|
||||||
|
|
||||||
|
if v.Elem().CanAddr() {
|
||||||
|
ptrs, exists := c.ptrs[v.Type()]
|
||||||
|
if exists {
|
||||||
|
if newVal, exists := ptrs[v.Elem().UnsafeAddr()]; exists {
|
||||||
|
return newVal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
clonedPtr := reflect.New(newVal.Type())
|
||||||
|
clonedPtr.Elem().Set(newVal)
|
||||||
|
|
||||||
|
return clonedPtr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cloner) cloneStruct(v reflect.Value) reflect.Value {
|
||||||
|
clonedStructPtr := reflect.New(v.Type())
|
||||||
|
clonedStruct := clonedStructPtr.Elem()
|
||||||
|
|
||||||
|
if v.CanAddr() {
|
||||||
|
ptrs := c.ptrs[clonedStructPtr.Type()]
|
||||||
|
if ptrs == nil {
|
||||||
|
ptrs = make(map[uintptr]reflect.Value)
|
||||||
|
c.ptrs[clonedStructPtr.Type()] = ptrs
|
||||||
|
}
|
||||||
|
ptrs[v.UnsafeAddr()] = clonedStructPtr
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < v.NumField(); i++ {
|
||||||
|
newStructValue := clonedStruct.Field(i)
|
||||||
|
if !newStructValue.CanSet() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
clonedVal := c.clone(v.Field(i))
|
||||||
|
if !clonedVal.IsValid() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
newStructValue.Set(clonedVal.Convert(newStructValue.Type()))
|
||||||
|
}
|
||||||
|
|
||||||
|
return clonedStruct
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package convertor
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/duke-git/lancet/internal"
|
"github.com/duke-git/lancet/internal"
|
||||||
@@ -213,3 +214,97 @@ func TestDecodeByte(t *testing.T) {
|
|||||||
DecodeByte(byteData, &obj)
|
DecodeByte(byteData, &obj)
|
||||||
assert.Equal("abc", obj)
|
assert.Equal("abc", obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDeepClone(t *testing.T) {
|
||||||
|
// assert := internal.NewAssert(t, "TestDeepClone")
|
||||||
|
|
||||||
|
type Struct struct {
|
||||||
|
Str string
|
||||||
|
Int int
|
||||||
|
Float float64
|
||||||
|
Bool bool
|
||||||
|
Nil interface{}
|
||||||
|
unexported string
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := []interface{}{
|
||||||
|
true,
|
||||||
|
1,
|
||||||
|
0.1,
|
||||||
|
map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
},
|
||||||
|
&Struct{
|
||||||
|
Str: "test",
|
||||||
|
Int: 1,
|
||||||
|
Float: 0.1,
|
||||||
|
Bool: true,
|
||||||
|
Nil: nil,
|
||||||
|
// unexported: "can't be cloned",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, item := range cases {
|
||||||
|
cloned := DeepClone(item)
|
||||||
|
|
||||||
|
t.Log(cloned)
|
||||||
|
if &cloned == &item {
|
||||||
|
t.Fatalf("[TestDeepClone case #%d failed]: equal pointer", i)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(item, cloned) {
|
||||||
|
t.Fatalf("[TestDeepClone case #%d failed] unequal objects", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCopyProperties(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestCopyProperties")
|
||||||
|
|
||||||
|
type Address struct {
|
||||||
|
Country string
|
||||||
|
ZipCode string
|
||||||
|
}
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
Role string
|
||||||
|
Addr Address
|
||||||
|
Hobbys []string
|
||||||
|
salary int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Employee struct {
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
Role string
|
||||||
|
Addr Address
|
||||||
|
Hobbys []string
|
||||||
|
salary int
|
||||||
|
}
|
||||||
|
|
||||||
|
user := User{Name: "user001", Age: 10, Role: "Admin", Addr: Address{Country: "CN", ZipCode: "001"}, Hobbys: []string{"a", "b"}, salary: 1000}
|
||||||
|
|
||||||
|
employee1 := Employee{}
|
||||||
|
|
||||||
|
err := CopyProperties(&employee1, &user)
|
||||||
|
|
||||||
|
assert.IsNil(err)
|
||||||
|
assert.Equal("user001", employee1.Name)
|
||||||
|
assert.Equal("Admin", employee1.Role)
|
||||||
|
assert.Equal("CN", employee1.Addr.Country)
|
||||||
|
assert.Equal(0, employee1.salary)
|
||||||
|
|
||||||
|
employee2 := Employee{Name: "employee001", Age: 20, Role: "User",
|
||||||
|
Addr: Address{Country: "UK", ZipCode: "002"}, salary: 500}
|
||||||
|
|
||||||
|
err = CopyProperties(&employee2, &user)
|
||||||
|
|
||||||
|
assert.IsNil(err)
|
||||||
|
assert.Equal("user001", employee2.Name)
|
||||||
|
assert.Equal("Admin", employee2.Role)
|
||||||
|
assert.Equal("CN", employee2.Addr.Country)
|
||||||
|
assert.Equal(500, employee2.salary)
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,14 +17,23 @@ import (
|
|||||||
// AesEcbEncrypt encrypt data with key use AES ECB algorithm
|
// AesEcbEncrypt encrypt data with key use AES ECB algorithm
|
||||||
// len(key) should be 16, 24 or 32
|
// len(key) should be 16, 24 or 32
|
||||||
func AesEcbEncrypt(data, key []byte) []byte {
|
func AesEcbEncrypt(data, key []byte) []byte {
|
||||||
cipher, _ := aes.NewCipher(generateAesKey(key))
|
size := len(key)
|
||||||
|
if size != 16 && size != 24 && size != 32 {
|
||||||
|
panic("key length shoud be 16 or 24 or 32")
|
||||||
|
}
|
||||||
|
|
||||||
|
cipher, _ := aes.NewCipher(generateAesKey(key, size))
|
||||||
length := (len(data) + aes.BlockSize) / aes.BlockSize
|
length := (len(data) + aes.BlockSize) / aes.BlockSize
|
||||||
|
|
||||||
plain := make([]byte, length*aes.BlockSize)
|
plain := make([]byte, length*aes.BlockSize)
|
||||||
|
|
||||||
copy(plain, data)
|
copy(plain, data)
|
||||||
|
|
||||||
pad := byte(len(plain) - len(data))
|
pad := byte(len(plain) - len(data))
|
||||||
for i := len(data); i < len(plain); i++ {
|
for i := len(data); i < len(plain); i++ {
|
||||||
plain[i] = pad
|
plain[i] = pad
|
||||||
}
|
}
|
||||||
|
|
||||||
encrypted := make([]byte, len(plain))
|
encrypted := make([]byte, len(plain))
|
||||||
for bs, be := 0, cipher.BlockSize(); bs <= len(data); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
|
for bs, be := 0, cipher.BlockSize(); bs <= len(data); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
|
||||||
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
|
cipher.Encrypt(encrypted[bs:be], plain[bs:be])
|
||||||
@@ -36,9 +45,14 @@ func AesEcbEncrypt(data, key []byte) []byte {
|
|||||||
// AesEcbDecrypt decrypt data with key use AES ECB algorithm
|
// AesEcbDecrypt decrypt data with key use AES ECB algorithm
|
||||||
// len(key) should be 16, 24 or 32
|
// len(key) should be 16, 24 or 32
|
||||||
func AesEcbDecrypt(encrypted, key []byte) []byte {
|
func AesEcbDecrypt(encrypted, key []byte) []byte {
|
||||||
cipher, _ := aes.NewCipher(generateAesKey(key))
|
size := len(key)
|
||||||
|
if size != 16 && size != 24 && size != 32 {
|
||||||
|
panic("key length shoud be 16 or 24 or 32")
|
||||||
|
}
|
||||||
|
|
||||||
|
cipher, _ := aes.NewCipher(generateAesKey(key, size))
|
||||||
decrypted := make([]byte, len(encrypted))
|
decrypted := make([]byte, len(encrypted))
|
||||||
//
|
|
||||||
for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
|
for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
|
||||||
cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
|
cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,15 +2,15 @@ package cryptor
|
|||||||
|
|
||||||
import "bytes"
|
import "bytes"
|
||||||
|
|
||||||
func generateAesKey(key []byte) []byte {
|
func generateAesKey(key []byte, size int) []byte {
|
||||||
genKey := make([]byte, 16)
|
aesKey := make([]byte, size)
|
||||||
copy(genKey, key)
|
copy(aesKey, key)
|
||||||
for i := 16; i < len(key); {
|
for i := size; i < len(key); {
|
||||||
for j := 0; j < 16 && i < len(key); j, i = j+1, i+1 {
|
for j := 0; j < size && i < len(key); j, i = j+1, i+1 {
|
||||||
genKey[j] ^= key[i]
|
aesKey[j] ^= key[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return genKey
|
return aesKey
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateDesKey(key []byte) []byte {
|
func generateDesKey(key []byte) []byte {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Convertor
|
# Convertor
|
||||||
|
|
||||||
Package convertor contains some functions for data type convertion.
|
Package convertor contains some functions for data type convertion.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -10,6 +11,7 @@ Package convertor contains some functions for data type convertion.
|
|||||||
<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/convertor"
|
"github.com/duke-git/lancet/convertor"
|
||||||
@@ -33,14 +35,14 @@ import (
|
|||||||
- [StructToMap](#StructToMap)
|
- [StructToMap](#StructToMap)
|
||||||
- [EncodeByte](#EncodeByte)
|
- [EncodeByte](#EncodeByte)
|
||||||
- [DecodeByte](#DecodeByte)
|
- [DecodeByte](#DecodeByte)
|
||||||
|
- [DeepClone](#DeepClone)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ColorHexToRGB">ColorHexToRGB</span>
|
### <span id="ColorHexToRGB">ColorHexToRGB</span>
|
||||||
|
|
||||||
<p>Convert color hex to color rgb.</p>
|
<p>Convert color hex to color rgb.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -48,6 +50,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ColorHexToRGB(colorHex string) (red, green, blue int)
|
func ColorHexToRGB(colorHex string) (red, green, blue int)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -65,8 +68,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ColorRGBToHex">ColorRGBToHex</span>
|
### <span id="ColorRGBToHex">ColorRGBToHex</span>
|
||||||
|
|
||||||
<p>Convert color rgb to color hex.</p>
|
<p>Convert color rgb to color hex.</p>
|
||||||
@@ -76,6 +77,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ColorRGBToHex(red, green, blue int) string
|
func ColorRGBToHex(red, green, blue int) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -96,8 +98,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToBool">ToBool</span>
|
### <span id="ToBool">ToBool</span>
|
||||||
|
|
||||||
<p>Convert string to a boolean value. Use strconv.ParseBool</p>
|
<p>Convert string to a boolean value. Use strconv.ParseBool</p>
|
||||||
@@ -107,6 +107,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToBool(s string) (bool, error)
|
func ToBool(s string) (bool, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -132,8 +133,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToBytes">ToBytes</span>
|
### <span id="ToBytes">ToBytes</span>
|
||||||
|
|
||||||
<p>Convert interface to byte slice.</p>
|
<p>Convert interface to byte slice.</p>
|
||||||
@@ -143,6 +142,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToBytes(data interface{}) ([]byte, error)
|
func ToBytes(data interface{}) ([]byte, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -162,8 +162,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToChar">ToChar</span>
|
### <span id="ToChar">ToChar</span>
|
||||||
|
|
||||||
<p>Convert string to char slice.</p>
|
<p>Convert string to char slice.</p>
|
||||||
@@ -173,6 +171,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToChar(s string) []string
|
func ToChar(s string) []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -195,7 +194,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToChannel">ToChannel</span>
|
### <span id="ToChannel">ToChannel</span>
|
||||||
|
|
||||||
<p>Convert a collection of elements to a read-only channels.</p>
|
<p>Convert a collection of elements to a read-only channels.</p>
|
||||||
@@ -205,6 +203,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToChannel(array []interface{}) <-chan interface{}
|
func ToChannel(array []interface{}) <-chan interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -232,8 +231,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToFloat">ToFloat</span>
|
### <span id="ToFloat">ToFloat</span>
|
||||||
|
|
||||||
<p>Convert interface to a float64 value. If param is a invalid floatable, will return 0 and error. </p>
|
<p>Convert interface to a float64 value. If param is a invalid floatable, will return 0 and error. </p>
|
||||||
@@ -243,6 +240,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToFloat(value interface{}) (float64, error)
|
func ToFloat(value interface{}) (float64, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -265,8 +263,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToInt">ToInt</span>
|
### <span id="ToInt">ToInt</span>
|
||||||
|
|
||||||
<p>Convert interface to a int64 value. If param is a invalid intable, will return 0 and error. </p>
|
<p>Convert interface to a int64 value. If param is a invalid intable, will return 0 and error. </p>
|
||||||
@@ -276,6 +272,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToInt(value interface{}) (int64, error)
|
func ToInt(value interface{}) (int64, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -298,8 +295,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToJson">ToJson</span>
|
### <span id="ToJson">ToJson</span>
|
||||||
|
|
||||||
<p>Convert interface to json string. If param can't be converted, will return "" and error. </p>
|
<p>Convert interface to json string. If param can't be converted, will return "" and error. </p>
|
||||||
@@ -309,6 +304,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToJson(value interface{}) (string, error)
|
func ToJson(value interface{}) (string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -326,8 +322,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToString">ToString</span>
|
### <span id="ToString">ToString</span>
|
||||||
|
|
||||||
<p>Convert interface to string. </p>
|
<p>Convert interface to string. </p>
|
||||||
@@ -337,6 +331,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToString(value interface{}) string
|
func ToString(value interface{}) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -354,8 +349,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="StructToMap">StructToMap</span>
|
### <span id="StructToMap">StructToMap</span>
|
||||||
|
|
||||||
<p>Convert struct to map, only convert exported field, struct field tag `json` should be set.</p>
|
<p>Convert struct to map, only convert exported field, struct field tag `json` should be set.</p>
|
||||||
@@ -365,6 +358,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func StructToMap(value interface{}) (map[string]interface{}, error)
|
func StructToMap(value interface{}) (map[string]interface{}, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -390,7 +384,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="EncodeByte">EncodeByte</span>
|
### <span id="EncodeByte">EncodeByte</span>
|
||||||
|
|
||||||
<p>Encode data to byte slice.</p>
|
<p>Encode data to byte slice.</p>
|
||||||
@@ -400,6 +393,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EncodeByte(data any) ([]byte, error)
|
func EncodeByte(data any) ([]byte, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -416,8 +410,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DecodeByte">DecodeByte</span>
|
### <span id="DecodeByte">DecodeByte</span>
|
||||||
|
|
||||||
<p>Decode byte data to target object. target should be a pointer instance.</p>
|
<p>Decode byte data to target object. target should be a pointer instance.</p>
|
||||||
@@ -427,6 +419,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func DecodeByte(data []byte, target any) error
|
func DecodeByte(data []byte, target any) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -444,3 +437,67 @@ func main() {
|
|||||||
fmt.Println(result) //"abc"
|
fmt.Println(result) //"abc"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="DeepClone">DeepClone</span>
|
||||||
|
|
||||||
|
<p>Creates a deep copy of passed item, can't clone unexported field of struct.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DeepClone[T any](src T) T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type Struct struct {
|
||||||
|
Str string
|
||||||
|
Int int
|
||||||
|
Float float64
|
||||||
|
Bool bool
|
||||||
|
Nil interface{}
|
||||||
|
unexported string
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := []interface{}{
|
||||||
|
true,
|
||||||
|
1,
|
||||||
|
0.1,
|
||||||
|
map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
},
|
||||||
|
&Struct{
|
||||||
|
Str: "test",
|
||||||
|
Int: 1,
|
||||||
|
Float: 0.1,
|
||||||
|
Bool: true,
|
||||||
|
Nil: nil,
|
||||||
|
// unexported: "can't be cloned",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, item := range cases {
|
||||||
|
cloned := convertor.DeepClone(item)
|
||||||
|
|
||||||
|
isPointerEqual := &cloned == &item
|
||||||
|
fmt.Println(cloned, isPointerEqual)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true false
|
||||||
|
// 1 false
|
||||||
|
// 0.1 false
|
||||||
|
// map[a:1 b:2] false
|
||||||
|
// &{test 1 0.1 true <nil> } false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Convertor
|
# Convertor
|
||||||
|
|
||||||
convertor 转换器包支持一些常见的数据类型转换
|
convertor 转换器包支持一些常见的数据类型转换
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -34,14 +35,14 @@ import (
|
|||||||
- [StructToMap](#StructToMap)
|
- [StructToMap](#StructToMap)
|
||||||
- [EncodeByte](#EncodeByte)
|
- [EncodeByte](#EncodeByte)
|
||||||
- [DecodeByte](#DecodeByte)
|
- [DecodeByte](#DecodeByte)
|
||||||
|
- [DeepClone](#DeepClone)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ColorHexToRGB">ColorHexToRGB</span>
|
### <span id="ColorHexToRGB">ColorHexToRGB</span>
|
||||||
|
|
||||||
<p>颜色值十六进制转rgb</p>
|
<p>颜色值十六进制转rgb</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -49,6 +50,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ColorHexToRGB(colorHex string) (red, green, blue int)
|
func ColorHexToRGB(colorHex string) (red, green, blue int)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -66,8 +68,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ColorRGBToHex">ColorRGBToHex</span>
|
### <span id="ColorRGBToHex">ColorRGBToHex</span>
|
||||||
|
|
||||||
<p>颜色值rgb转十六进制</p>
|
<p>颜色值rgb转十六进制</p>
|
||||||
@@ -77,6 +77,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ColorRGBToHex(red, green, blue int) string
|
func ColorRGBToHex(red, green, blue int) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -97,8 +98,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToBool">ToBool</span>
|
### <span id="ToBool">ToBool</span>
|
||||||
|
|
||||||
<p>字符串转布尔类型,使用strconv.ParseBool</p>
|
<p>字符串转布尔类型,使用strconv.ParseBool</p>
|
||||||
@@ -108,6 +107,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToBool(s string) (bool, error)
|
func ToBool(s string) (bool, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -133,8 +133,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToBytes">ToBytes</span>
|
### <span id="ToBytes">ToBytes</span>
|
||||||
|
|
||||||
<p>interface转字节切片.</p>
|
<p>interface转字节切片.</p>
|
||||||
@@ -144,6 +142,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToBytes(data interface{}) ([]byte, error)
|
func ToBytes(data interface{}) ([]byte, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -163,8 +162,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToChar">ToChar</span>
|
### <span id="ToChar">ToChar</span>
|
||||||
|
|
||||||
<p>字符串转字符切片</p>
|
<p>字符串转字符切片</p>
|
||||||
@@ -174,6 +171,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToChar(s string) []string
|
func ToChar(s string) []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -196,8 +194,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToChannel">ToChannel</span>
|
### <span id="ToChannel">ToChannel</span>
|
||||||
|
|
||||||
<p>将切片转为只读channel</p>
|
<p>将切片转为只读channel</p>
|
||||||
@@ -207,6 +203,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToChannel(array []interface{}) <-chan interface{}
|
func ToChannel(array []interface{}) <-chan interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -234,8 +231,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToFloat">ToFloat</span>
|
### <span id="ToFloat">ToFloat</span>
|
||||||
|
|
||||||
<p>将interface转成float64类型,如果参数无法转换,会返回0和error</p>
|
<p>将interface转成float64类型,如果参数无法转换,会返回0和error</p>
|
||||||
@@ -245,6 +240,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToFloat(value interface{}) (float64, error)
|
func ToFloat(value interface{}) (float64, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -267,8 +263,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToInt">ToInt</span>
|
### <span id="ToInt">ToInt</span>
|
||||||
|
|
||||||
<p>将interface转成intt64类型,如果参数无法转换,会返回0和error</p>
|
<p>将interface转成intt64类型,如果参数无法转换,会返回0和error</p>
|
||||||
@@ -278,6 +272,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToInt(value interface{}) (int64, error)
|
func ToInt(value interface{}) (int64, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -300,8 +295,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToJson">ToJson</span>
|
### <span id="ToJson">ToJson</span>
|
||||||
|
|
||||||
<p>将interface转成json字符串,如果参数无法转换,会返回""和error</p>
|
<p>将interface转成json字符串,如果参数无法转换,会返回""和error</p>
|
||||||
@@ -311,6 +304,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToJson(value interface{}) (string, error)
|
func ToJson(value interface{}) (string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -328,8 +322,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToString">ToString</span>
|
### <span id="ToString">ToString</span>
|
||||||
|
|
||||||
<p>将interface转成字符串</p>
|
<p>将interface转成字符串</p>
|
||||||
@@ -339,6 +331,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToString(value interface{}) string
|
func ToString(value interface{}) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -356,8 +349,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="StructToMap">StructToMap</span>
|
### <span id="StructToMap">StructToMap</span>
|
||||||
|
|
||||||
<p>将struct转成map,只会转换struct中可导出的字段。struct中导出字段需要设置json tag标记</p>
|
<p>将struct转成map,只会转换struct中可导出的字段。struct中导出字段需要设置json tag标记</p>
|
||||||
@@ -367,6 +358,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func StructToMap(value interface{}) (map[string]interface{}, error)
|
func StructToMap(value interface{}) (map[string]interface{}, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -392,7 +384,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="EncodeByte">EncodeByte</span>
|
### <span id="EncodeByte">EncodeByte</span>
|
||||||
|
|
||||||
<p>将data编码成字节切片</p>
|
<p>将data编码成字节切片</p>
|
||||||
@@ -402,6 +393,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EncodeByte(data any) ([]byte, error)
|
func EncodeByte(data any) ([]byte, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -418,8 +410,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DecodeByte">DecodeByte</span>
|
### <span id="DecodeByte">DecodeByte</span>
|
||||||
|
|
||||||
<p>解码字节切片到目标对象,目标对象需要传入一个指针实例子</p>
|
<p>解码字节切片到目标对象,目标对象需要传入一个指针实例子</p>
|
||||||
@@ -429,6 +419,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func DecodeByte(data []byte, target any) error
|
func DecodeByte(data []byte, target any) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -446,3 +437,67 @@ func main() {
|
|||||||
fmt.Println(result) //"abc"
|
fmt.Println(result) //"abc"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="DeepClone">DeepClone</span>
|
||||||
|
|
||||||
|
<p>创建一个传入值的深拷贝, 无法克隆结构体的非导出字段。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func DeepClone[T any](src T) T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type Struct struct {
|
||||||
|
Str string
|
||||||
|
Int int
|
||||||
|
Float float64
|
||||||
|
Bool bool
|
||||||
|
Nil interface{}
|
||||||
|
unexported string
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := []interface{}{
|
||||||
|
true,
|
||||||
|
1,
|
||||||
|
0.1,
|
||||||
|
map[string]int{
|
||||||
|
"a": 1,
|
||||||
|
"b": 2,
|
||||||
|
},
|
||||||
|
&Struct{
|
||||||
|
Str: "test",
|
||||||
|
Int: 1,
|
||||||
|
Float: 0.1,
|
||||||
|
Bool: true,
|
||||||
|
Nil: nil,
|
||||||
|
// unexported: "can't be cloned",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, item := range cases {
|
||||||
|
cloned := convertor.DeepClone(item)
|
||||||
|
|
||||||
|
isPointerEqual := &cloned == &item
|
||||||
|
fmt.Println(cloned, isPointerEqual)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true false
|
||||||
|
// 1 false
|
||||||
|
// 0.1 false
|
||||||
|
// map[a:1 b:2] false
|
||||||
|
// &{test 1 0.1 true <nil> } false
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Cryptor
|
# Cryptor
|
||||||
|
|
||||||
Package cryptor contains some functions for data encryption and decryption. Support base64, md5, hmac, aes, des, rsa.
|
Package cryptor contains some functions for data encryption and decryption. Support base64, md5, hmac, aes, des, rsa.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -13,6 +14,7 @@ Package cryptor contains some functions for data encryption and decryption. Supp
|
|||||||
<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/cryptor"
|
"github.com/duke-git/lancet/cryptor"
|
||||||
@@ -56,13 +58,10 @@ import (
|
|||||||
- [RsaEncrypt](#RsaEncrypt)
|
- [RsaEncrypt](#RsaEncrypt)
|
||||||
- [RsaDecrypt](#RsaDecrypt)
|
- [RsaDecrypt](#RsaDecrypt)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesEcbEncrypt">AesEcbEncrypt</span>
|
### <span id="AesEcbEncrypt">AesEcbEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Encrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -72,6 +71,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func AesEcbEncrypt(data, key []byte) []byte
|
func AesEcbEncrypt(data, key []byte) []byte
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -91,8 +91,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesEcbDecrypt">AesEcbDecrypt</span>
|
### <span id="AesEcbDecrypt">AesEcbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Decrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -102,6 +100,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AesEcbDecrypt(encrypted, key []byte) []byte
|
func AesEcbDecrypt(encrypted, key []byte) []byte
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -121,8 +120,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCbcEncrypt">AesCbcEncrypt</span>
|
### <span id="AesCbcEncrypt">AesCbcEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Encrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -132,6 +129,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AesCbcEncrypt(data, key []byte) []byte
|
func AesCbcEncrypt(data, key []byte) []byte
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -151,8 +149,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCbcDecrypt">AesCbcDecrypt</span>
|
### <span id="AesCbcDecrypt">AesCbcDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -182,8 +178,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCtrCrypt">AesCtrCrypt</span>
|
### <span id="AesCtrCrypt">AesCtrCrypt</span>
|
||||||
|
|
||||||
<p>Encrypt or decrypt data with key use AES CTR algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Encrypt or decrypt data with key use AES CTR algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -214,8 +208,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCfbEncrypt">AesCfbEncrypt</span>
|
### <span id="AesCfbEncrypt">AesCfbEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use AES CFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Encrypt data with key use AES CFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -244,8 +236,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCfbDecrypt">AesCfbDecrypt</span>
|
### <span id="AesCfbDecrypt">AesCfbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -275,8 +265,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesOfbEncrypt">AesOfbEncrypt</span>
|
### <span id="AesOfbEncrypt">AesOfbEncrypt</span>
|
||||||
|
|
||||||
<p>Enecrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Enecrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -305,8 +293,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCfbDecrypt">AesOfbDecrypt</span>
|
### <span id="AesCfbDecrypt">AesOfbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Decrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -337,8 +323,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Base64StdEncode">Base64StdEncode</span>
|
### <span id="Base64StdEncode">Base64StdEncode</span>
|
||||||
|
|
||||||
<p>Encode string with base64 encoding.</p>
|
<p>Encode string with base64 encoding.</p>
|
||||||
@@ -348,6 +332,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Base64StdEncode(s string) string
|
func Base64StdEncode(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -364,8 +349,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Base64StdDecode">Base64StdDecode</span>
|
### <span id="Base64StdDecode">Base64StdDecode</span>
|
||||||
|
|
||||||
<p>Decode a base64 encoded string.</p>
|
<p>Decode a base64 encoded string.</p>
|
||||||
@@ -392,8 +375,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesEcbEncrypt">DesEcbEncrypt</span>
|
### <span id="DesEcbEncrypt">DesEcbEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p>
|
<p>Encrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -423,8 +404,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesEcbDecrypt">DesEcbDecrypt</span>
|
### <span id="DesEcbDecrypt">DesEcbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p>
|
<p>Decrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -455,8 +434,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCbcEncrypt">DesCbcEncrypt</span>
|
### <span id="DesCbcEncrypt">DesCbcEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
<p>Encrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -486,8 +463,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCbcDecrypt">DesCbcDecrypt</span>
|
### <span id="DesCbcDecrypt">DesCbcDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
<p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -518,8 +493,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCtrCrypt">DesCtrCrypt</span>
|
### <span id="DesCtrCrypt">DesCtrCrypt</span>
|
||||||
|
|
||||||
<p>Encrypt or decrypt data with key use DES CTR algorithm. Length of `key` param should be 8.</p>
|
<p>Encrypt or decrypt data with key use DES CTR algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -550,8 +523,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCfbEncrypt">DesCfbEncrypt</span>
|
### <span id="DesCfbEncrypt">DesCfbEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use DES CFB algorithm. Length of `key` param should be 8.</p>
|
<p>Encrypt data with key use DES CFB algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -580,8 +551,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCfbDecrypt">DesCfbDecrypt</span>
|
### <span id="DesCfbDecrypt">DesCfbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
<p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -611,8 +580,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesOfbEncrypt">DesOfbEncrypt</span>
|
### <span id="DesOfbEncrypt">DesOfbEncrypt</span>
|
||||||
|
|
||||||
<p>Enecrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
|
<p>Enecrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -641,8 +608,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesOfbDecrypt">DesOfbDecrypt</span>
|
### <span id="DesOfbDecrypt">DesOfbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
|
<p>Decrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -673,8 +638,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacMd5">HmacMd5</span>
|
### <span id="HmacMd5">HmacMd5</span>
|
||||||
|
|
||||||
<p>Get the md5 hmac hash of string.</p>
|
<p>Get the md5 hmac hash of string.</p>
|
||||||
@@ -701,8 +664,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacSha1">HmacSha1</span>
|
### <span id="HmacSha1">HmacSha1</span>
|
||||||
|
|
||||||
<p>Get the sha1 hmac hash of string.</p>
|
<p>Get the sha1 hmac hash of string.</p>
|
||||||
@@ -729,8 +690,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacSha256">HmacSha256</span>
|
### <span id="HmacSha256">HmacSha256</span>
|
||||||
|
|
||||||
<p>Get the sha256 hmac hash of string</p>
|
<p>Get the sha256 hmac hash of string</p>
|
||||||
@@ -757,8 +716,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacSha512">HmacSha512</span>
|
### <span id="HmacSha512">HmacSha512</span>
|
||||||
|
|
||||||
<p>Get the sha512 hmac hash of string.</p>
|
<p>Get the sha512 hmac hash of string.</p>
|
||||||
@@ -781,13 +738,10 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.HmacSha512("hello world", "12345"))
|
s := cryptor.HmacSha512("hello world", "12345"))
|
||||||
fmt.Println(s)
|
fmt.Println(s) //5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175
|
||||||
//5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Md5String">Md5String</span>
|
### <span id="Md5String">Md5String</span>
|
||||||
|
|
||||||
<p>Get the md5 value of string.</p>
|
<p>Get the md5 value of string.</p>
|
||||||
@@ -814,8 +768,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Md5File">Md5File</span>
|
### <span id="Md5File">Md5File</span>
|
||||||
|
|
||||||
<p>Get the md5 value of file.</p>
|
<p>Get the md5 value of file.</p>
|
||||||
@@ -842,8 +794,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Sha1">Sha1</span>
|
### <span id="Sha1">Sha1</span>
|
||||||
|
|
||||||
<p>Get the sha1 value of string.</p>
|
<p>Get the sha1 value of string.</p>
|
||||||
@@ -870,8 +820,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Sha256">Sha256</span>
|
### <span id="Sha256">Sha256</span>
|
||||||
|
|
||||||
<p>Get the sha256 value of string.</p>
|
<p>Get the sha256 value of string.</p>
|
||||||
@@ -898,8 +846,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Sha512">Sha512</span>
|
### <span id="Sha512">Sha512</span>
|
||||||
|
|
||||||
<p>Get the sha512 value of string.</p>
|
<p>Get the sha512 value of string.</p>
|
||||||
@@ -926,8 +872,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GenerateRsaKey">GenerateRsaKey</span>
|
### <span id="GenerateRsaKey">GenerateRsaKey</span>
|
||||||
|
|
||||||
<p>Create the rsa public and private key file in current directory.</p>
|
<p>Create the rsa public and private key file in current directory.</p>
|
||||||
@@ -956,8 +900,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RsaEncrypt">RsaEncrypt</span>
|
### <span id="RsaEncrypt">RsaEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with public key file useing ras algorithm.</p>
|
<p>Encrypt data with public key file useing ras algorithm.</p>
|
||||||
@@ -992,8 +934,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RsaDecrypt">RsaDecrypt</span>
|
### <span id="RsaDecrypt">RsaDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with private key file useing ras algorithm.</p>
|
<p>Decrypt data with private key file useing ras algorithm.</p>
|
||||||
@@ -1027,6 +967,3 @@ func main() {
|
|||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Cryptor
|
# Cryptor
|
||||||
|
|
||||||
cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。
|
cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -13,6 +14,7 @@ cryptor加密包支持数据加密和解密,获取md5,hash值。支持base64
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/cryptor"
|
"github.com/duke-git/lancet/cryptor"
|
||||||
@@ -22,6 +24,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [AesEcbEncrypt](#AesEcbEncrypt)
|
- [AesEcbEncrypt](#AesEcbEncrypt)
|
||||||
- [AesEcbDecrypt](#AesEcbDecrypt)
|
- [AesEcbDecrypt](#AesEcbDecrypt)
|
||||||
- [AesCbcEncrypt](#AesCbcEncrypt)
|
- [AesCbcEncrypt](#AesCbcEncrypt)
|
||||||
@@ -55,13 +58,10 @@ import (
|
|||||||
- [RsaEncrypt](#RsaEncrypt)
|
- [RsaEncrypt](#RsaEncrypt)
|
||||||
- [RsaDecrypt](#RsaDecrypt)
|
- [RsaDecrypt](#RsaDecrypt)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesEcbEncrypt">AesEcbEncrypt</span>
|
### <span id="AesEcbEncrypt">AesEcbEncrypt</span>
|
||||||
|
|
||||||
<p>使用AES ECB算法模式加密数据. 参数`key`的长度是16, 24 or 32。</p>
|
<p>使用AES ECB算法模式加密数据. 参数`key`的长度是16, 24 or 32。</p>
|
||||||
@@ -71,6 +71,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func AesEcbEncrypt(data, key []byte) []byte
|
func AesEcbEncrypt(data, key []byte) []byte
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -90,8 +91,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesEcbDecrypt">AesEcbDecrypt</span>
|
### <span id="AesEcbDecrypt">AesEcbDecrypt</span>
|
||||||
|
|
||||||
<p>使用AES ECB算法模式解密数据. 参数`key`的长度是16, 24 or 32。
|
<p>使用AES ECB算法模式解密数据. 参数`key`的长度是16, 24 or 32。
|
||||||
@@ -101,6 +100,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AesEcbDecrypt(encrypted, key []byte) []byte
|
func AesEcbDecrypt(encrypted, key []byte) []byte
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -120,8 +120,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCbcEncrypt">AesCbcEncrypt</span>
|
### <span id="AesCbcEncrypt">AesCbcEncrypt</span>
|
||||||
|
|
||||||
<p>使用AES CBC算法模式加密数据. 参数`key`的长度是16, 24 or 32。</p>
|
<p>使用AES CBC算法模式加密数据. 参数`key`的长度是16, 24 or 32。</p>
|
||||||
@@ -131,6 +129,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AesCbcEncrypt(data, key []byte) []byte
|
func AesCbcEncrypt(data, key []byte) []byte
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -150,8 +149,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCbcDecrypt">AesCbcDecrypt</span>
|
### <span id="AesCbcDecrypt">AesCbcDecrypt</span>
|
||||||
|
|
||||||
<p>使用AES CBC算法模式解密数据. 参数`key`的长度是16, 24 or 32。</p>
|
<p>使用AES CBC算法模式解密数据. 参数`key`的长度是16, 24 or 32。</p>
|
||||||
@@ -182,8 +179,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCtrCrypt">AesCtrCrypt</span>
|
### <span id="AesCtrCrypt">AesCtrCrypt</span>
|
||||||
|
|
||||||
<p>使用AES CTR算法模式加密/解密数据. 参数`key`的长度是16, 24 or 32。</p>
|
<p>使用AES CTR算法模式加密/解密数据. 参数`key`的长度是16, 24 or 32。</p>
|
||||||
@@ -214,8 +209,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCfbEncrypt">AesCfbEncrypt</span>
|
### <span id="AesCfbEncrypt">AesCfbEncrypt</span>
|
||||||
|
|
||||||
<p>使用AES CFB算法模式加密数据. 参数`key`的长度是16, 24 or 32。</p>
|
<p>使用AES CFB算法模式加密数据. 参数`key`的长度是16, 24 or 32。</p>
|
||||||
@@ -244,8 +237,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCfbDecrypt">AesCfbDecrypt</span>
|
### <span id="AesCfbDecrypt">AesCfbDecrypt</span>
|
||||||
|
|
||||||
<p>使用AES CFB算法模式解密数据. 参数`key`的长度是16, 24 or 32。</p>
|
<p>使用AES CFB算法模式解密数据. 参数`key`的长度是16, 24 or 32。</p>
|
||||||
@@ -275,8 +266,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesOfbEncrypt">AesOfbEncrypt</span>
|
### <span id="AesOfbEncrypt">AesOfbEncrypt</span>
|
||||||
|
|
||||||
<p>使用AES OFB算法模式加密数据. 参数`key`的长度是16, 24 or 32</p>
|
<p>使用AES OFB算法模式加密数据. 参数`key`的长度是16, 24 or 32</p>
|
||||||
@@ -305,8 +294,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCfbDecrypt">AesOfbDecrypt</span>
|
### <span id="AesCfbDecrypt">AesOfbDecrypt</span>
|
||||||
|
|
||||||
<p>使用AES OFB算法模式解密数据. 参数`key`的长度是16, 24 or 32</p>
|
<p>使用AES OFB算法模式解密数据. 参数`key`的长度是16, 24 or 32</p>
|
||||||
@@ -336,8 +323,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Base64StdEncode">Base64StdEncode</span>
|
### <span id="Base64StdEncode">Base64StdEncode</span>
|
||||||
|
|
||||||
<p>将字符串base64编码</p>
|
<p>将字符串base64编码</p>
|
||||||
@@ -347,6 +332,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Base64StdEncode(s string) string
|
func Base64StdEncode(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>列子:</b>
|
<b>列子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -363,8 +349,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Base64StdDecode">Base64StdDecode</span>
|
### <span id="Base64StdDecode">Base64StdDecode</span>
|
||||||
|
|
||||||
<p>解码base64字符串</p>
|
<p>解码base64字符串</p>
|
||||||
@@ -391,8 +375,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesEcbEncrypt">DesEcbEncrypt</span>
|
### <span id="DesEcbEncrypt">DesEcbEncrypt</span>
|
||||||
|
|
||||||
<p>使用DES ECB算法模式加密数据. 参数`key`的长度是8</p>
|
<p>使用DES ECB算法模式加密数据. 参数`key`的长度是8</p>
|
||||||
@@ -422,8 +404,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesEcbDecrypt">DesEcbDecrypt</span>
|
### <span id="DesEcbDecrypt">DesEcbDecrypt</span>
|
||||||
|
|
||||||
<p>使用DES ECB算法模式解密数据. 参数`key`的长度是8</p>
|
<p>使用DES ECB算法模式解密数据. 参数`key`的长度是8</p>
|
||||||
@@ -454,8 +434,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCbcEncrypt">DesCbcEncrypt</span>
|
### <span id="DesCbcEncrypt">DesCbcEncrypt</span>
|
||||||
|
|
||||||
<p>使用DES CBC算法模式加密数据. 参数`key`的长度是8</p>
|
<p>使用DES CBC算法模式加密数据. 参数`key`的长度是8</p>
|
||||||
@@ -485,8 +463,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCbcDecrypt">DesCbcDecrypt</span>
|
### <span id="DesCbcDecrypt">DesCbcDecrypt</span>
|
||||||
|
|
||||||
<p>使用DES CBC算法模式解密数据. 参数`key`的长度是8</p>
|
<p>使用DES CBC算法模式解密数据. 参数`key`的长度是8</p>
|
||||||
@@ -517,8 +493,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCtrCrypt">DesCtrCrypt</span>
|
### <span id="DesCtrCrypt">DesCtrCrypt</span>
|
||||||
|
|
||||||
<p>使用DES CTR算法模式加密/解密数据. 参数`key`的长度是8</p>
|
<p>使用DES CTR算法模式加密/解密数据. 参数`key`的长度是8</p>
|
||||||
@@ -549,8 +523,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCfbEncrypt">DesCfbEncrypt</span>
|
### <span id="DesCfbEncrypt">DesCfbEncrypt</span>
|
||||||
|
|
||||||
<p>使用DES CFB算法模式加密数据. 参数`key`的长度是8</p>
|
<p>使用DES CFB算法模式加密数据. 参数`key`的长度是8</p>
|
||||||
@@ -579,8 +551,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCfbDecrypt">DesCfbDecrypt</span>
|
### <span id="DesCfbDecrypt">DesCfbDecrypt</span>
|
||||||
|
|
||||||
<p>使用DES CFB算法模式解密数据. 参数`key`的长度是8</p>
|
<p>使用DES CFB算法模式解密数据. 参数`key`的长度是8</p>
|
||||||
@@ -610,8 +580,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesOfbEncrypt">DesOfbEncrypt</span>
|
### <span id="DesOfbEncrypt">DesOfbEncrypt</span>
|
||||||
|
|
||||||
<p>使用DES OFB算法模式加密数据. 参数`key`的长度是8</p>
|
<p>使用DES OFB算法模式加密数据. 参数`key`的长度是8</p>
|
||||||
@@ -640,8 +608,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesOfbDecrypt">DesOfbDecrypt</span>
|
### <span id="DesOfbDecrypt">DesOfbDecrypt</span>
|
||||||
|
|
||||||
<p>使用DES OFB算法模式解密数据. 参数`key`的长度是8</p>
|
<p>使用DES OFB算法模式解密数据. 参数`key`的长度是8</p>
|
||||||
@@ -671,8 +637,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacMd5">HmacMd5</span>
|
### <span id="HmacMd5">HmacMd5</span>
|
||||||
|
|
||||||
<p>获取字符串md5 hmac值</p>
|
<p>获取字符串md5 hmac值</p>
|
||||||
@@ -699,8 +663,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacSha1">HmacSha1</span>
|
### <span id="HmacSha1">HmacSha1</span>
|
||||||
|
|
||||||
<p>获取字符串sha1 hmac值</p>
|
<p>获取字符串sha1 hmac值</p>
|
||||||
@@ -727,8 +689,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacSha256">HmacSha256</span>
|
### <span id="HmacSha256">HmacSha256</span>
|
||||||
|
|
||||||
<p>获取字符串sha256 hmac值</p>
|
<p>获取字符串sha256 hmac值</p>
|
||||||
@@ -755,8 +715,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacSha512">HmacSha512</span>
|
### <span id="HmacSha512">HmacSha512</span>
|
||||||
|
|
||||||
<p>获取字符串sha512 hmac值</p>
|
<p>获取字符串sha512 hmac值</p>
|
||||||
@@ -784,8 +742,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Md5String">Md5String</span>
|
### <span id="Md5String">Md5String</span>
|
||||||
|
|
||||||
<p>获取字符串md5值</p>
|
<p>获取字符串md5值</p>
|
||||||
@@ -812,8 +768,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Md5File">Md5File</span>
|
### <span id="Md5File">Md5File</span>
|
||||||
|
|
||||||
<p>获取文件md5值.</p>
|
<p>获取文件md5值.</p>
|
||||||
@@ -840,8 +794,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Sha1">Sha1</span>
|
### <span id="Sha1">Sha1</span>
|
||||||
|
|
||||||
<p>获取字符串sha1值</p>
|
<p>获取字符串sha1值</p>
|
||||||
@@ -868,8 +820,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Sha256">Sha256</span>
|
### <span id="Sha256">Sha256</span>
|
||||||
|
|
||||||
<p>获取字符串sha256值</p>
|
<p>获取字符串sha256值</p>
|
||||||
@@ -896,8 +846,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Sha512">Sha512</span>
|
### <span id="Sha512">Sha512</span>
|
||||||
|
|
||||||
<p>获取字符串sha512值</p>
|
<p>获取字符串sha512值</p>
|
||||||
@@ -924,8 +872,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GenerateRsaKey">GenerateRsaKey</span>
|
### <span id="GenerateRsaKey">GenerateRsaKey</span>
|
||||||
|
|
||||||
<p>在当前目录下创建rsa私钥文件和公钥文件</p>
|
<p>在当前目录下创建rsa私钥文件和公钥文件</p>
|
||||||
@@ -954,8 +900,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RsaEncrypt">RsaEncrypt</span>
|
### <span id="RsaEncrypt">RsaEncrypt</span>
|
||||||
|
|
||||||
<p>用公钥文件ras加密数据</p>
|
<p>用公钥文件ras加密数据</p>
|
||||||
@@ -981,6 +925,7 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data := []byte("hello world")
|
data := []byte("hello world")
|
||||||
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
||||||
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
||||||
@@ -988,8 +933,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RsaDecrypt">RsaDecrypt</span>
|
### <span id="RsaDecrypt">RsaDecrypt</span>
|
||||||
|
|
||||||
<p>用私钥文件rsa解密数据</p>
|
<p>用私钥文件rsa解密数据</p>
|
||||||
@@ -1015,12 +958,10 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data := []byte("hello world")
|
data := []byte("hello world")
|
||||||
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
||||||
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted)) //hello world
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
101
docs/datetime.md
101
docs/datetime.md
@@ -1,4 +1,5 @@
|
|||||||
# Datetime
|
# Datetime
|
||||||
|
|
||||||
Package datetime supports date and time format and compare.
|
Package datetime supports date and time format and compare.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -10,6 +11,7 @@ Package datetime supports date and time format and compare.
|
|||||||
<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/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
@@ -19,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [AddDay](#AddDay)
|
- [AddDay](#AddDay)
|
||||||
- [AddHour](#AddHour)
|
- [AddHour](#AddHour)
|
||||||
- [AddMinute](#AddMinute)
|
- [AddMinute](#AddMinute)
|
||||||
@@ -40,7 +43,6 @@ import (
|
|||||||
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
|
- [GetZeroHourTimestamp](#GetZeroHourTimestamp)
|
||||||
- [GetNightTimestamp](#GetNightTimestamp)
|
- [GetNightTimestamp](#GetNightTimestamp)
|
||||||
- [FormatTimeToStr](#FormatTimeToStr)
|
- [FormatTimeToStr](#FormatTimeToStr)
|
||||||
|
|
||||||
- [FormatStrToTime](#FormatStrToTime)
|
- [FormatStrToTime](#FormatStrToTime)
|
||||||
- [NewUnixNow](#NewUnixNow)
|
- [NewUnixNow](#NewUnixNow)
|
||||||
- [NewUnix](#NewUnix)
|
- [NewUnix](#NewUnix)
|
||||||
@@ -56,7 +58,9 @@ import (
|
|||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
## Note:
|
## Note:
|
||||||
|
|
||||||
1. 'format' string param in func FormatTimeToStr and FormatStrToTime function should be one of flows:
|
1. 'format' string param in func FormatTimeToStr and FormatStrToTime function should be one of flows:
|
||||||
|
|
||||||
- yyyy-mm-dd hh:mm:ss
|
- yyyy-mm-dd hh:mm:ss
|
||||||
- yyyy-mm-dd hh:mm
|
- yyyy-mm-dd hh:mm
|
||||||
- yyyy-mm-dd hh
|
- yyyy-mm-dd hh
|
||||||
@@ -76,8 +80,8 @@ import (
|
|||||||
- hh:mm:ss
|
- hh:mm:ss
|
||||||
- mm:ss
|
- mm:ss
|
||||||
|
|
||||||
|
|
||||||
### <span id="AddDay">AddDay</span>
|
### <span id="AddDay">AddDay</span>
|
||||||
|
|
||||||
<p>Add or sub days to time.</p>
|
<p>Add or sub days to time.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -85,6 +89,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func AddDay(t time.Time, day int64) time.Time
|
func AddDay(t time.Time, day int64) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -105,8 +110,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="AddHour">AddHour</span>
|
### <span id="AddHour">AddHour</span>
|
||||||
|
|
||||||
<p>Add or sub hours to time.</p>
|
<p>Add or sub hours to time.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -114,6 +119,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AddHour(t time.Time, hour int64) time.Time
|
func AddHour(t time.Time, hour int64) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -135,6 +141,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="AddMinute">AddMinute</span>
|
### <span id="AddMinute">AddMinute</span>
|
||||||
|
|
||||||
<p>Add or sub minutes to time.</p>
|
<p>Add or sub minutes to time.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -142,6 +149,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AddMinute(t time.Time, minute int64) time.Time
|
func AddMinute(t time.Time, minute int64) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -163,6 +171,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="BeginOfMinute">BeginOfMinute</span>
|
### <span id="BeginOfMinute">BeginOfMinute</span>
|
||||||
|
|
||||||
<p>Return beginning minute time of day.</p>
|
<p>Return beginning minute time of day.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -170,6 +179,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfMinute(t time.Time) time.Time
|
func BeginOfMinute(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -189,6 +199,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="BeginOfHour">BeginOfHour</span>
|
### <span id="BeginOfHour">BeginOfHour</span>
|
||||||
|
|
||||||
<p>Return zero time of day.</p>
|
<p>Return zero time of day.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -196,6 +207,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfHour(t time.Time) time.Time
|
func BeginOfHour(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -215,6 +227,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="BeginOfDay">BeginOfDay</span>
|
### <span id="BeginOfDay">BeginOfDay</span>
|
||||||
|
|
||||||
<p>Return begin time of day.</p>
|
<p>Return begin time of day.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -222,6 +235,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfDay(t time.Time) time.Time
|
func BeginOfDay(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -240,9 +254,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeginOfWeek">BeginOfWeek</span>
|
### <span id="BeginOfWeek">BeginOfWeek</span>
|
||||||
|
|
||||||
<p>Return beginning time of week, week begin from Sunday.</p>
|
<p>Return beginning time of week, week begin from Sunday.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -250,6 +263,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfWeek(t time.Time) time.Time
|
func BeginOfWeek(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -268,9 +282,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeginOfMonth">BeginOfMonth</span>
|
### <span id="BeginOfMonth">BeginOfMonth</span>
|
||||||
|
|
||||||
<p>Return beginning time of month</p>
|
<p>Return beginning time of month</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -278,6 +291,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfMonth(t time.Time) time.Time
|
func BeginOfMonth(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -296,8 +310,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeginOfYear">BeginOfYear</span>
|
### <span id="BeginOfYear">BeginOfYear</span>
|
||||||
|
|
||||||
<p>Return beginning time of year.</p>
|
<p>Return beginning time of year.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -305,6 +319,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfYear(t time.Time) time.Time
|
func BeginOfYear(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -323,9 +338,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EndOfMinute">EndOfMinute</span>
|
### <span id="EndOfMinute">EndOfMinute</span>
|
||||||
|
|
||||||
<p>Return end time minute of day.</p>
|
<p>Return end time minute of day.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -333,6 +347,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfMinute(t time.Time) time.Time
|
func EndOfMinute(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -352,6 +367,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="EndOfHour">EndOfHour</span>
|
### <span id="EndOfHour">EndOfHour</span>
|
||||||
|
|
||||||
<p>Return end time hour of day.</p>
|
<p>Return end time hour of day.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -359,6 +375,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfHour(t time.Time) time.Time
|
func EndOfHour(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -378,6 +395,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="EndOfDay">EndOfDay</span>
|
### <span id="EndOfDay">EndOfDay</span>
|
||||||
|
|
||||||
<p>Return end time hour of day.</p>
|
<p>Return end time hour of day.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -385,6 +403,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfDay(t time.Time) time.Time
|
func EndOfDay(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -403,9 +422,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EndOfWeek">EndOfWeek</span>
|
### <span id="EndOfWeek">EndOfWeek</span>
|
||||||
|
|
||||||
<p>Return end time of week, week end with Saturday.</p>
|
<p>Return end time of week, week end with Saturday.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -413,6 +431,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfWeek(t time.Time) time.Time
|
func EndOfWeek(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -431,9 +450,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EndOfMonth">EndOfMonth</span>
|
### <span id="EndOfMonth">EndOfMonth</span>
|
||||||
|
|
||||||
<p>Return end time of month</p>
|
<p>Return end time of month</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -441,6 +459,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfMonth(t time.Time) time.Time
|
func EndOfMonth(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -459,8 +478,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="EndOfYear">EndOfYear</span>
|
### <span id="EndOfYear">EndOfYear</span>
|
||||||
|
|
||||||
<p>Return beginning time of year.</p>
|
<p>Return beginning time of year.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -468,6 +487,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfYear(t time.Time) time.Time
|
func EndOfYear(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -486,8 +506,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <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>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -495,6 +515,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetNowDate() string
|
func GetNowDate() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -513,8 +534,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetNowTime">GetNowTime</span>
|
### <span id="GetNowTime">GetNowTime</span>
|
||||||
|
|
||||||
<p>Get current time string, format is hh:mm:ss.</p>
|
<p>Get current time string, format is hh:mm:ss.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -522,6 +543,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetNowTime() string
|
func GetNowTime() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -540,8 +562,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetNowDateTime">GetNowDateTime</span>
|
### <span id="GetNowDateTime">GetNowDateTime</span>
|
||||||
|
|
||||||
<p>Get current date time string, format is yyyy-mm-dd hh:mm:ss.</p>
|
<p>Get current date time string, format is yyyy-mm-dd hh:mm:ss.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -549,6 +571,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetNowDateTime() string
|
func GetNowDateTime() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -567,8 +590,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetZeroHourTimestamp">GetZeroHourTimestamp</span>
|
### <span id="GetZeroHourTimestamp">GetZeroHourTimestamp</span>
|
||||||
|
|
||||||
<p>Return timestamp of zero hour (timestamp of 00:00).</p>
|
<p>Return timestamp of zero hour (timestamp of 00:00).</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -576,6 +599,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetZeroHourTimestamp() int64
|
func GetZeroHourTimestamp() int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -594,8 +618,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetNightTimestamp">GetNightTimestamp</span>
|
### <span id="GetNightTimestamp">GetNightTimestamp</span>
|
||||||
|
|
||||||
<p>Return timestamp of zero hour (timestamp of 23:59).</p>
|
<p>Return timestamp of zero hour (timestamp of 23:59).</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -603,6 +627,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetNightTimestamp() int64
|
func GetNightTimestamp() int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -622,6 +647,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="FormatTimeToStr">FormatTimeToStr</span>
|
### <span id="FormatTimeToStr">FormatTimeToStr</span>
|
||||||
|
|
||||||
<p>Format time to string, `format` param specification see note 1.</p>
|
<p>Format time to string, `format` param specification see note 1.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -629,6 +655,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FormatTimeToStr(t time.Time, format string) string
|
func FormatTimeToStr(t time.Time, format string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -647,8 +674,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="FormatStrToTime">FormatStrToTime</span>
|
### <span id="FormatStrToTime">FormatStrToTime</span>
|
||||||
|
|
||||||
<p>Format string to time, `format` param specification see note 1.</p>
|
<p>Format string to time, `format` param specification see note 1.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -656,6 +683,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FormatStrToTime(str, format string) (time.Time, error)
|
func FormatStrToTime(str, format string) (time.Time, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -672,9 +700,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="NewUnixNow">NewUnixNow</span>
|
### <span id="NewUnixNow">NewUnixNow</span>
|
||||||
|
|
||||||
<p>Return unix timestamp of current time</p>
|
<p>Return unix timestamp of current time</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -685,6 +712,7 @@ type theTime struct {
|
|||||||
}
|
}
|
||||||
func NewUnixNow() *theTime
|
func NewUnixNow() *theTime
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -701,8 +729,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="NewUnix">NewUnix</span>
|
### <span id="NewUnix">NewUnix</span>
|
||||||
|
|
||||||
<p>Return unix timestamp of specified int64 value.</p>
|
<p>Return unix timestamp of specified int64 value.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -713,6 +741,7 @@ type theTime struct {
|
|||||||
}
|
}
|
||||||
func NewUnix(unix int64) *theTime
|
func NewUnix(unix int64) *theTime
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -729,9 +758,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="NewFormat">NewFormat</span>
|
### <span id="NewFormat">NewFormat</span>
|
||||||
|
|
||||||
<p>Return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss".</p>
|
<p>Return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss".</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -742,6 +770,7 @@ type theTime struct {
|
|||||||
}
|
}
|
||||||
func NewFormat(t string) (*theTime, error)
|
func NewFormat(t string) (*theTime, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -758,10 +787,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="NewISO8601">NewISO8601</span>
|
### <span id="NewISO8601">NewISO8601</span>
|
||||||
|
|
||||||
<p>Return unix timestamp of specified iso8601 time string.</p>
|
<p>Return unix timestamp of specified iso8601 time string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -772,6 +799,7 @@ type theTime struct {
|
|||||||
}
|
}
|
||||||
func NewISO8601(iso8601 string) (*theTime, error)
|
func NewISO8601(iso8601 string) (*theTime, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -788,9 +816,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToUnix">ToUnix</span>
|
### <span id="ToUnix">ToUnix</span>
|
||||||
|
|
||||||
<p>Return unix timestamp.</p>
|
<p>Return unix timestamp.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -798,6 +825,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (t *theTime) ToUnix() int64
|
func (t *theTime) ToUnix() int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -814,9 +842,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToFormat">ToFormat</span>
|
### <span id="ToFormat">ToFormat</span>
|
||||||
|
|
||||||
<p>Return time string 'yyyy-mm-dd hh:mm:ss'.</p>
|
<p>Return time string 'yyyy-mm-dd hh:mm:ss'.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -824,6 +851,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (t *theTime) ToFormat() string
|
func (t *theTime) ToFormat() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -840,9 +868,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToFormatForTpl">ToFormatForTpl</span>
|
### <span id="ToFormatForTpl">ToFormatForTpl</span>
|
||||||
|
|
||||||
<p>Return the time string which format is specified tpl.</p>
|
<p>Return the time string which format is specified tpl.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -850,6 +877,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (t *theTime) ToFormatForTpl(tpl string) string
|
func (t *theTime) ToFormatForTpl(tpl string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -867,8 +895,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToIso8601">ToIso8601</span>
|
### <span id="ToIso8601">ToIso8601</span>
|
||||||
|
|
||||||
<p>Return iso8601 time string.</p>
|
<p>Return iso8601 time string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -876,6 +904,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (t *theTime) ToIso8601() string
|
func (t *theTime) ToIso8601() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Datetime
|
# Datetime
|
||||||
|
|
||||||
datetime 日期时间处理包,格式化日期,比较日期。
|
datetime 日期时间处理包,格式化日期,比较日期。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -10,6 +11,7 @@ datetime日期时间处理包,格式化日期,比较日期。
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/datetime"
|
"github.com/duke-git/lancet/datetime"
|
||||||
@@ -19,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [AddDay](#AddDay)
|
- [AddDay](#AddDay)
|
||||||
- [AddHour](#AddHour)
|
- [AddHour](#AddHour)
|
||||||
- [AddMinute](#AddMinute)
|
- [AddMinute](#AddMinute)
|
||||||
@@ -55,7 +58,9 @@ import (
|
|||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
## 注:
|
## 注:
|
||||||
|
|
||||||
1. 方法 FormatTimeToStr 和 FormatStrToTime 中的 format 参数值需要传以下类型之一:
|
1. 方法 FormatTimeToStr 和 FormatStrToTime 中的 format 参数值需要传以下类型之一:
|
||||||
|
|
||||||
- yyyy-mm-dd hh:mm:ss
|
- yyyy-mm-dd hh:mm:ss
|
||||||
- yyyy-mm-dd hh:mm
|
- yyyy-mm-dd hh:mm
|
||||||
- yyyy-mm-dd hh
|
- yyyy-mm-dd hh
|
||||||
@@ -75,8 +80,8 @@ import (
|
|||||||
- hh:mm:ss
|
- hh:mm:ss
|
||||||
- mm:ss
|
- mm:ss
|
||||||
|
|
||||||
|
|
||||||
### <span id="AddDay">AddDay</span>
|
### <span id="AddDay">AddDay</span>
|
||||||
|
|
||||||
<p>将日期加/减天数</p>
|
<p>将日期加/减天数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -84,6 +89,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func AddDay(t time.Time, day int64) time.Time
|
func AddDay(t time.Time, day int64) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -104,8 +110,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="AddHour">AddHour</span>
|
### <span id="AddHour">AddHour</span>
|
||||||
|
|
||||||
<p>将日期加/减小时数</p>
|
<p>将日期加/减小时数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -113,6 +119,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AddHour(t time.Time, hour int64) time.Time
|
func AddHour(t time.Time, hour int64) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -134,6 +141,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="AddMinute">AddMinute</span>
|
### <span id="AddMinute">AddMinute</span>
|
||||||
|
|
||||||
<p>将日期加/减分钟数</p>
|
<p>将日期加/减分钟数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -141,6 +149,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AddMinute(t time.Time, minute int64) time.Time
|
func AddMinute(t time.Time, minute int64) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -162,6 +171,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="BeginOfMinute">BeginOfMinute</span>
|
### <span id="BeginOfMinute">BeginOfMinute</span>
|
||||||
|
|
||||||
<p>返回指定时间的分钟开始时间</p>
|
<p>返回指定时间的分钟开始时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -169,6 +179,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfMinute(t time.Time) time.Time
|
func BeginOfMinute(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -188,6 +199,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="BeginOfHour">BeginOfHour</span>
|
### <span id="BeginOfHour">BeginOfHour</span>
|
||||||
|
|
||||||
<p>返回指定时间的小时开始时间</p>
|
<p>返回指定时间的小时开始时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -195,6 +207,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfHour(t time.Time) time.Time
|
func BeginOfHour(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -214,6 +227,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="BeginOfDay">BeginOfDay</span>
|
### <span id="BeginOfDay">BeginOfDay</span>
|
||||||
|
|
||||||
<p>返回指定时间的当天开始时间</p>
|
<p>返回指定时间的当天开始时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -221,6 +235,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfDay(t time.Time) time.Time
|
func BeginOfDay(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -239,9 +254,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeginOfWeek">BeginOfWeek</span>
|
### <span id="BeginOfWeek">BeginOfWeek</span>
|
||||||
|
|
||||||
<p>返回指定时间的星期开始时间</p>
|
<p>返回指定时间的星期开始时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -249,6 +263,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfWeek(t time.Time) time.Time
|
func BeginOfWeek(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -267,9 +282,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeginOfMonth">BeginOfMonth</span>
|
### <span id="BeginOfMonth">BeginOfMonth</span>
|
||||||
|
|
||||||
<p>返回指定时间的当月开始时间</p>
|
<p>返回指定时间的当月开始时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -277,6 +291,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfMonth(t time.Time) time.Time
|
func BeginOfMonth(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -295,8 +310,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeginOfYear">BeginOfYear</span>
|
### <span id="BeginOfYear">BeginOfYear</span>
|
||||||
|
|
||||||
<p>返回指定时间的当年开始时间</p>
|
<p>返回指定时间的当年开始时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -304,6 +319,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeginOfYear(t time.Time) time.Time
|
func BeginOfYear(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -322,9 +338,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EndOfMinute">EndOfMinute</span>
|
### <span id="EndOfMinute">EndOfMinute</span>
|
||||||
|
|
||||||
<p>返回指定时间的分钟结束时间</p>
|
<p>返回指定时间的分钟结束时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -332,6 +347,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfMinute(t time.Time) time.Time
|
func EndOfMinute(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -351,6 +367,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="EndOfHour">EndOfHour</span>
|
### <span id="EndOfHour">EndOfHour</span>
|
||||||
|
|
||||||
<p>返回指定时间的小时结束时间</p>
|
<p>返回指定时间的小时结束时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -358,6 +375,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfHour(t time.Time) time.Time
|
func EndOfHour(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -377,6 +395,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="EndOfDay">EndOfDay</span>
|
### <span id="EndOfDay">EndOfDay</span>
|
||||||
|
|
||||||
<p>返回指定时间的当天结束时间.</p>
|
<p>返回指定时间的当天结束时间.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -384,6 +403,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfDay(t time.Time) time.Time
|
func EndOfDay(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -402,9 +422,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EndOfWeek">EndOfWeek</span>
|
### <span id="EndOfWeek">EndOfWeek</span>
|
||||||
|
|
||||||
<p>返回指定时间的星期结束时间</p>
|
<p>返回指定时间的星期结束时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -412,6 +431,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfWeek(t time.Time) time.Time
|
func EndOfWeek(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -430,9 +450,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EndOfMonth">EndOfMonth</span>
|
### <span id="EndOfMonth">EndOfMonth</span>
|
||||||
|
|
||||||
<p>返回指定时间的月份结束时间</p>
|
<p>返回指定时间的月份结束时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -440,6 +459,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfMonth(t time.Time) time.Time
|
func EndOfMonth(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -458,8 +478,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="EndOfYear">EndOfYear</span>
|
### <span id="EndOfYear">EndOfYear</span>
|
||||||
|
|
||||||
<p>返回指定时间的年份结束时间</p>
|
<p>返回指定时间的年份结束时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -467,6 +487,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EndOfYear(t time.Time) time.Time
|
func EndOfYear(t time.Time) time.Time
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -485,8 +506,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetNowDate">GetNowDate</span>
|
### <span id="GetNowDate">GetNowDate</span>
|
||||||
|
|
||||||
<p>获取当天日期,返回格式:yyyy-mm-dd</p>
|
<p>获取当天日期,返回格式:yyyy-mm-dd</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -494,6 +515,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetNowDate() string
|
func GetNowDate() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -512,8 +534,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetNowTime">GetNowTime</span>
|
### <span id="GetNowTime">GetNowTime</span>
|
||||||
|
|
||||||
<p>获取当时时间,返回格式:hh:mm:ss</p>
|
<p>获取当时时间,返回格式:hh:mm:ss</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -521,6 +543,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetNowTime() string
|
func GetNowTime() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -539,8 +562,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetNowDateTime">GetNowDateTime</span>
|
### <span id="GetNowDateTime">GetNowDateTime</span>
|
||||||
|
|
||||||
<p>获取当时日期和时间,返回格式:yyyy-mm-dd hh:mm:ss.</p>
|
<p>获取当时日期和时间,返回格式:yyyy-mm-dd hh:mm:ss.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -548,6 +571,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetNowDateTime() string
|
func GetNowDateTime() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -566,8 +590,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetZeroHourTimestamp">GetZeroHourTimestamp</span>
|
### <span id="GetZeroHourTimestamp">GetZeroHourTimestamp</span>
|
||||||
|
|
||||||
<p>获取零时时间戳(timestamp of 00:00).</p>
|
<p>获取零时时间戳(timestamp of 00:00).</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -575,6 +599,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetZeroHourTimestamp() int64
|
func GetZeroHourTimestamp() int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -593,8 +618,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetNightTimestamp">GetNightTimestamp</span>
|
### <span id="GetNightTimestamp">GetNightTimestamp</span>
|
||||||
|
|
||||||
<p>获取午夜时间戳(timestamp of 23:59).</p>
|
<p>获取午夜时间戳(timestamp of 23:59).</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -602,6 +627,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetNightTimestamp() int64
|
func GetNightTimestamp() int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -621,6 +647,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="FormatTimeToStr">FormatTimeToStr</span>
|
### <span id="FormatTimeToStr">FormatTimeToStr</span>
|
||||||
|
|
||||||
<p>将日期格式化成字符串,`format` 参数格式参考注<sup>1</sup></p>
|
<p>将日期格式化成字符串,`format` 参数格式参考注<sup>1</sup></p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -628,6 +655,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FormatTimeToStr(t time.Time, format string) string
|
func FormatTimeToStr(t time.Time, format string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -646,8 +674,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="FormatStrToTime">FormatStrToTime</span>
|
### <span id="FormatStrToTime">FormatStrToTime</span>
|
||||||
|
|
||||||
<p>将字符串格式化成时间,`format` 参数格式参考注<sup>1</sup></p>
|
<p>将字符串格式化成时间,`format` 参数格式参考注<sup>1</sup></p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -655,6 +683,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FormatStrToTime(str, format string) (time.Time, error)
|
func FormatStrToTime(str, format string) (time.Time, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -671,9 +700,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="NewUnixNow">NewUnixNow</span>
|
### <span id="NewUnixNow">NewUnixNow</span>
|
||||||
|
|
||||||
<p>创建一个当前时间的unix时间戳</p>
|
<p>创建一个当前时间的unix时间戳</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -684,6 +712,7 @@ type theTime struct {
|
|||||||
}
|
}
|
||||||
func NewUnixNow() *theTime
|
func NewUnixNow() *theTime
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -700,8 +729,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="NewUnix">NewUnix</span>
|
### <span id="NewUnix">NewUnix</span>
|
||||||
|
|
||||||
<p>创建一个unix时间戳</p>
|
<p>创建一个unix时间戳</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -712,6 +741,7 @@ type theTime struct {
|
|||||||
}
|
}
|
||||||
func NewUnix(unix int64) *theTime
|
func NewUnix(unix int64) *theTime
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -728,9 +758,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="NewFormat">NewFormat</span>
|
### <span id="NewFormat">NewFormat</span>
|
||||||
|
|
||||||
<p>创建一个yyyy-mm-dd hh:mm:ss格式时间字符串的unix时间戳</p>
|
<p>创建一个yyyy-mm-dd hh:mm:ss格式时间字符串的unix时间戳</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -741,6 +770,7 @@ type theTime struct {
|
|||||||
}
|
}
|
||||||
func NewFormat(t string) (*theTime, error)
|
func NewFormat(t string) (*theTime, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -757,10 +787,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="NewISO8601">NewISO8601</span>
|
### <span id="NewISO8601">NewISO8601</span>
|
||||||
|
|
||||||
<p>创建一个iso8601格式时间字符串的unix时间戳</p>
|
<p>创建一个iso8601格式时间字符串的unix时间戳</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -771,6 +799,7 @@ type theTime struct {
|
|||||||
}
|
}
|
||||||
func NewISO8601(iso8601 string) (*theTime, error)
|
func NewISO8601(iso8601 string) (*theTime, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -787,9 +816,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToUnix">ToUnix</span>
|
### <span id="ToUnix">ToUnix</span>
|
||||||
|
|
||||||
<p>返回unix时间戳</p>
|
<p>返回unix时间戳</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -797,6 +825,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (t *theTime) ToUnix() int64
|
func (t *theTime) ToUnix() int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -813,9 +842,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToFormat">ToFormat</span>
|
### <span id="ToFormat">ToFormat</span>
|
||||||
|
|
||||||
<p>返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串</p>
|
<p>返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -823,6 +851,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (t *theTime) ToFormat() string
|
func (t *theTime) ToFormat() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -839,9 +868,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToFormatForTpl">ToFormatForTpl</span>
|
### <span id="ToFormatForTpl">ToFormatForTpl</span>
|
||||||
|
|
||||||
<p>返回tpl格式指定的日期字符串</p>
|
<p>返回tpl格式指定的日期字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -849,6 +877,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (t *theTime) ToFormatForTpl(tpl string) string
|
func (t *theTime) ToFormatForTpl(tpl string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -866,8 +895,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToIso8601">ToIso8601</span>
|
### <span id="ToIso8601">ToIso8601</span>
|
||||||
|
|
||||||
<p>返回iso8601日期字符串</p>
|
<p>返回iso8601日期字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -875,6 +904,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (t *theTime) ToIso8601() string
|
func (t *theTime) ToIso8601() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -891,4 +921,3 @@ func main() {
|
|||||||
fmt.Println(ts) //"2006-01-02T23:04:05+08:00"
|
fmt.Println(ts) //"2006-01-02T23:04:05+08:00"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Fileutil
|
# Fileutil
|
||||||
|
|
||||||
Package fileutil implements some basic functions for file operations.
|
Package fileutil implements some basic functions for file operations.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -10,6 +11,7 @@ Package fileutil implements some basic functions for file operations.
|
|||||||
<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/fileutil"
|
"github.com/duke-git/lancet/fileutil"
|
||||||
@@ -19,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [ClearFile](#ClearFile)
|
- [ClearFile](#ClearFile)
|
||||||
- [CreateFile](#CreateFile)
|
- [CreateFile](#CreateFile)
|
||||||
- [CreateDir](#CreateDir)
|
- [CreateDir](#CreateDir)
|
||||||
@@ -39,9 +42,8 @@ import (
|
|||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ClearFile">ClearFile</span>
|
### <span id="ClearFile">ClearFile</span>
|
||||||
|
|
||||||
<p>Clear the file content, write empty string to the file.</p>
|
<p>Clear the file content, write empty string to the file.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -49,6 +51,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ClearFile(path string) error
|
func ClearFile(path string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -67,9 +70,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CreateDir">CreateDir</span>
|
### <span id="CreateDir">CreateDir</span>
|
||||||
|
|
||||||
<p>Create directory in absolute path. param `absPath` like /a/, /a/b/.</p>
|
<p>Create directory in absolute path. param `absPath` like /a/, /a/b/.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -77,6 +79,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CreateDir(absPath string) error
|
func CreateDir(absPath string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -93,9 +96,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CreateFile">CreateFile</span>
|
### <span id="CreateFile">CreateFile</span>
|
||||||
|
|
||||||
<p>Create file in path. return true if create succeed.</p>
|
<p>Create file in path. return true if create succeed.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -103,6 +105,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CreateFile(path string) bool
|
func CreateFile(path string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -119,8 +122,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="CopyFile">CopyFile</span>
|
### <span id="CopyFile">CopyFile</span>
|
||||||
|
|
||||||
<p>Copy src file to dest file. If dest file exist will overwrite it.</p>
|
<p>Copy src file to dest file. If dest file exist will overwrite it.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -128,6 +131,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CopyFile(srcFilePath string, dstFilePath string) error
|
func CopyFile(srcFilePath string, dstFilePath string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -146,9 +150,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="FileMode">FileMode</span>
|
### <span id="FileMode">FileMode</span>
|
||||||
|
|
||||||
<p>Return file mode infomation.</p>
|
<p>Return file mode infomation.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -156,6 +159,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FileMode(path string) (fs.FileMode, error)
|
func FileMode(path string) (fs.FileMode, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -175,9 +179,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="MiMeType">MiMeType</span>
|
### <span id="MiMeType">MiMeType</span>
|
||||||
|
|
||||||
<p>Get file mime type, 'file' param's type should be string or *os.File.</p>
|
<p>Get file mime type, 'file' param's type should be string or *os.File.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -185,6 +188,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func MiMeType(file interface{}) string
|
func MiMeType(file interface{}) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -206,10 +210,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsExist">IsExist</span>
|
### <span id="IsExist">IsExist</span>
|
||||||
|
|
||||||
<p>Checks if a file or directory exists.</p>
|
<p>Checks if a file or directory exists.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -217,6 +219,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsExist(path string) bool
|
func IsExist(path string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -234,9 +237,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsLink">IsLink</span>
|
### <span id="IsLink">IsLink</span>
|
||||||
|
|
||||||
<p>Checks if a file is symbol link or not.</p>
|
<p>Checks if a file is symbol link or not.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -244,6 +246,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsLink(path string) bool
|
func IsLink(path string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -260,9 +263,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsDir">IsDir</span>
|
### <span id="IsDir">IsDir</span>
|
||||||
|
|
||||||
<p>Checks if the path is directy or not.</p>
|
<p>Checks if the path is directy or not.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -270,6 +272,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsDir(path string) bool
|
func IsDir(path string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -289,9 +292,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ListFileNames">ListFileNames</span>
|
### <span id="ListFileNames">ListFileNames</span>
|
||||||
|
|
||||||
<p>List all file names in given path.</p>
|
<p>List all file names in given path.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -299,6 +301,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ListFileNames(path string) ([]string, error)
|
func ListFileNames(path string) ([]string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -315,9 +318,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RemoveFile">RemoveFile</span>
|
### <span id="RemoveFile">RemoveFile</span>
|
||||||
|
|
||||||
<p>Remove the file of path.</p>
|
<p>Remove the file of path.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -325,6 +327,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RemoveFile(path string) error
|
func RemoveFile(path string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -343,8 +346,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReadFileToString">ReadFileToString</span>
|
### <span id="ReadFileToString">ReadFileToString</span>
|
||||||
|
|
||||||
<p>Return string of file content.</p>
|
<p>Return string of file content.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -352,6 +355,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ReadFileToString(path string) (string, error)
|
func ReadFileToString(path string) (string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -375,9 +379,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReadFileByLine">ReadFileByLine</span>
|
### <span id="ReadFileByLine">ReadFileByLine</span>
|
||||||
|
|
||||||
<p>Read file line by line, and return slice of lines</p>
|
<p>Read file line by line, and return slice of lines</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -385,6 +388,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ReadFileByLine(path string)([]string, error)
|
func ReadFileByLine(path string)([]string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -409,9 +413,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Zip">Zip</span>
|
### <span id="Zip">Zip</span>
|
||||||
|
|
||||||
<p>Create a zip file of fpath, fpath could be a file or a directory.</p>
|
<p>Create a zip file of fpath, fpath could be a file or a directory.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -419,6 +422,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Zip(fpath string, destPath string) error
|
func Zip(fpath string, destPath string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -437,10 +441,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UnZip">UnZip</span>
|
### <span id="UnZip">UnZip</span>
|
||||||
|
|
||||||
<p>Unzip the file and save it to dest path.</p>
|
<p>Unzip the file and save it to dest path.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -448,6 +450,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UnZip(zipFile string, destPath string) error
|
func UnZip(zipFile string, destPath string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -465,8 +468,3 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Fileutil
|
# Fileutil
|
||||||
|
|
||||||
fileutil 包支持文件基本操作。
|
fileutil 包支持文件基本操作。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -10,6 +11,7 @@ fileutil包支持文件基本操作。
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/fileutil"
|
"github.com/duke-git/lancet/fileutil"
|
||||||
@@ -19,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [ClearFile](#ClearFile)
|
- [ClearFile](#ClearFile)
|
||||||
- [CreateFile](#CreateFile)
|
- [CreateFile](#CreateFile)
|
||||||
- [CreateDir](#CreateDir)
|
- [CreateDir](#CreateDir)
|
||||||
@@ -39,9 +42,8 @@ import (
|
|||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ClearFile">ClearFile</span>
|
### <span id="ClearFile">ClearFile</span>
|
||||||
|
|
||||||
<p>清空文件内容</p>
|
<p>清空文件内容</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -49,6 +51,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ClearFile(path string) error
|
func ClearFile(path string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -67,8 +70,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="CreateDir">CreateDir</span>
|
### <span id="CreateDir">CreateDir</span>
|
||||||
|
|
||||||
<p>使用绝对路径创建嵌套目录,例如/a/, /a/b/</p>
|
<p>使用绝对路径创建嵌套目录,例如/a/, /a/b/</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -76,6 +79,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CreateDir(absPath string) error
|
func CreateDir(absPath string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -92,9 +96,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CreateFile">CreateFile</span>
|
### <span id="CreateFile">CreateFile</span>
|
||||||
|
|
||||||
<p>创建文件,创建成功返回true, 否则返回false</p>
|
<p>创建文件,创建成功返回true, 否则返回false</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -102,6 +105,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CreateFile(path string) bool
|
func CreateFile(path string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -118,8 +122,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="CopyFile">CopyFile</span>
|
### <span id="CopyFile">CopyFile</span>
|
||||||
|
|
||||||
<p>拷贝文件,会覆盖原有的拷贝文件</p>
|
<p>拷贝文件,会覆盖原有的拷贝文件</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -127,6 +131,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CopyFile(srcFilePath string, dstFilePath string) error
|
func CopyFile(srcFilePath string, dstFilePath string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -145,9 +150,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="FileMode">FileMode</span>
|
### <span id="FileMode">FileMode</span>
|
||||||
|
|
||||||
<p>获取文件mode信息</p>
|
<p>获取文件mode信息</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -155,6 +159,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FileMode(path string) (fs.FileMode, error)
|
func FileMode(path string) (fs.FileMode, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -174,9 +179,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="MiMeType">MiMeType</span>
|
### <span id="MiMeType">MiMeType</span>
|
||||||
|
|
||||||
<p>获取文件mime类型, 'file'参数的类型必须是string或者*os.File</p>
|
<p>获取文件mime类型, 'file'参数的类型必须是string或者*os.File</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -184,6 +188,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func MiMeType(file interface{}) string
|
func MiMeType(file interface{}) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -205,10 +210,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsExist">IsExist</span>
|
### <span id="IsExist">IsExist</span>
|
||||||
|
|
||||||
<p>判断文件或目录是否存在</p>
|
<p>判断文件或目录是否存在</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -216,6 +219,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsExist(path string) bool
|
func IsExist(path string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -233,9 +237,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsLink">IsLink</span>
|
### <span id="IsLink">IsLink</span>
|
||||||
|
|
||||||
<p>判断文件是否是符号链接</p>
|
<p>判断文件是否是符号链接</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -243,6 +246,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsLink(path string) bool
|
func IsLink(path string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -259,9 +263,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsDir">IsDir</span>
|
### <span id="IsDir">IsDir</span>
|
||||||
|
|
||||||
<p>判断目录是否存在</p>
|
<p>判断目录是否存在</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -269,6 +272,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsDir(path string) bool
|
func IsDir(path string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -288,9 +292,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ListFileNames">ListFileNames</span>
|
### <span id="ListFileNames">ListFileNames</span>
|
||||||
|
|
||||||
<p>返回目录下所有文件名</p>
|
<p>返回目录下所有文件名</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -298,6 +301,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ListFileNames(path string) ([]string, error)
|
func ListFileNames(path string) ([]string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -314,9 +318,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RemoveFile">RemoveFile</span>
|
### <span id="RemoveFile">RemoveFile</span>
|
||||||
|
|
||||||
<p>删除文件</p>
|
<p>删除文件</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -324,6 +327,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RemoveFile(path string) error
|
func RemoveFile(path string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -342,8 +346,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReadFileToString">ReadFileToString</span>
|
### <span id="ReadFileToString">ReadFileToString</span>
|
||||||
|
|
||||||
<p>读取文件内容并返回字符串</p>
|
<p>读取文件内容并返回字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -351,6 +355,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ReadFileToString(path string) (string, error)
|
func ReadFileToString(path string) (string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -374,9 +379,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReadFileByLine">ReadFileByLine</span>
|
### <span id="ReadFileByLine">ReadFileByLine</span>
|
||||||
|
|
||||||
<p>按行读取文件内容,返回字符串切片包含每一行</p>
|
<p>按行读取文件内容,返回字符串切片包含每一行</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -384,6 +388,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ReadFileByLine(path string)([]string, error)
|
func ReadFileByLine(path string)([]string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -408,9 +413,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Zip">Zip</span>
|
### <span id="Zip">Zip</span>
|
||||||
|
|
||||||
<p>zip压缩文件, fpath参数可以是文件或目录</p>
|
<p>zip压缩文件, fpath参数可以是文件或目录</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -418,6 +422,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Zip(fpath string, destPath string) error
|
func Zip(fpath string, destPath string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -436,10 +441,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UnZip">UnZip</span>
|
### <span id="UnZip">UnZip</span>
|
||||||
|
|
||||||
<p>zip解压缩文件并保存在目录中</p>
|
<p>zip解压缩文件并保存在目录中</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -447,6 +450,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UnZip(zipFile string, destPath string) error
|
func UnZip(zipFile string, destPath string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -464,8 +468,3 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Formatter
|
# Formatter
|
||||||
|
|
||||||
formatter contains some functions for data formatting.
|
formatter contains some functions for data formatting.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -10,6 +11,7 @@ formatter contains some functions for data formatting.
|
|||||||
<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/formatter"
|
"github.com/duke-git/lancet/formatter"
|
||||||
@@ -19,15 +21,15 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [Comma](#Comma)
|
- [Comma](#Comma)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Comma">Comma</span>
|
### <span id="Comma">Comma</span>
|
||||||
|
|
||||||
<p>Add comma to number by every 3 numbers from right. ahead by symbol char.
|
<p>Add comma to number by every 3 numbers from right. ahead by symbol char.
|
||||||
Param should be number or numberic string.</p>
|
Param should be number or numberic string.</p>
|
||||||
|
|
||||||
@@ -36,6 +38,7 @@ Param should be number or numberic string.</p>
|
|||||||
```go
|
```go
|
||||||
func Comma(v interface{}, symbol string) string
|
func Comma(v interface{}, symbol string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Formatter
|
# Formatter
|
||||||
|
|
||||||
formatter 格式化器包含一些数据格式化处理方法。
|
formatter 格式化器包含一些数据格式化处理方法。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -10,6 +11,7 @@ formatter格式化器包含一些数据格式化处理方法。
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/formatter"
|
"github.com/duke-git/lancet/formatter"
|
||||||
@@ -19,15 +21,15 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [Comma](#Comma)
|
- [Comma](#Comma)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Comma">Comma</span>
|
### <span id="Comma">Comma</span>
|
||||||
|
|
||||||
<p>用逗号每隔3位分割数字/字符串,签名添加符号。参数必须是数字或者可以转为数字的字符串</p>
|
<p>用逗号每隔3位分割数字/字符串,签名添加符号。参数必须是数字或者可以转为数字的字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -35,6 +37,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func Comma(v interface{}, symbol string) string
|
func Comma(v interface{}, symbol string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Function
|
# Function
|
||||||
|
|
||||||
Package function can control the flow of function execution and support part of functional programming.
|
Package function can control the flow of function execution and support part of functional programming.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -11,6 +12,7 @@ Package function can control the flow of function execution and support part of
|
|||||||
<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/function"
|
"github.com/duke-git/lancet/function"
|
||||||
@@ -20,21 +22,23 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [After](#After)
|
- [After](#After)
|
||||||
- [Before](#Before)
|
- [Before](#Before)
|
||||||
- [Curry](#Curry)
|
- [Curry](#Curry)
|
||||||
- [Compose](#Compose)
|
- [Compose](#Compose)
|
||||||
- [Debounced](#Debounced)
|
- [Debounced](#Debounced)
|
||||||
- [Delay](#Delay)
|
- [Delay](#Delay)
|
||||||
|
- [Pipeline](#Pipeline)
|
||||||
|
- [Schedule](#Schedule)
|
||||||
- [Watcher](#Watcher)
|
- [Watcher](#Watcher)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="After">After</span>
|
### <span id="After">After</span>
|
||||||
|
|
||||||
<p>Creates a function that invokes given func once it's called n or more times.</p>
|
<p>Creates a function that invokes given func once it's called n or more times.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -42,6 +46,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -78,8 +83,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Before">Before</span>
|
### <span id="Before">Before</span>
|
||||||
|
|
||||||
<p>creates a function that invokes func once it's called less than n times.</p>
|
<p>creates a function that invokes func once it's called less than n times.</p>
|
||||||
@@ -89,6 +92,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -124,8 +128,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Curry">Curry</span>
|
### <span id="Curry">Curry</span>
|
||||||
|
|
||||||
<p>Make a curry function.</p>
|
<p>Make a curry function.</p>
|
||||||
@@ -136,6 +138,7 @@ func main() {
|
|||||||
type Fn func(...interface{}) interface{}
|
type Fn func(...interface{}) interface{}
|
||||||
func (f Fn) Curry(i interface{}) func(...interface{}) interface{}
|
func (f Fn) Curry(i interface{}) func(...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -159,8 +162,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Compose">Compose</span>
|
### <span id="Compose">Compose</span>
|
||||||
|
|
||||||
<p>Compose the function list from right to left, then return the composed function.</p>
|
<p>Compose the function list from right to left, then return the composed function.</p>
|
||||||
@@ -170,6 +171,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{}
|
func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -195,8 +197,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Debounced">Debounced</span>
|
### <span id="Debounced">Debounced</span>
|
||||||
|
|
||||||
<p>Creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked.</p>
|
<p>Creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked.</p>
|
||||||
@@ -206,6 +206,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Debounced(fn func(), duration time.Duration) func()
|
func Debounced(fn func(), duration time.Duration) func()
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -237,8 +238,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Delay">Delay</span>
|
### <span id="Delay">Delay</span>
|
||||||
|
|
||||||
<p>Invoke function after delayed time.</p>
|
<p>Invoke function after delayed time.</p>
|
||||||
@@ -248,6 +247,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Delay(delay time.Duration, fn interface{}, args ...interface{})
|
func Delay(delay time.Duration, fn interface{}, args ...interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -266,8 +266,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Schedule">Schedule</span>
|
### <span id="Schedule">Schedule</span>
|
||||||
|
|
||||||
<p>Invoke function every duration time, until close the returned bool chan.</p>
|
<p>Invoke function every duration time, until close the returned bool chan.</p>
|
||||||
@@ -277,6 +275,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool
|
func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -301,7 +300,48 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Pipeline">Pipeline</span>
|
||||||
|
|
||||||
|
<p>Pipeline takes a list of functions and returns a function whose param will be passed into
|
||||||
|
the functions one by one.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Pipeline(funcs ...func(interface{}) interface{}) func(interface{}) interface{}
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/function"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
addOne := func(x interface{}) interface{} {
|
||||||
|
return x.(int) + 1
|
||||||
|
}
|
||||||
|
double := func(x interface{}) interface{} {
|
||||||
|
return 2 * x.(int)
|
||||||
|
}
|
||||||
|
square := func(x interface{}) interface{} {
|
||||||
|
return x.(int) * x.(int)
|
||||||
|
}
|
||||||
|
|
||||||
|
f := function.Pipeline(addOne, double, square)
|
||||||
|
|
||||||
|
result := fn(2)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 36
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Watcher">Watcher</span>
|
### <span id="Watcher">Watcher</span>
|
||||||
|
|
||||||
@@ -320,6 +360,7 @@ func (w *Watcher) Stop() //stop the watcher
|
|||||||
func (w *Watcher) Reset() //reset the watcher
|
func (w *Watcher) Reset() //reset the watcher
|
||||||
func (w *Watcher) GetElapsedTime() time.Duration //get the elapsed time of function execution
|
func (w *Watcher) GetElapsedTime() time.Duration //get the elapsed time of function execution
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -359,6 +400,3 @@ func longRunningTask() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Function
|
# Function
|
||||||
|
|
||||||
function 函数包控制函数执行流程,包含部分函数式编程。
|
function 函数包控制函数执行流程,包含部分函数式编程。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -11,6 +12,7 @@ function函数包控制函数执行流程,包含部分函数式编程。
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/function"
|
"github.com/duke-git/lancet/function"
|
||||||
@@ -20,21 +22,23 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [After](#After)
|
- [After](#After)
|
||||||
- [Before](#Before)
|
- [Before](#Before)
|
||||||
- [Curry](#Curry)
|
- [Curry](#Curry)
|
||||||
- [Compose](#Compose)
|
- [Compose](#Compose)
|
||||||
- [Debounced](#Debounced)
|
- [Debounced](#Debounced)
|
||||||
- [Delay](#Delay)
|
- [Delay](#Delay)
|
||||||
|
- [Pipeline](#Pipeline)
|
||||||
|
- [Schedule](#Schedule)
|
||||||
- [Watcher](#Watcher)
|
- [Watcher](#Watcher)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="After">After</span>
|
### <span id="After">After</span>
|
||||||
|
|
||||||
<p>创建一个函数,当他被调用n或更多次之后将马上触发fn</p>
|
<p>创建一个函数,当他被调用n或更多次之后将马上触发fn</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -42,6 +46,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -78,8 +83,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Before">Before</span>
|
### <span id="Before">Before</span>
|
||||||
|
|
||||||
<p>创建一个函数,调用次数不超过n次,之后再调用这个函数,将返回一次最后调用fn的结果</p>
|
<p>创建一个函数,调用次数不超过n次,之后再调用这个函数,将返回一次最后调用fn的结果</p>
|
||||||
@@ -89,6 +92,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -124,8 +128,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Curry">Curry</span>
|
### <span id="Curry">Curry</span>
|
||||||
|
|
||||||
<p>创建一个柯里化的函数</p>
|
<p>创建一个柯里化的函数</p>
|
||||||
@@ -136,6 +138,7 @@ func main() {
|
|||||||
type Fn func(...interface{}) interface{}
|
type Fn func(...interface{}) interface{}
|
||||||
func (f Fn) Curry(i interface{}) func(...interface{}) interface{}
|
func (f Fn) Curry(i interface{}) func(...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -159,8 +162,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Compose">Compose</span>
|
### <span id="Compose">Compose</span>
|
||||||
|
|
||||||
<p>从右至左组合函数列表fnList, 返回组合后的函数</p>
|
<p>从右至左组合函数列表fnList, 返回组合后的函数</p>
|
||||||
@@ -170,6 +171,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{}
|
func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -195,8 +197,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Debounced">Debounced</span>
|
### <span id="Debounced">Debounced</span>
|
||||||
|
|
||||||
<p>创建一个 debounced 函数,该函数延迟调用 fn 直到自上次调用 debounced 函数后等待持续时间过去。</p>
|
<p>创建一个 debounced 函数,该函数延迟调用 fn 直到自上次调用 debounced 函数后等待持续时间过去。</p>
|
||||||
@@ -206,6 +206,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Debounced(fn func(), duration time.Duration) func()
|
func Debounced(fn func(), duration time.Duration) func()
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -237,8 +238,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Delay">Delay</span>
|
### <span id="Delay">Delay</span>
|
||||||
|
|
||||||
<p>延迟delay时间后调用函数</p>
|
<p>延迟delay时间后调用函数</p>
|
||||||
@@ -248,6 +247,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Delay(delay time.Duration, fn interface{}, args ...interface{})
|
func Delay(delay time.Duration, fn interface{}, args ...interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -266,8 +266,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Schedule">Schedule</span>
|
### <span id="Schedule">Schedule</span>
|
||||||
|
|
||||||
<p>每次持续时间调用函数,直到关闭返回的 bool chan</p>
|
<p>每次持续时间调用函数,直到关闭返回的 bool chan</p>
|
||||||
@@ -277,6 +275,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool
|
func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -301,7 +300,47 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Pipeline">Pipeline</span>
|
||||||
|
|
||||||
|
<p>管道执行多个函数</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Pipeline(funcs ...func(interface{}) interface{}) func(interface{}) interface{}
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>例子:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/function"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
addOne := func(x interface{}) interface{} {
|
||||||
|
return x.(int) + 1
|
||||||
|
}
|
||||||
|
double := func(x interface{}) interface{} {
|
||||||
|
return 2 * x.(int)
|
||||||
|
}
|
||||||
|
square := func(x interface{}) interface{} {
|
||||||
|
return x.(int) * x.(int)
|
||||||
|
}
|
||||||
|
|
||||||
|
f := function.Pipeline(addOne, double, square)
|
||||||
|
|
||||||
|
result := fn(2)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 36
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Watcher">Watcher</span>
|
### <span id="Watcher">Watcher</span>
|
||||||
|
|
||||||
@@ -320,6 +359,7 @@ func (w *Watcher) Stop() //stop the watcher
|
|||||||
func (w *Watcher) Reset() //reset the watcher
|
func (w *Watcher) Reset() //reset the watcher
|
||||||
func (w *Watcher) GetElapsedTime() time.Duration //get the elapsed time of function execution
|
func (w *Watcher) GetElapsedTime() time.Duration //get the elapsed time of function execution
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -359,6 +399,3 @@ func longRunningTask() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Mathutil
|
# Mathutil
|
||||||
|
|
||||||
Package mathutil implements some functions for math calculation.
|
Package mathutil implements some functions for math calculation.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ Package mathutil implements some functions for math calculation.
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go)
|
[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>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Example:
|
## Example:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/mathutil"
|
"github.com/duke-git/lancet/mathutil"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [Exponent](#Exponent)
|
- [Exponent](#Exponent)
|
||||||
- [Fibonacci](#Fibonacci)
|
- [Fibonacci](#Fibonacci)
|
||||||
- [Factorial](#Factorial)
|
- [Factorial](#Factorial)
|
||||||
@@ -32,8 +34,8 @@ import (
|
|||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="Exponent">Exponent</span>
|
### <span id="Exponent">Exponent</span>
|
||||||
|
|
||||||
<p>Calculate x to the nth power.</p>
|
<p>Calculate x to the nth power.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -41,6 +43,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func Exponent(x, n int64) int64
|
func Exponent(x, n int64) int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -58,9 +61,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Fibonacci">Fibonacci</span>
|
### <span id="Fibonacci">Fibonacci</span>
|
||||||
|
|
||||||
<p>Calculate the nth number of fibonacci sequence.</p>
|
<p>Calculate the nth number of fibonacci sequence.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -68,6 +70,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Fibonacci(first, second, n int) int
|
func Fibonacci(first, second, n int) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -87,9 +90,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Factorial">Factorial</span>
|
### <span id="Factorial">Factorial</span>
|
||||||
|
|
||||||
<p>Calculate the factorial of x.</p>
|
<p>Calculate the factorial of x.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -97,6 +99,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Factorial(x uint) uint
|
func Factorial(x uint) uint
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -115,9 +118,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Percent">Percent</span>
|
### <span id="Percent">Percent</span>
|
||||||
|
|
||||||
<p>calculate the percentage of val to total, retain n decimal places.</p>
|
<p>calculate the percentage of val to total, retain n decimal places.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -125,6 +127,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Percent(val, total float64, n int) float64
|
func Percent(val, total float64, n int) float64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -141,9 +144,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RoundToFloat">RoundToFloat</span>
|
### <span id="RoundToFloat">RoundToFloat</span>
|
||||||
|
|
||||||
<p>Round float up to n decimal places.</p>
|
<p>Round float up to n decimal places.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -151,6 +153,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RoundToFloat(x float64, n int) float64
|
func RoundToFloat(x float64, n int) float64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -170,10 +173,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RoundToString">RoundToString</span>
|
### <span id="RoundToString">RoundToString</span>
|
||||||
|
|
||||||
<p>Round float up to n decimal places. will return string.</p>
|
<p>Round float up to n decimal places. will return string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -181,6 +182,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RoundToString(x float64, n int) string
|
func RoundToString(x float64, n int) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -200,9 +202,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="TruncRound">TruncRound</span>
|
### <span id="TruncRound">TruncRound</span>
|
||||||
|
|
||||||
<p>Round float off n decimal places.</p>
|
<p>Round float off n decimal places.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -210,6 +211,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func TruncRound(x float64, n int) float64
|
func TruncRound(x float64, n int) float64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -228,6 +230,3 @@ func main() {
|
|||||||
fmt.Println(mathutil.TruncRound(0.125, 3)) //0.125
|
fmt.Println(mathutil.TruncRound(0.125, 3)) //0.125
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Mathutil
|
# Mathutil
|
||||||
|
|
||||||
mathutil 包实现了一些数学计算的函数.
|
mathutil 包实现了一些数学计算的函数.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ mathutil包实现了一些数学计算的函数.
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go](https://github.com/duke-git/lancet/blob/v1/mathutil/mathutil.go)
|
[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>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/mathutil"
|
"github.com/duke-git/lancet/mathutil"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [Exponent](#Exponent)
|
- [Exponent](#Exponent)
|
||||||
- [Fibonacci](#Fibonacci)
|
- [Fibonacci](#Fibonacci)
|
||||||
- [Factorial](#Factorial)
|
- [Factorial](#Factorial)
|
||||||
@@ -33,8 +35,8 @@ import (
|
|||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="Exponent">Exponent</span>
|
### <span id="Exponent">Exponent</span>
|
||||||
|
|
||||||
<p>指数计算(x的n次方)</p>
|
<p>指数计算(x的n次方)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -42,6 +44,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func Exponent(x, n int64) int64
|
func Exponent(x, n int64) int64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -59,9 +62,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Fibonacci">Fibonacci</span>
|
### <span id="Fibonacci">Fibonacci</span>
|
||||||
|
|
||||||
<p>计算斐波那契数列的第n个数</p>
|
<p>计算斐波那契数列的第n个数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -69,6 +71,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Fibonacci(first, second, n int) int
|
func Fibonacci(first, second, n int) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -88,9 +91,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Factorial">Factorial</span>
|
### <span id="Factorial">Factorial</span>
|
||||||
|
|
||||||
<p>计算阶乘</p>
|
<p>计算阶乘</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -98,6 +100,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Factorial(x uint) uint
|
func Factorial(x uint) uint
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -116,9 +119,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Percent">Percent</span>
|
### <span id="Percent">Percent</span>
|
||||||
|
|
||||||
<p>计算百分比,保留n位小数</p>
|
<p>计算百分比,保留n位小数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -126,6 +128,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Percent(val, total float64, n int) float64
|
func Percent(val, total float64, n int) float64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -142,9 +145,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RoundToFloat">RoundToFloat</span>
|
### <span id="RoundToFloat">RoundToFloat</span>
|
||||||
|
|
||||||
<p>四舍五入,保留n位小数</p>
|
<p>四舍五入,保留n位小数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -152,6 +154,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RoundToFloat(x float64, n int) float64
|
func RoundToFloat(x float64, n int) float64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -171,10 +174,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RoundToString">RoundToString</span>
|
### <span id="RoundToString">RoundToString</span>
|
||||||
|
|
||||||
<p>四舍五入,保留n位小数,返回字符串</p>
|
<p>四舍五入,保留n位小数,返回字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -182,6 +183,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RoundToString(x float64, n int) string
|
func RoundToString(x float64, n int) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -201,9 +203,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="TruncRound">TruncRound</span>
|
### <span id="TruncRound">TruncRound</span>
|
||||||
|
|
||||||
<p>截短n位小数(不进行四舍五入)</p>
|
<p>截短n位小数(不进行四舍五入)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -211,6 +212,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func TruncRound(x float64, n int) float64
|
func TruncRound(x float64, n int) float64
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -229,6 +231,3 @@ func main() {
|
|||||||
fmt.Println(mathutil.TruncRound(0.125, 3)) //0.125
|
fmt.Println(mathutil.TruncRound(0.125, 3)) //0.125
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Netutil
|
# Netutil
|
||||||
|
|
||||||
Package netutil contains functions to get net information and send http request.
|
Package netutil contains functions to get net information and send http request.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -14,6 +15,7 @@ Package netutil contains functions to get net information and send http request.
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/netutil"
|
"github.com/duke-git/lancet/netutil"
|
||||||
@@ -23,6 +25,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
||||||
- [EncodeUrl](#EncodeUrl)
|
- [EncodeUrl](#EncodeUrl)
|
||||||
- [GetInternalIp](#GetInternalIp)
|
- [GetInternalIp](#GetInternalIp)
|
||||||
@@ -48,8 +51,8 @@ import (
|
|||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span>
|
### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span>
|
||||||
|
|
||||||
<p>Convert map to url query string.</p>
|
<p>Convert map to url query string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -57,6 +60,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ConvertMapToQueryString(param map[string]interface{}) string
|
func ConvertMapToQueryString(param map[string]interface{}) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -79,9 +83,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EncodeUrl">EncodeUrl</span>
|
### <span id="EncodeUrl">EncodeUrl</span>
|
||||||
|
|
||||||
<p>Encode url query string values.</p>
|
<p>Encode url query string values.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -89,6 +92,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EncodeUrl(urlStr string) (string, error)
|
func EncodeUrl(urlStr string) (string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -109,8 +113,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetInternalIp">GetInternalIp</span>
|
### <span id="GetInternalIp">GetInternalIp</span>
|
||||||
|
|
||||||
<p>Get internal ip information.</p>
|
<p>Get internal ip information.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -118,6 +122,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetInternalIp() string
|
func GetInternalIp() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -137,9 +142,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetIps">GetIps</span>
|
### <span id="GetIps">GetIps</span>
|
||||||
|
|
||||||
<p>Get all ipv4 list.</p>
|
<p>Get all ipv4 list.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -147,6 +151,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetIps() []string
|
func GetIps() []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -164,9 +169,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetMacAddrs">GetMacAddrs</span>
|
### <span id="GetMacAddrs">GetMacAddrs</span>
|
||||||
|
|
||||||
<p>Get all mac addresses list.</p>
|
<p>Get all mac addresses list.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -174,6 +178,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetMacAddrs() []string {
|
func GetMacAddrs() []string {
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -191,9 +196,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetPublicIpInfo">GetPublicIpInfo</span>
|
### <span id="GetPublicIpInfo">GetPublicIpInfo</span>
|
||||||
|
|
||||||
<p>Get public ip information.</p>
|
<p>Get public ip information.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -215,6 +219,7 @@ type PublicIpInfo struct {
|
|||||||
Ip string `json:"query"`
|
Ip string `json:"query"`
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -235,8 +240,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetRequestPublicIp">GetRequestPublicIp</span>
|
### <span id="GetRequestPublicIp">GetRequestPublicIp</span>
|
||||||
|
|
||||||
<p>Get http request public ip.</p>
|
<p>Get http request public ip.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -244,6 +249,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetRequestPublicIp(req *http.Request) string
|
func GetRequestPublicIp(req *http.Request) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -277,9 +283,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsPublicIP">IsPublicIP</span>
|
### <span id="IsPublicIP">IsPublicIP</span>
|
||||||
|
|
||||||
<p>Checks if a ip is public or not.</p>
|
<p>Checks if a ip is public or not.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -287,6 +292,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsPublicIP(IP net.IP) bool
|
func IsPublicIP(IP net.IP) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -307,9 +313,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsInternalIP">IsInternalIP</span>
|
### <span id="IsInternalIP">IsInternalIP</span>
|
||||||
|
|
||||||
<p>Checks if an ip is intranet or not.</p>
|
<p>Checks if an ip is intranet or not.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -317,6 +322,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsInternalIP(IP net.IP) bool
|
func IsInternalIP(IP net.IP) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -337,9 +343,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpRequest">HttpRequest</span>
|
### <span id="HttpRequest">HttpRequest</span>
|
||||||
|
|
||||||
<p>HttpRequest is a struct used to abstract HTTP request entity.</p>
|
<p>HttpRequest is a struct used to abstract HTTP request entity.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -354,6 +359,7 @@ type HttpRequest struct {
|
|||||||
Body []byte
|
Body []byte
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -382,8 +388,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpClient">HttpClient</span>
|
### <span id="HttpClient">HttpClient</span>
|
||||||
|
|
||||||
<p>HttpClient is a struct used to send HTTP request. It can be instanced with some configurations or none config.</p>
|
<p>HttpClient is a struct used to send HTTP request. It can be instanced with some configurations or none config.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -410,6 +416,7 @@ func NewHttpClient() *HttpClient
|
|||||||
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
|
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -431,9 +438,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SendRequest">SendRequest</span>
|
### <span id="SendRequest">SendRequest</span>
|
||||||
|
|
||||||
<p>Use HttpClient to send HTTP request.</p>
|
<p>Use HttpClient to send HTTP request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -441,6 +447,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
|
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -479,9 +486,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DecodeResponse">DecodeResponse</span>
|
### <span id="DecodeResponse">DecodeResponse</span>
|
||||||
|
|
||||||
<p>Decode http response into target object.</p>
|
<p>Decode http response into target object.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -489,6 +495,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (client *HttpClient) DecodeResponse(resp *http.Response, target interface{}) error
|
func (client *HttpClient) DecodeResponse(resp *http.Response, target interface{}) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -527,8 +534,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="StructToUrlValues">StructToUrlValues</span>
|
### <span id="StructToUrlValues">StructToUrlValues</span>
|
||||||
|
|
||||||
<p>Convert struct to url values, only convert the field which is exported and has `json` tag.</p>
|
<p>Convert struct to url values, only convert the field which is exported and has `json` tag.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -536,6 +543,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func StructToUrlValues(targetStruct interface{}) url.Values
|
func StructToUrlValues(targetStruct interface{}) url.Values
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -562,8 +570,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>Send http get request.</p>
|
<p>Send http get request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -575,6 +583,7 @@ func main() {
|
|||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
func HttpGet(url string, params ...interface{}) (*http.Response, error)
|
func HttpGet(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -603,9 +612,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>Send http post request.</p>
|
<p>Send http post request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -617,6 +625,7 @@ func main() {
|
|||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
func HttpPost(url string, params ...interface{}) (*http.Response, error)
|
func HttpPost(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -652,9 +661,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>Send http put request.</p>
|
<p>Send http put request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -666,6 +674,7 @@ func main() {
|
|||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
func HttpPut(url string, params ...interface{}) (*http.Response, error)
|
func HttpPut(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -702,9 +711,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>Send http delete request.</p>
|
<p>Send http delete request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -716,6 +724,7 @@ func main() {
|
|||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
func HttpDelete(url string, params ...interface{}) (*http.Response, error)
|
func HttpDelete(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -741,9 +750,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>Send http patch request.</p>
|
<p>Send http patch request.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -755,6 +763,7 @@ func main() {
|
|||||||
// params[3] is http client which type should be http.Client.
|
// params[3] is http client which type should be http.Client.
|
||||||
func HttpPatch(url string, params ...interface{}) (*http.Response, error)
|
func HttpPatch(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -791,9 +800,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ParseHttpResponse">ParseHttpResponse</span>
|
### <span id="ParseHttpResponse">ParseHttpResponse</span>
|
||||||
|
|
||||||
<p>Decode http response to specified interface.</p>
|
<p>Decode http response to specified interface.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -801,6 +809,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ParseHttpResponse(resp *http.Response, obj interface{}) error
|
func ParseHttpResponse(resp *http.Response, obj interface{}) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -841,4 +850,3 @@ func main() {
|
|||||||
fmt.Println(toDoResp)
|
fmt.Println(toDoResp)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Netutil
|
# Netutil
|
||||||
|
|
||||||
netutil 网络包支持获取 ip 地址,发送 http 请求。
|
netutil 网络包支持获取 ip 地址,发送 http 请求。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -13,6 +14,7 @@ netutil网络包支持获取ip地址,发送http请求。
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/netutil"
|
"github.com/duke-git/lancet/netutil"
|
||||||
@@ -22,6 +24,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
- [ConvertMapToQueryString](#ConvertMapToQueryString)
|
||||||
- [EncodeUrl](#EncodeUrl)
|
- [EncodeUrl](#EncodeUrl)
|
||||||
- [GetInternalIp](#GetInternalIp)
|
- [GetInternalIp](#GetInternalIp)
|
||||||
@@ -47,8 +50,8 @@ import (
|
|||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span>
|
### <span id="ConvertMapToQueryString">ConvertMapToQueryString</span>
|
||||||
|
|
||||||
<p>将map转换成http查询字符串.</p>
|
<p>将map转换成http查询字符串.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -56,6 +59,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ConvertMapToQueryString(param map[string]interface{}) string
|
func ConvertMapToQueryString(param map[string]interface{}) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -78,9 +82,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EncodeUrl">EncodeUrl</span>
|
### <span id="EncodeUrl">EncodeUrl</span>
|
||||||
|
|
||||||
<p>编码url query string的值</p>
|
<p>编码url query string的值</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -88,6 +91,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EncodeUrl(urlStr string) (string, error)
|
func EncodeUrl(urlStr string) (string, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -108,9 +112,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetInternalIp">GetInternalIp</span>
|
### <span id="GetInternalIp">GetInternalIp</span>
|
||||||
|
|
||||||
<p>获取内部ip</p>
|
<p>获取内部ip</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -118,6 +121,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetInternalIp() string
|
func GetInternalIp() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -137,8 +141,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetIps">GetIps</span>
|
### <span id="GetIps">GetIps</span>
|
||||||
|
|
||||||
<p>获取ipv4地址列表</p>
|
<p>获取ipv4地址列表</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -146,6 +150,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetIps() []string
|
func GetIps() []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -163,9 +168,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetMacAddrs">GetMacAddrs</span>
|
### <span id="GetMacAddrs">GetMacAddrs</span>
|
||||||
|
|
||||||
<p>获取mac地址列</p>
|
<p>获取mac地址列</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -173,6 +177,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetMacAddrs() []string {
|
func GetMacAddrs() []string {
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -190,9 +195,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetPublicIpInfo">GetPublicIpInfo</span>
|
### <span id="GetPublicIpInfo">GetPublicIpInfo</span>
|
||||||
|
|
||||||
<p>获取公网ip信息</p>
|
<p>获取公网ip信息</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -214,6 +218,7 @@ type PublicIpInfo struct {
|
|||||||
Ip string `json:"query"`
|
Ip string `json:"query"`
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -234,9 +239,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetRequestPublicIp">GetRequestPublicIp</span>
|
### <span id="GetRequestPublicIp">GetRequestPublicIp</span>
|
||||||
|
|
||||||
<p>获取http请求ip</p>
|
<p>获取http请求ip</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -244,6 +248,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetRequestPublicIp(req *http.Request) string
|
func GetRequestPublicIp(req *http.Request) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -277,9 +282,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsPublicIP">IsPublicIP</span>
|
### <span id="IsPublicIP">IsPublicIP</span>
|
||||||
|
|
||||||
<p>判断ip是否是公共ip</p>
|
<p>判断ip是否是公共ip</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -287,6 +291,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsPublicIP(IP net.IP) bool
|
func IsPublicIP(IP net.IP) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -307,9 +312,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsInternalIP">IsInternalIP</span>
|
### <span id="IsInternalIP">IsInternalIP</span>
|
||||||
|
|
||||||
<p>判断ip是否是局域网ip.</p>
|
<p>判断ip是否是局域网ip.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -317,6 +321,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsInternalIP(IP net.IP) bool
|
func IsInternalIP(IP net.IP) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -337,8 +342,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpRequest">HttpRequest</span>
|
### <span id="HttpRequest">HttpRequest</span>
|
||||||
|
|
||||||
<p>HttpRequest用于抽象HTTP请求实体的结构</p>
|
<p>HttpRequest用于抽象HTTP请求实体的结构</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -353,6 +358,7 @@ type HttpRequest struct {
|
|||||||
Body []byte
|
Body []byte
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -381,8 +387,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpClient">HttpClient</span>
|
### <span id="HttpClient">HttpClient</span>
|
||||||
|
|
||||||
<p>HttpClient是用于发送HTTP请求的结构体。它可以用一些配置参数或无配置实例化.</p>
|
<p>HttpClient是用于发送HTTP请求的结构体。它可以用一些配置参数或无配置实例化.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -409,6 +415,7 @@ func NewHttpClient() *HttpClient
|
|||||||
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
|
func NewHttpClientWithConfig(config *HttpClientConfig) *HttpClient
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -430,9 +437,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SendRequest">SendRequest</span>
|
### <span id="SendRequest">SendRequest</span>
|
||||||
|
|
||||||
<p>HttpClient发送http请求</p>
|
<p>HttpClient发送http请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -440,6 +446,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
|
func (client *HttpClient) SendRequest(request *HttpRequest) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -478,9 +485,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DecodeResponse">DecodeResponse</span>
|
### <span id="DecodeResponse">DecodeResponse</span>
|
||||||
|
|
||||||
<p>解析http响应体到目标结构体</p>
|
<p>解析http响应体到目标结构体</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -488,6 +494,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func (client *HttpClient) DecodeResponse(resp *http.Response, target interface{}) error
|
func (client *HttpClient) DecodeResponse(resp *http.Response, target interface{}) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -526,8 +533,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="StructToUrlValues">StructToUrlValues</span>
|
### <span id="StructToUrlValues">StructToUrlValues</span>
|
||||||
|
|
||||||
<p>将结构体转为url values, 仅转化结构体导出字段并且包含`json` tag.</p>
|
<p>将结构体转为url values, 仅转化结构体导出字段并且包含`json` tag.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -535,6 +542,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func StructToUrlValues(targetStruct interface{}) url.Values
|
func StructToUrlValues(targetStruct interface{}) url.Values
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -561,9 +569,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpGet">HttpGet (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>发送http get请求</p>
|
<p>发送http get请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -575,6 +582,7 @@ func main() {
|
|||||||
// params[3] http client,类型必须是http.Client
|
// params[3] http client,类型必须是http.Client
|
||||||
func HttpGet(url string, params ...interface{}) (*http.Response, error)
|
func HttpGet(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -603,9 +611,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPost">HttpPost (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>发送http post请求</p>
|
<p>发送http post请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -617,6 +624,7 @@ func main() {
|
|||||||
// params[3] http client,类型必须是http.Client
|
// params[3] http client,类型必须是http.Client
|
||||||
func HttpPost(url string, params ...interface{}) (*http.Response, error)
|
func HttpPost(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -652,9 +660,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPut">HttpPut (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>发送http put请求</p>
|
<p>发送http put请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -666,6 +673,7 @@ func main() {
|
|||||||
// params[3] http client,类型必须是http.Client
|
// params[3] http client,类型必须是http.Client
|
||||||
func HttpPut(url string, params ...interface{}) (*http.Response, error)
|
func HttpPut(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -702,9 +710,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpDelete">HttpDelete (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>发送http delete请求</p>
|
<p>发送http delete请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -716,6 +723,7 @@ func main() {
|
|||||||
// params[3] http client,类型必须是http.Client
|
// params[3] http client,类型必须是http.Client
|
||||||
func HttpDelete(url string, params ...interface{}) (*http.Response, error)
|
func HttpDelete(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -741,9 +749,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
### <span id="HttpPatch">HttpPatch (Deprecated: use SendRequest for replacement)</span>
|
||||||
|
|
||||||
<p>发送http patch请求</p>
|
<p>发送http patch请求</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -755,6 +762,7 @@ func main() {
|
|||||||
// params[3] http client,类型必须是http.Client
|
// params[3] http client,类型必须是http.Client
|
||||||
func HttpPatch(url string, params ...interface{}) (*http.Response, error)
|
func HttpPatch(url string, params ...interface{}) (*http.Response, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -791,9 +799,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ParseHttpResponse">ParseHttpResponse</span>
|
### <span id="ParseHttpResponse">ParseHttpResponse</span>
|
||||||
|
|
||||||
<p>将http请求响应解码成特定struct值</p>
|
<p>将http请求响应解码成特定struct值</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -801,6 +808,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ParseHttpResponse(resp *http.Response, obj interface{}) error
|
func ParseHttpResponse(resp *http.Response, obj interface{}) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -841,4 +849,3 @@ func main() {
|
|||||||
fmt.Println(toDoResp)
|
fmt.Println(toDoResp)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Retry
|
# Retry
|
||||||
|
|
||||||
Package retry is for executing a function repeatedly until it was successful or canceled by the context.
|
Package retry is for executing a function repeatedly until it was successful or canceled by the context.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ Package retry is for executing a function repeatedly until it was successful or
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/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](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>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/retry"
|
"github.com/duke-git/lancet/retry"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [Context](#Context)
|
- [Context](#Context)
|
||||||
- [Retry](#Retry)
|
- [Retry](#Retry)
|
||||||
- [RetryFunc](#RetryFunc)
|
- [RetryFunc](#RetryFunc)
|
||||||
@@ -30,8 +32,8 @@ import (
|
|||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="Context">Context</span>
|
### <span id="Context">Context</span>
|
||||||
|
|
||||||
<p>Set retry context config, can cancel the retry with context.</p>
|
<p>Set retry context config, can cancel the retry with context.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -39,6 +41,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func Context(ctx context.Context)
|
func Context(ctx context.Context)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -72,10 +75,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryFunc">RetryFunc</span>
|
### <span id="RetryFunc">RetryFunc</span>
|
||||||
|
|
||||||
<p>Function that retry executes.</p>
|
<p>Function that retry executes.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -83,6 +84,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
type RetryFunc func() error
|
type RetryFunc func() error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -115,9 +117,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryTimes">RetryTimes</span>
|
### <span id="RetryTimes">RetryTimes</span>
|
||||||
|
|
||||||
<p>Set times of retry. Default times is 5.</p>
|
<p>Set times of retry. Default times is 5.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -125,6 +126,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RetryTimes(n uint)
|
func RetryTimes(n uint)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -154,9 +156,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryDuration">RetryDuration</span>
|
### <span id="RetryDuration">RetryDuration</span>
|
||||||
|
|
||||||
<p>Set duration of retries. Default duration is 3 second.</p>
|
<p>Set duration of retries. Default duration is 3 second.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -164,6 +165,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RetryDuration(d time.Duration)
|
func RetryDuration(d time.Duration)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -195,8 +197,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Retry">Retry</span>
|
### <span id="Retry">Retry</span>
|
||||||
|
|
||||||
<p>Executes the retryFunc repeatedly until it was successful or canceled by the context.</p>
|
<p>Executes the retryFunc repeatedly until it was successful or canceled by the context.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -204,6 +206,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Retry(retryFunc RetryFunc, opts ...Option) error
|
func Retry(retryFunc RetryFunc, opts ...Option) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Retry
|
# Retry
|
||||||
|
|
||||||
retry 重试执行函数直到函数运行成功或被 context cancel。
|
retry 重试执行函数直到函数运行成功或被 context cancel。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ retry重试执行函数直到函数运行成功或被context cancel。
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/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](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>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/retry"
|
"github.com/duke-git/lancet/retry"
|
||||||
@@ -20,20 +21,19 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [Context](#Context)
|
- [Context](#Context)
|
||||||
- [Retry](#Retry)
|
- [Retry](#Retry)
|
||||||
- [RetryFunc](#RetryFunc)
|
- [RetryFunc](#RetryFunc)
|
||||||
- [RetryDuration](#RetryDuration)
|
- [RetryDuration](#RetryDuration)
|
||||||
- [RetryTimes](#RetryTimes)
|
- [RetryTimes](#RetryTimes)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
|
||||||
## Document 文档
|
## Document 文档
|
||||||
|
|
||||||
|
|
||||||
### <span id="Context">Context</span>
|
### <span id="Context">Context</span>
|
||||||
|
|
||||||
<p>设置重试context参数</p>
|
<p>设置重试context参数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -41,6 +41,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func Context(ctx context.Context)
|
func Context(ctx context.Context)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -74,10 +75,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryFunc">RetryFunc</span>
|
### <span id="RetryFunc">RetryFunc</span>
|
||||||
|
|
||||||
<p>被重试执行的函数</p>
|
<p>被重试执行的函数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -85,6 +84,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
type RetryFunc func() error
|
type RetryFunc func() error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -117,9 +117,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryTimes">RetryTimes</span>
|
### <span id="RetryTimes">RetryTimes</span>
|
||||||
|
|
||||||
<p>设置重试次数,默认5</p>
|
<p>设置重试次数,默认5</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -127,6 +126,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RetryTimes(n uint)
|
func RetryTimes(n uint)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -156,9 +156,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RetryDuration">RetryDuration</span>
|
### <span id="RetryDuration">RetryDuration</span>
|
||||||
|
|
||||||
<p>设置重试间隔时间,默认3秒</p>
|
<p>设置重试间隔时间,默认3秒</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -166,6 +165,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RetryDuration(d time.Duration)
|
func RetryDuration(d time.Duration)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -197,8 +197,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Retry">Retry</span>
|
### <span id="Retry">Retry</span>
|
||||||
|
|
||||||
<p>重试执行函数retryFunc,直到函数运行成功,或被context停止</p>
|
<p>重试执行函数retryFunc,直到函数运行成功,或被context停止</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -206,6 +206,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Retry(retryFunc RetryFunc, opts ...Option) error
|
func Retry(retryFunc RetryFunc, opts ...Option) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|||||||
187
docs/slice.md
187
docs/slice.md
@@ -1,4 +1,5 @@
|
|||||||
# Slice
|
# Slice
|
||||||
|
|
||||||
Package slice implements some functions to manipulate slice.
|
Package slice implements some functions to manipulate slice.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ Package slice implements some functions to manipulate slice.
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/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](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>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/slice"
|
"github.com/duke-git/lancet/slice"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [AppendIfAbsent](#AppendIfAbsent)
|
- [AppendIfAbsent](#AppendIfAbsent)
|
||||||
- [Contain](#Contain)
|
- [Contain](#Contain)
|
||||||
- [ContainSubSlice](#ContainSubSlice)
|
- [ContainSubSlice](#ContainSubSlice)
|
||||||
@@ -66,11 +68,11 @@ import (
|
|||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
## Note:
|
## Note:
|
||||||
|
|
||||||
1. param which type is interface{} in below functions should be slice.
|
1. param which type is interface{} in below functions should be slice.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AppendIfAbsent">AppendIfAbsent</span>
|
### <span id="AppendIfAbsent">AppendIfAbsent</span>
|
||||||
|
|
||||||
<p>If slice doesn't contain the value, append it to the slice.</p>
|
<p>If slice doesn't contain the value, append it to the slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -78,6 +80,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func AppendIfAbsent(slice interface{}, value interface{}) interface{}
|
func AppendIfAbsent(slice interface{}, value interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -96,8 +99,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Contain">Contain</span>
|
### <span id="Contain">Contain</span>
|
||||||
|
|
||||||
<p>Check if the value is in the slice or not. iterableType param can be string, map or slice.</p>
|
<p>Check if the value is in the slice or not. iterableType param can be string, map or slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -105,6 +108,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Contain(iterableType interface{}, value interface{}) bool
|
func Contain(iterableType interface{}, value interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -119,8 +123,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainSubSlice">ContainSubSlice</span>
|
### <span id="ContainSubSlice">ContainSubSlice</span>
|
||||||
|
|
||||||
<p>Check if the slice contain subslice or not.</p>
|
<p>Check if the slice contain subslice or not.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -128,6 +132,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ContainSubSlice(slice interface{}, subslice interface{}) bool
|
func ContainSubSlice(slice interface{}, subslice interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -142,10 +147,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Chunk">Chunk</span>
|
### <span id="Chunk">Chunk</span>
|
||||||
|
|
||||||
<p>Creates an slice of elements split into groups the length of `size`.</p>
|
<p>Creates an slice of elements split into groups the length of `size`.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -153,6 +156,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Chunk(slice []interface{}, size int) [][]interface{}
|
func Chunk(slice []interface{}, size int) [][]interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -168,9 +172,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Compact">Compact</span>
|
### <span id="Compact">Compact</span>
|
||||||
|
|
||||||
<p>Creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey.</p>
|
<p>Creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -178,6 +181,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Compact(slice interface{}) interface{}
|
func Compact(slice interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -192,8 +196,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Concat">Concat</span>
|
### <span id="Concat">Concat</span>
|
||||||
|
|
||||||
<p>Creates a new slice concatenating slice with any additional slices and/or values.</p>
|
<p>Creates a new slice concatenating slice with any additional slices and/or values.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -201,6 +205,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Concat(slice interface{}, values ...interface{}) interface{}
|
func Concat(slice interface{}, values ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -218,9 +223,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Count">Count</span>
|
### <span id="Count">Count</span>
|
||||||
|
|
||||||
<p>Count iterates over elements of slice, returns a count of all matched elements. The function signature should be func(index int, value interface{}) bool.</p>
|
<p>Count iterates over elements of slice, returns a count of all matched elements. The function signature should be func(index int, value interface{}) bool.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -228,6 +232,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Count(slice, function interface{}) int
|
func Count(slice, function interface{}) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -247,10 +252,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Difference">Difference</span>
|
### <span id="Difference">Difference</span>
|
||||||
|
|
||||||
<p>Creates an slice of whose element not included in the other given slice.</p>
|
<p>Creates an slice of whose element not included in the other given slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -258,6 +261,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Difference(slice1, slice2 interface{}) interface{}
|
func Difference(slice1, slice2 interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -275,10 +279,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DifferenceBy">DifferenceBy</span>
|
### <span id="DifferenceBy">DifferenceBy</span>
|
||||||
|
|
||||||
<p>DifferenceBy accepts iteratee func which is invoked for each element of slice and values to generate the criterion by which they're compared.</p>
|
<p>DifferenceBy accepts iteratee func which is invoked for each element of slice and values to generate the criterion by which they're compared.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -286,6 +288,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func DifferenceBy(slice interface{}, comparedSlice interface{}, iterateeFn interface{}) interface{}
|
func DifferenceBy(slice interface{}, comparedSlice interface{}, iterateeFn interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -306,10 +309,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DeleteByIndex">DeleteByIndex</span>
|
### <span id="DeleteByIndex">DeleteByIndex</span>
|
||||||
|
|
||||||
<p>Delete the element of slice from start index to end index - 1.</p>
|
<p>Delete the element of slice from start index to end index - 1.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -317,6 +318,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error)
|
func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -335,10 +337,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Drop">Drop</span>
|
### <span id="Drop">Drop</span>
|
||||||
|
|
||||||
<p>Creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0.</p>
|
<p>Creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -346,6 +346,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Drop(slice interface{}, n int) interface{}
|
func Drop(slice interface{}, n int) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -366,9 +367,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Equal">Equal</span>
|
### <span id="Equal">Equal</span>
|
||||||
|
|
||||||
<p>Check if two slices are equal: the same length and all elements' order and value are equal.</p>
|
<p>Check if two slices are equal: the same length and all elements' order and value are equal.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -376,6 +376,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Equal(slice1, slice2 interface{}) bool
|
func Equal(slice1, slice2 interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -397,9 +398,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EqualWith">EqualWith</span>
|
### <span id="EqualWith">EqualWith</span>
|
||||||
|
|
||||||
<p>Check if two slices are equal with comparator funcation.comparator signature: func(a interface{}, b interface{}) bool</p>
|
<p>Check if two slices are equal with comparator funcation.comparator signature: func(a interface{}, b interface{}) bool</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -407,6 +407,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EqualWith(slice1, slice2 interface{}, comparator interface{}) bool
|
func EqualWith(slice1, slice2 interface{}, comparator interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -429,9 +430,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Every">Every</span>
|
### <span id="Every">Every</span>
|
||||||
|
|
||||||
<p>Return true if all of the values in the slice pass the predicate function. The function signature should be func(index int, value interface{}) bool.</p>
|
<p>Return true if all of the values in the slice pass the predicate function. The function signature should be func(index int, value interface{}) bool.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -439,6 +439,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Every(slice, function interface{}) bool
|
func Every(slice, function interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -458,10 +459,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Filter">Filter</span>
|
### <span id="Filter">Filter</span>
|
||||||
|
|
||||||
<p>Return all elements which match the function. Function signature should be func(index int, value interface{}) bool.</p>
|
<p>Return all elements which match the function. Function signature should be func(index int, value interface{}) bool.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -469,6 +468,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Filter(slice, function interface{}) interface{}
|
func Filter(slice, function interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -488,9 +488,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Find">Find</span>
|
### <span id="Find">Find</span>
|
||||||
|
|
||||||
<p>Iterates over elements of slice, returning the first one that passes a truth test on function.function signature should be func(index int, value interface{}) bool.</p>
|
<p>Iterates over elements of slice, returning the first one that passes a truth test on function.function signature should be func(index int, value interface{}) bool.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -498,6 +497,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Find(slice, function interface{}) (interface{}, bool)
|
func Find(slice, function interface{}) (interface{}, bool)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -518,10 +518,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="FindLast">FindLast</span>
|
### <span id="FindLast">FindLast</span>
|
||||||
|
|
||||||
<p>iterates over elements of slice from end to begin, returning the last one that passes a truth test on function. The function signature should be func(index int, value interface{}) bool.</p>
|
<p>iterates over elements of slice from end to begin, returning the last one that passes a truth test on function. The function signature should be func(index int, value interface{}) bool.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -529,6 +527,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FindLast(slice, function interface{}) (interface{}, bool)
|
func FindLast(slice, function interface{}) (interface{}, bool)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -549,9 +548,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="FlattenDeep">FlattenDeep</span>
|
### <span id="FlattenDeep">FlattenDeep</span>
|
||||||
|
|
||||||
<p>flattens slice recursive.</p>
|
<p>flattens slice recursive.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -559,6 +557,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FlattenDeep(slice interface{}) interface{}
|
func FlattenDeep(slice interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -574,9 +573,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ForEach">ForEach</span>
|
### <span id="ForEach">ForEach</span>
|
||||||
|
|
||||||
<p>Iterates over elements of slice and invokes function for each element, function signature should be func(index int, value interface{}).</p>
|
<p>Iterates over elements of slice and invokes function for each element, function signature should be func(index int, value interface{}).</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -584,6 +582,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ForEach(slice, function interface{})
|
func ForEach(slice, function interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -602,10 +601,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GroupBy">GroupBy</span>
|
### <span id="GroupBy">GroupBy</span>
|
||||||
|
|
||||||
<p>Iterates over elements of the slice, each element will be group by criteria, returns two slices. The function signature should be func(index int, value interface{}) bool.</p>
|
<p>Iterates over elements of the slice, each element will be group by criteria, returns two slices. The function signature should be func(index int, value interface{}) bool.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -613,6 +610,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GroupBy(slice, function interface{}) (interface{}, interface{})
|
func GroupBy(slice, function interface{}) (interface{}, interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -633,10 +631,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IntSlice">IntSlice</span>
|
### <span id="IntSlice">IntSlice</span>
|
||||||
|
|
||||||
<p>Convert interface slice to int slice.</p>
|
<p>Convert interface slice to int slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -644,6 +640,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IntSlice(slice interface{}) []int
|
func IntSlice(slice interface{}) []int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -659,10 +656,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="InterfaceSlice">InterfaceSlice</span>
|
### <span id="InterfaceSlice">InterfaceSlice</span>
|
||||||
|
|
||||||
<p>Convert value to interface slice.</p>
|
<p>Convert value to interface slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -670,6 +665,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func InterfaceSlice(slice interface{}) []interface{}
|
func InterfaceSlice(slice interface{}) []interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -685,10 +681,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Intersection">Intersection</span>
|
### <span id="Intersection">Intersection</span>
|
||||||
|
|
||||||
<p>Creates a slice of unique values that included by all slices.</p>
|
<p>Creates a slice of unique values that included by all slices.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -696,6 +690,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Intersection(slices ...interface{}) interface{}
|
func Intersection(slices ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -713,9 +708,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IndexOf">IndexOf</span>
|
### <span id="IndexOf">IndexOf</span>
|
||||||
|
|
||||||
<p>Returns the index at which the first occurrence of a value is found in a slice or return -1 if the value cannot be found.</p>
|
<p>Returns the index at which the first occurrence of a value is found in a slice or return -1 if the value cannot be found.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -723,6 +717,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IndexOf(slice, value interface{}) int
|
func IndexOf(slice, value interface{}) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -741,9 +736,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="LastIndexOf">LastIndexOf</span>
|
### <span id="LastIndexOf">LastIndexOf</span>
|
||||||
|
|
||||||
<p>Returns the index at which the last occurrence of a value is found in a slice or return -1 if the value cannot be found.</p>
|
<p>Returns the index at which the last occurrence of a value is found in a slice or return -1 if the value cannot be found.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -751,6 +745,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func LastIndexOf(slice, value interface{}) int
|
func LastIndexOf(slice, value interface{}) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -769,8 +764,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="InsertByIndex">InsertByIndex</span>
|
### <span id="InsertByIndex">InsertByIndex</span>
|
||||||
|
|
||||||
<p>insert the element into slice at index.</p>
|
<p>insert the element into slice at index.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -778,6 +773,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error)
|
func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -797,10 +793,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Map">Map</span>
|
### <span id="Map">Map</span>
|
||||||
|
|
||||||
<p>Creates an slice of values by running each element in slice thru function, function signature should be func(index int, value interface{}) interface{}.</p>
|
<p>Creates an slice of values by running each element in slice thru function, function signature should be func(index int, value interface{}) interface{}.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -808,6 +802,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Map(slice, function interface{}) interface{}
|
func Map(slice, function interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -826,10 +821,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReverseSlice">ReverseSlice</span>
|
### <span id="ReverseSlice">ReverseSlice</span>
|
||||||
|
|
||||||
<p>Reverse the elements order in slice.</p>
|
<p>Reverse the elements order in slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -837,6 +830,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ReverseSlice(slice interface{})
|
func ReverseSlice(slice interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -852,9 +846,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Reduce">Reduce</span>
|
### <span id="Reduce">Reduce</span>
|
||||||
|
|
||||||
<p>Reduce slice, function signature should be func(index int, value1, value2 interface{}) interface{}.</p>
|
<p>Reduce slice, function signature should be func(index int, value1, value2 interface{}) interface{}.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -862,6 +855,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Reduce(slice, function, zero interface{}) interface{}
|
func Reduce(slice, function, zero interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -880,10 +874,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Shuffle">Shuffle</span>
|
### <span id="Shuffle">Shuffle</span>
|
||||||
|
|
||||||
<p>Creates an slice of shuffled values.</p>
|
<p>Creates an slice of shuffled values.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -891,6 +883,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Shuffle(slice interface{}) interface{}
|
func Shuffle(slice interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -906,9 +899,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SortByField">SortByField</span>
|
### <span id="SortByField">SortByField</span>
|
||||||
|
|
||||||
<p>Sort struct slice by field. Slice element should be struct, field type should be int, uint, string, or bool. Default sort type is ascending (asc), if descending order, set sortType to desc</p>
|
<p>Sort struct slice by field. Slice element should be struct, field type should be int, uint, string, or bool. Default sort type is ascending (asc), if descending order, set sortType to desc</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -916,6 +908,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SortByField(slice interface{}, field string, sortType ...string) error
|
func SortByField(slice interface{}, field string, sortType ...string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -949,9 +942,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Some">Some</span>
|
### <span id="Some">Some</span>
|
||||||
|
|
||||||
<p>Return true if any of the values in the list pass the predicate function, function signature should be func(index int, value interface{}) bool.</p>
|
<p>Return true if any of the values in the list pass the predicate function, function signature should be func(index int, value interface{}) bool.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -959,6 +951,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Some(slice, function interface{}) bool
|
func Some(slice, function interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -978,9 +971,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="StringSlice">StringSlice</span>
|
### <span id="StringSlice">StringSlice</span>
|
||||||
|
|
||||||
<p>Convert interface slice to string slice.</p>
|
<p>Convert interface slice to string slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -988,6 +980,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func StringSlice(slice interface{}) []string
|
func StringSlice(slice interface{}) []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1003,9 +996,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToSlice">ToSlice</span>
|
### <span id="ToSlice">ToSlice</span>
|
||||||
|
|
||||||
<p>Returns a slices of a variable parameter transformation</p>
|
<p>Returns a slices of a variable parameter transformation</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -1013,6 +1005,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToSlice(value ...interface{}) interface{}
|
func ToSlice(value ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1027,9 +1020,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToSlicePointer">ToSlicePointer</span>
|
### <span id="ToSlicePointer">ToSlicePointer</span>
|
||||||
|
|
||||||
<p>Returns a pointer to the slices of a variable parameter transformation</p>
|
<p>Returns a pointer to the slices of a variable parameter transformation</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -1037,6 +1029,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToSlicePointer(value ...interface{}) []*interface{}
|
func ToSlicePointer(value ...interface{}) []*interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1053,9 +1046,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Unique">Unique</span>
|
### <span id="Unique">Unique</span>
|
||||||
|
|
||||||
<p>Remove duplicate elements in slice.</p>
|
<p>Remove duplicate elements in slice.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -1063,6 +1055,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Unique(slice interface{}) interface{}
|
func Unique(slice interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1077,9 +1070,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UniqueBy">UniqueBy</span>
|
### <span id="UniqueBy">UniqueBy</span>
|
||||||
|
|
||||||
<p>Call iteratee func with every item of slice, then remove duplicated.</p>
|
<p>Call iteratee func with every item of slice, then remove duplicated.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -1087,6 +1079,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UniqueBy(slice, iteratee interface{}) interface{}
|
func UniqueBy(slice, iteratee interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1103,9 +1096,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Union">Union</span>
|
### <span id="Union">Union</span>
|
||||||
|
|
||||||
<p>Creates a slice of unique values, in order, from all given slices. using == for equality comparisons.</p>
|
<p>Creates a slice of unique values, in order, from all given slices. using == for equality comparisons.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -1113,6 +1105,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Union(slices ...interface{}) interface{}
|
func Union(slices ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1130,9 +1123,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpdateByIndex">UpdateByIndex</span>
|
### <span id="UpdateByIndex">UpdateByIndex</span>
|
||||||
|
|
||||||
<p>Update the slice element at index. if param index < 0 or index >= len(slice), will return error. </p>
|
<p>Update the slice element at index. if param index < 0 or index >= len(slice), will return error. </p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -1140,6 +1132,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error)
|
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1156,10 +1149,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Without">Without</span>
|
### <span id="Without">Without</span>
|
||||||
|
|
||||||
<p>Creates a slice excluding all given values. </p>
|
<p>Creates a slice excluding all given values. </p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -1167,6 +1158,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Without(slice interface{}, values ...interface{}) interface{}
|
func Without(slice interface{}, values ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1180,14 +1172,3 @@ func main() {
|
|||||||
fmt.Println(res) //[]int{3, 4, 5}
|
fmt.Println(res) //[]int{3, 4, 5}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Slice
|
# Slice
|
||||||
|
|
||||||
slice 包包含操作切片的方法集合。
|
slice 包包含操作切片的方法集合。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ slice包包含操作切片的方法集合。
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/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](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>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/slice"
|
"github.com/duke-git/lancet/slice"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [AppendIfAbsent](#AppendIfAbsent)
|
- [AppendIfAbsent](#AppendIfAbsent)
|
||||||
- [Contain](#Contain)
|
- [Contain](#Contain)
|
||||||
- [ContainSubSlice](#ContainSubSlice)
|
- [ContainSubSlice](#ContainSubSlice)
|
||||||
@@ -66,6 +68,7 @@ import (
|
|||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
### <span id="AppendIfAbsent">AppendIfAbsent</span>
|
### <span id="AppendIfAbsent">AppendIfAbsent</span>
|
||||||
|
|
||||||
<p>当前切片中不包含值时,将该值追加到切片中</p>
|
<p>当前切片中不包含值时,将该值追加到切片中</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -73,6 +76,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func AppendIfAbsent(slice interface{}, value interface{}) interface{}
|
func AppendIfAbsent(slice interface{}, value interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -91,9 +95,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Contain">Contain</span>
|
### <span id="Contain">Contain</span>
|
||||||
|
|
||||||
<p>判断slice是否包含value</p>
|
<p>判断slice是否包含value</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -101,6 +104,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Contain(iterableType interface{}, value interface{}) bool
|
func Contain(iterableType interface{}, value interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -115,8 +119,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainSubSlice">ContainSubSlice</span>
|
### <span id="ContainSubSlice">ContainSubSlice</span>
|
||||||
|
|
||||||
<p>判断slice是否包含subslice</p>
|
<p>判断slice是否包含subslice</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -124,6 +128,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ContainSubSlice(slice interface{}, subslice interface{}) bool
|
func ContainSubSlice(slice interface{}, subslice interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -138,10 +143,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Chunk">Chunk</span>
|
### <span id="Chunk">Chunk</span>
|
||||||
|
|
||||||
<p>按照size参数均分slice</p>
|
<p>按照size参数均分slice</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -149,6 +152,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Chunk(slice []interface{}, size int) [][]interface{}
|
func Chunk(slice []interface{}, size int) [][]interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -164,9 +168,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Compact">Compact</span>
|
### <span id="Compact">Compact</span>
|
||||||
|
|
||||||
<p>去除slice中的假值(false values are false, nil, 0, "")</p>
|
<p>去除slice中的假值(false values are false, nil, 0, "")</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -174,6 +177,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Compact(slice interface{}) interface{}
|
func Compact(slice interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -188,8 +192,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Concat">Concat</span>
|
### <span id="Concat">Concat</span>
|
||||||
|
|
||||||
<p>连接values到slice中,values类型可以是切片或多个值</p>
|
<p>连接values到slice中,values类型可以是切片或多个值</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -197,6 +201,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Concat(slice interface{}, values ...interface{}) interface{}
|
func Concat(slice interface{}, values ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -214,9 +219,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Count">Count</span>
|
### <span id="Count">Count</span>
|
||||||
|
|
||||||
<p>遍历切片,对每个元素执行函数function. 返回符合函数返回值为true的元素的个数,函数签名必须是func(index int, value interface{}) bool</p>
|
<p>遍历切片,对每个元素执行函数function. 返回符合函数返回值为true的元素的个数,函数签名必须是func(index int, value interface{}) bool</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -224,6 +228,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Count(slice, function interface{}) int
|
func Count(slice, function interface{}) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -243,10 +248,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Difference">Difference</span>
|
### <span id="Difference">Difference</span>
|
||||||
|
|
||||||
<p>创建一个切片,其元素不包含在另一个给定切片中</p>
|
<p>创建一个切片,其元素不包含在另一个给定切片中</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -254,6 +257,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Difference(slice1, slice2 interface{}) interface{}
|
func Difference(slice1, slice2 interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -271,10 +275,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DifferenceBy">DifferenceBy</span>
|
### <span id="DifferenceBy">DifferenceBy</span>
|
||||||
|
|
||||||
<p>在slice和comparedSlice中的每个元素调用iteratee函数,并比较它们的返回值,如果不想等返回在slice中对应的值</p>
|
<p>在slice和comparedSlice中的每个元素调用iteratee函数,并比较它们的返回值,如果不想等返回在slice中对应的值</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -282,6 +284,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func DifferenceBy(slice interface{}, comparedSlice interface{}, iterateeFn interface{}) interface{}
|
func DifferenceBy(slice interface{}, comparedSlice interface{}, iterateeFn interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -302,10 +305,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DeleteByIndex">DeleteByIndex</span>
|
### <span id="DeleteByIndex">DeleteByIndex</span>
|
||||||
|
|
||||||
<p>删除切片中从开始索引到结束索引-1的元素</p>
|
<p>删除切片中从开始索引到结束索引-1的元素</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -313,6 +314,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error)
|
func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -331,10 +333,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Drop">Drop</span>
|
### <span id="Drop">Drop</span>
|
||||||
|
|
||||||
<p>创建一个切片,当 n > 0 时从开头删除 n 个元素,或者当 n < 0 时从结尾删除 n 个元素</p>
|
<p>创建一个切片,当 n > 0 时从开头删除 n 个元素,或者当 n < 0 时从结尾删除 n 个元素</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -342,6 +342,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Drop(slice interface{}, n int) interface{}
|
func Drop(slice interface{}, n int) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -362,9 +363,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Equal">Equal</span>
|
### <span id="Equal">Equal</span>
|
||||||
|
|
||||||
<p>检查两个切片是否相等,相等条件:切片长度相同,元素顺序和值都相同</p>
|
<p>检查两个切片是否相等,相等条件:切片长度相同,元素顺序和值都相同</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -372,6 +372,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Equal(slice1, slice2 interface{}) bool
|
func Equal(slice1, slice2 interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -393,9 +394,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EqualWith">EqualWith</span>
|
### <span id="EqualWith">EqualWith</span>
|
||||||
|
|
||||||
<p>检查两个切片是否相等,相等条件:对两个切片的元素调用比较函数comparator,返回true。 comparator函数签名: func(a interface{}, b interface{}) bool</p>
|
<p>检查两个切片是否相等,相等条件:对两个切片的元素调用比较函数comparator,返回true。 comparator函数签名: func(a interface{}, b interface{}) bool</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -403,6 +403,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func EqualWith(slice1, slice2 interface{}, comparator interface{}) bool
|
func EqualWith(slice1, slice2 interface{}, comparator interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -425,9 +426,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Every">Every</span>
|
### <span id="Every">Every</span>
|
||||||
|
|
||||||
<p>如果切片中的所有值都通过谓词函数,则返回true。 函数签名应该是func(index int, value interface{}) bool</p>
|
<p>如果切片中的所有值都通过谓词函数,则返回true。 函数签名应该是func(index int, value interface{}) bool</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -435,6 +435,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Every(slice, function interface{}) bool
|
func Every(slice, function interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -454,10 +455,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Filter">Filter</span>
|
### <span id="Filter">Filter</span>
|
||||||
|
|
||||||
<p>返回与函数匹配的所有元素。 函数签名应该是 func(index int, value interface{}) bool</p>
|
<p>返回与函数匹配的所有元素。 函数签名应该是 func(index int, value interface{}) bool</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -465,6 +464,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Filter(slice, function interface{}) interface{}
|
func Filter(slice, function interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -484,9 +484,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Find">Find</span>
|
### <span id="Find">Find</span>
|
||||||
|
|
||||||
<p>遍历slice的元素,返回第一个通过function真值测试的元素。函数签名应该是 func(index int, value interface{}) bool</p>
|
<p>遍历slice的元素,返回第一个通过function真值测试的元素。函数签名应该是 func(index int, value interface{}) bool</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -494,6 +493,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Find(slice, function interface{}) (interface{}, bool)
|
func Find(slice, function interface{}) (interface{}, bool)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -514,10 +514,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="FindLast">FindLast</span>
|
### <span id="FindLast">FindLast</span>
|
||||||
|
|
||||||
<p>从头到尾遍历 slice 的元素,返回最后一个通过函数真值测试的元素。 函数签名应该是 func(index int, value interface{}) bool。</p>
|
<p>从头到尾遍历 slice 的元素,返回最后一个通过函数真值测试的元素。 函数签名应该是 func(index int, value interface{}) bool。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -525,6 +523,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FindLast(slice, function interface{}) (interface{}, bool)
|
func FindLast(slice, function interface{}) (interface{}, bool)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -545,9 +544,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="FlattenDeep">FlattenDeep</span>
|
### <span id="FlattenDeep">FlattenDeep</span>
|
||||||
|
|
||||||
<p>flattens slice recursive.</p>
|
<p>flattens slice recursive.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -555,6 +553,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func FlattenDeep(slice interface{}) interface{}
|
func FlattenDeep(slice interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -570,11 +569,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ForEach">ForEach</span>
|
### <span id="ForEach">ForEach</span>
|
||||||
|
|
||||||
<p>遍历slice的元素并为每个元素调用函数,函数签名应该是func(index int, value interface{})</p>
|
<p>遍历slice的元素并为每个元素调用函数,函数签名应该是func(index int, value interface{})</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -582,6 +578,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ForEach(slice, function interface{})
|
func ForEach(slice, function interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -600,10 +597,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GroupBy">GroupBy</span>
|
### <span id="GroupBy">GroupBy</span>
|
||||||
|
|
||||||
<p>迭代切片的元素,每个元素将按条件分组,返回两个切片。 函数签名应该是func(index int, value interface{}) bool</p>
|
<p>迭代切片的元素,每个元素将按条件分组,返回两个切片。 函数签名应该是func(index int, value interface{}) bool</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -611,6 +606,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GroupBy(slice, function interface{}) (interface{}, interface{})
|
func GroupBy(slice, function interface{}) (interface{}, interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -631,10 +627,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IntSlice">IntSlice</span>
|
### <span id="IntSlice">IntSlice</span>
|
||||||
|
|
||||||
<p>将接口切片转换为int切片</p>
|
<p>将接口切片转换为int切片</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -642,6 +636,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IntSlice(slice interface{}) []int
|
func IntSlice(slice interface{}) []int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -657,10 +652,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="InterfaceSlice">InterfaceSlice</span>
|
### <span id="InterfaceSlice">InterfaceSlice</span>
|
||||||
|
|
||||||
<p>将值转换为接口切片</p>
|
<p>将值转换为接口切片</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -668,6 +661,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func InterfaceSlice(slice interface{}) []interface{}
|
func InterfaceSlice(slice interface{}) []interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -683,10 +677,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Intersection">Intersection</span>
|
### <span id="Intersection">Intersection</span>
|
||||||
|
|
||||||
<p>多个切片的交集</p>
|
<p>多个切片的交集</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -694,6 +686,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Intersection(slices ...interface{}) interface{}
|
func Intersection(slices ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -711,9 +704,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IndexOf">IndexOf</span>
|
### <span id="IndexOf">IndexOf</span>
|
||||||
|
|
||||||
<p>返回在切片中找到值的第一个匹配项的索引,如果找不到值,则返回-1</p>
|
<p>返回在切片中找到值的第一个匹配项的索引,如果找不到值,则返回-1</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -721,6 +713,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IndexOf(slice, value interface{}) int
|
func IndexOf(slice, value interface{}) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -739,9 +732,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="LastIndexOf">LastIndexOf</span>
|
### <span id="LastIndexOf">LastIndexOf</span>
|
||||||
|
|
||||||
<p>返回在切片中找到最后一个值的索引,如果找不到该值,则返回-1</p>
|
<p>返回在切片中找到最后一个值的索引,如果找不到该值,则返回-1</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -749,6 +741,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func LastIndexOf(slice, value interface{}) int
|
func LastIndexOf(slice, value interface{}) int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -767,9 +760,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="InsertByIndex">InsertByIndex</span>
|
### <span id="InsertByIndex">InsertByIndex</span>
|
||||||
|
|
||||||
<p>将元素插入到索引处的切片中</p>
|
<p>将元素插入到索引处的切片中</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -777,6 +769,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error)
|
func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -796,10 +789,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Map">Map</span>
|
### <span id="Map">Map</span>
|
||||||
|
|
||||||
<p>通过运行函数slice中的每个元素来创建一个值切片,函数签名应该是func(index int, value interface{}) interface{}。</p>
|
<p>通过运行函数slice中的每个元素来创建一个值切片,函数签名应该是func(index int, value interface{}) interface{}。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -807,6 +798,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Map(slice, function interface{}) interface{}
|
func Map(slice, function interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -825,10 +817,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReverseSlice">ReverseSlice</span>
|
### <span id="ReverseSlice">ReverseSlice</span>
|
||||||
|
|
||||||
<p>反转切片中的元素顺序</p>
|
<p>反转切片中的元素顺序</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -836,6 +826,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ReverseSlice(slice interface{})
|
func ReverseSlice(slice interface{})
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -851,9 +842,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Reduce">Reduce</span>
|
### <span id="Reduce">Reduce</span>
|
||||||
|
|
||||||
<p>将slice中的元素运行函数,返回运行结果。函数签名应该是func(index int, value1, value2 interface{}) interface{}。</p>
|
<p>将slice中的元素运行函数,返回运行结果。函数签名应该是func(index int, value1, value2 interface{}) interface{}。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -861,6 +851,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Reduce(slice, function, zero interface{}) interface{}
|
func Reduce(slice, function, zero interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -879,10 +870,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Shuffle">Shuffle</span>
|
### <span id="Shuffle">Shuffle</span>
|
||||||
|
|
||||||
<p>随机打乱切片中的元素顺序</p>
|
<p>随机打乱切片中的元素顺序</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -890,6 +879,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Shuffle(slice interface{}) interface{}
|
func Shuffle(slice interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -905,9 +895,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SortByField">SortByField</span>
|
### <span id="SortByField">SortByField</span>
|
||||||
|
|
||||||
<p>按字段对结构切片进行排序。slice元素应为struct,字段类型应为int、uint、string或bool。 默认排序类型是升序(asc),如果是降序,设置 sortType 为 desc</p>
|
<p>按字段对结构切片进行排序。slice元素应为struct,字段类型应为int、uint、string或bool。 默认排序类型是升序(asc),如果是降序,设置 sortType 为 desc</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -915,6 +904,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SortByField(slice interface{}, field string, sortType ...string) error
|
func SortByField(slice interface{}, field string, sortType ...string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -948,9 +938,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Some">Some</span>
|
### <span id="Some">Some</span>
|
||||||
|
|
||||||
<p>如果列表中的任何值通过谓词函数,则返回true,函数签名应该是func(index int, value interface{}) bool .</p>
|
<p>如果列表中的任何值通过谓词函数,则返回true,函数签名应该是func(index int, value interface{}) bool .</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -958,6 +947,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Some(slice, function interface{}) bool
|
func Some(slice, function interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -977,9 +967,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="StringSlice">StringSlice</span>
|
### <span id="StringSlice">StringSlice</span>
|
||||||
|
|
||||||
<p>将接口切片转换为字符串切片</p>
|
<p>将接口切片转换为字符串切片</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -987,6 +976,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func StringSlice(slice interface{}) []string
|
func StringSlice(slice interface{}) []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1002,9 +992,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToSlice">ToSlice</span>
|
### <span id="ToSlice">ToSlice</span>
|
||||||
|
|
||||||
<p>将可变参数转为切片</p>
|
<p>将可变参数转为切片</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -1012,6 +1001,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToSlice(value ...interface{}) interface{}
|
func ToSlice(value ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1026,9 +1016,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToSlicePointer">ToSlicePointer</span>
|
### <span id="ToSlicePointer">ToSlicePointer</span>
|
||||||
|
|
||||||
<p>将可变参数转为指针切片</p>
|
<p>将可变参数转为指针切片</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -1036,6 +1025,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ToSlicePointer(value ...interface{}) []*interface{}
|
func ToSlicePointer(value ...interface{}) []*interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1052,9 +1042,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Unique">Unique</span>
|
### <span id="Unique">Unique</span>
|
||||||
|
|
||||||
<p>删除切片中的重复元素</p>
|
<p>删除切片中的重复元素</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -1062,6 +1051,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Unique(slice interface{}) interface{}
|
func Unique(slice interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1076,9 +1066,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UniqueBy">UniqueBy</span>
|
### <span id="UniqueBy">UniqueBy</span>
|
||||||
|
|
||||||
<p>对切片的每个项目调用iteratee函数,然后删除重复的</p>
|
<p>对切片的每个项目调用iteratee函数,然后删除重复的</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -1086,6 +1075,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UniqueBy(slice, iteratee interface{}) interface{}
|
func UniqueBy(slice, iteratee interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1102,8 +1092,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Union">Union</span>
|
### <span id="Union">Union</span>
|
||||||
|
|
||||||
<p>从所有给定的切片按顺序创建一个唯一值切片。 使用 == 进行相等比较。</p>
|
<p>从所有给定的切片按顺序创建一个唯一值切片。 使用 == 进行相等比较。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -1111,6 +1101,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Union(slices ...interface{}) interface{}
|
func Union(slices ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1128,9 +1119,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpdateByIndex">UpdateByIndex</span>
|
### <span id="UpdateByIndex">UpdateByIndex</span>
|
||||||
|
|
||||||
<p>更新索引处的切片元素。 如果 param index < 0 或 index >= len(slice),将返回错误</p>
|
<p>更新索引处的切片元素。 如果 param index < 0 或 index >= len(slice),将返回错误</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -1138,6 +1128,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error)
|
func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1154,10 +1145,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Without">Without</span>
|
### <span id="Without">Without</span>
|
||||||
|
|
||||||
<p>创建一个不包括所有给定值的切片</p>
|
<p>创建一个不包括所有给定值的切片</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -1165,6 +1154,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Without(slice interface{}, values ...interface{}) interface{}
|
func Without(slice interface{}, values ...interface{}) interface{}
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1178,14 +1168,3 @@ func main() {
|
|||||||
fmt.Println(res) //[]int{3, 4, 5}
|
fmt.Println(res) //[]int{3, 4, 5}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
272
docs/strutil.md
272
docs/strutil.md
@@ -1,4 +1,5 @@
|
|||||||
# Strutil
|
# Strutil
|
||||||
|
|
||||||
Package strutil contains some functions to manipulate string.
|
Package strutil contains some functions to manipulate string.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ Package strutil contains some functions to manipulate string.
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/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](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>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/strutil"
|
"github.com/duke-git/lancet/strutil"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [After](#After)
|
- [After](#After)
|
||||||
- [AfterLast](#AfterLast)
|
- [AfterLast](#AfterLast)
|
||||||
- [Before](#Before)
|
- [Before](#Before)
|
||||||
@@ -31,6 +33,7 @@ import (
|
|||||||
- [UpperKebabCase](#UpperKebabCase)
|
- [UpperKebabCase](#UpperKebabCase)
|
||||||
- [LowerFirst](#LowerFirst)
|
- [LowerFirst](#LowerFirst)
|
||||||
- [UpperFirst](#UpperFirst)
|
- [UpperFirst](#UpperFirst)
|
||||||
|
- [Pad](#Pad)
|
||||||
- [PadEnd](#PadEnd)
|
- [PadEnd](#PadEnd)
|
||||||
- [PadStart](#PadStart)
|
- [PadStart](#PadStart)
|
||||||
- [Reverse](#Reverse)
|
- [Reverse](#Reverse)
|
||||||
@@ -39,14 +42,15 @@ import (
|
|||||||
- [Wrap](#Wrap)
|
- [Wrap](#Wrap)
|
||||||
- [Unwrap](#Unwrap)
|
- [Unwrap](#Unwrap)
|
||||||
- [SplitEx](#SplitEx)
|
- [SplitEx](#SplitEx)
|
||||||
|
- [SplitWords](#SplitWords)
|
||||||
|
- [WordCount](#WordCount)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="After">After</span>
|
### <span id="After">After</span>
|
||||||
|
|
||||||
<p>Creates substring in source string after position when char first appear.</p>
|
<p>Creates substring in source string after position when char first appear.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -54,6 +58,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func After(s, char string) string
|
func After(s, char string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -74,9 +79,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AfterLast">AfterLast</span>
|
### <span id="AfterLast">AfterLast</span>
|
||||||
|
|
||||||
<p>Creates substring in source string after position when char last appear.</p>
|
<p>Creates substring in source string after position when char last appear.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -84,6 +88,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AfterLast(s, char string) string
|
func AfterLast(s, char string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -104,10 +109,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Before">Before</span>
|
### <span id="Before">Before</span>
|
||||||
|
|
||||||
<p>Creates substring in source string before position when char first appear.</p>
|
<p>Creates substring in source string before position when char first appear.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -115,6 +118,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Before(s, char string) string
|
func Before(s, char string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -135,10 +139,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeforeLast">BeforeLast</span>
|
### <span id="BeforeLast">BeforeLast</span>
|
||||||
|
|
||||||
<p>Creates substring in source string before position when char first appear.</p>
|
<p>Creates substring in source string before position when char first appear.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -146,6 +148,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeforeLast(s, char string) string
|
func BeforeLast(s, char string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -166,10 +169,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CamelCase">CamelCase</span>
|
### <span id="CamelCase">CamelCase</span>
|
||||||
|
|
||||||
<p>Coverts string to camelCase string, non letters and numbers will be ignored.</p>
|
<p>Coverts string to camelCase string, non letters and numbers will be ignored.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -177,6 +178,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CamelCase(s string) string
|
func CamelCase(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -202,10 +204,8 @@ func main() {
|
|||||||
fmt.Println(s4) //foo11Bar
|
fmt.Println(s4) //foo11Bar
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Capitalize">Capitalize</span>
|
### <span id="Capitalize">Capitalize</span>
|
||||||
|
|
||||||
<p>Convert the first character of a string to upper case.</p>
|
<p>Convert the first character of a string to upper case.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -213,6 +213,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Capitalize(s string) string
|
func Capitalize(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -233,9 +234,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsString">IsString</span>
|
### <span id="IsString">IsString</span>
|
||||||
|
|
||||||
<p>Check if the value's data type is string.</p>
|
<p>Check if the value's data type is string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -243,6 +243,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsString(v interface{}) bool
|
func IsString(v interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -261,9 +262,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="KebabCase">KebabCase</span>
|
### <span id="KebabCase">KebabCase</span>
|
||||||
|
|
||||||
<p>KebabCase covert string to kebab-case, non letters and numbers will be ignored.</p>
|
<p>KebabCase covert string to kebab-case, non letters and numbers will be ignored.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -271,6 +271,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func KebabCase(s string) string
|
func KebabCase(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -294,9 +295,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpperKebabCase">UpperKebabCase</span>
|
### <span id="UpperKebabCase">UpperKebabCase</span>
|
||||||
|
|
||||||
<p>UpperKebabCase covert string to upper KEBAB-CASE, non letters and numbers will be ignored.</p>
|
<p>UpperKebabCase covert string to upper KEBAB-CASE, non letters and numbers will be ignored.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -304,6 +304,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func KebabCase(s string) string
|
func KebabCase(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -327,9 +328,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="LowerFirst">LowerFirst</span>
|
### <span id="LowerFirst">LowerFirst</span>
|
||||||
|
|
||||||
<p>Convert the first character of string to lower case.</p>
|
<p>Convert the first character of string to lower case.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -337,6 +337,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func LowerFirst(s string) string
|
func LowerFirst(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -360,10 +361,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpperFirst">UpperFirst</span>
|
### <span id="UpperFirst">UpperFirst</span>
|
||||||
|
|
||||||
<p>Convert the first character of string to upper case.</p>
|
<p>Convert the first character of string to upper case.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -371,6 +370,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UpperFirst(s string) string
|
func UpperFirst(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -394,10 +394,53 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Pad">Pad</span>
|
||||||
|
|
||||||
|
<p>Pads string on the left and right side if it's shorter than size.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Pad(source string, size int, padStr string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.Pad("foo", 1, "bar")
|
||||||
|
result2 := strutil.Pad("foo", 2, "bar")
|
||||||
|
result3 := strutil.Pad("foo", 3, "bar")
|
||||||
|
result4 := strutil.Pad("foo", 4, "bar")
|
||||||
|
result5 := strutil.Pad("foo", 5, "bar")
|
||||||
|
result6 := strutil.Pad("foo", 6, "bar")
|
||||||
|
result7 := strutil.Pad("foo", 7, "bar")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
// Output:
|
||||||
|
// foo
|
||||||
|
// foo
|
||||||
|
// foo
|
||||||
|
// foob
|
||||||
|
// bfoob
|
||||||
|
// bfooba
|
||||||
|
// bafooba
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="PadEnd">PadEnd</span>
|
### <span id="PadEnd">PadEnd</span>
|
||||||
|
|
||||||
<p>Pads string on the right side if it's shorter than size.</p>
|
<p>Pads string on the right side if it's shorter than size.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -405,6 +448,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func PadEnd(source string, size int, padStr string) string
|
func PadEnd(source string, size int, padStr string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -414,24 +458,35 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.PadEnd("a", 1, "b")
|
result1 := strutil.PadEnd("foo", 1, "bar")
|
||||||
fmt.Println(s1) //a
|
result2 := strutil.PadEnd("foo", 2, "bar")
|
||||||
|
result3 := strutil.PadEnd("foo", 3, "bar")
|
||||||
|
result4 := strutil.PadEnd("foo", 4, "bar")
|
||||||
|
result5 := strutil.PadEnd("foo", 5, "bar")
|
||||||
|
result6 := strutil.PadEnd("foo", 6, "bar")
|
||||||
|
result7 := strutil.PadEnd("foo", 7, "bar")
|
||||||
|
|
||||||
s2 := strutil.PadEnd("a", 2, "b")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //ab
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
s3 := strutil.PadEnd("abcd", 6, "mno")
|
// Output:
|
||||||
fmt.Println(s3) //abcdmn
|
// foo
|
||||||
|
// foo
|
||||||
s4 := strutil.PadEnd("abc", 6, "ab")
|
// foo
|
||||||
fmt.Println(s4) //abcaba
|
// foob
|
||||||
|
// fooba
|
||||||
|
// foobar
|
||||||
|
// foobarb
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="PadStart">PadStart</span>
|
### <span id="PadStart">PadStart</span>
|
||||||
|
|
||||||
<p>Pads string on the left side if it's shorter than size.</p>
|
<p>Pads string on the left side if it's shorter than size.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -439,6 +494,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func PadStart(source string, size int, padStr string) string
|
func PadStart(source string, size int, padStr string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -448,24 +504,35 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.PadStart("a", 1, "b")
|
result1 := strutil.PadStart("foo", 1, "bar")
|
||||||
fmt.Println(s1) //a
|
result2 := strutil.PadStart("foo", 2, "bar")
|
||||||
|
result3 := strutil.PadStart("foo", 3, "bar")
|
||||||
|
result4 := strutil.PadStart("foo", 4, "bar")
|
||||||
|
result5 := strutil.PadStart("foo", 5, "bar")
|
||||||
|
result6 := strutil.PadStart("foo", 6, "bar")
|
||||||
|
result7 := strutil.PadStart("foo", 7, "bar")
|
||||||
|
|
||||||
s2 := strutil.PadStart("a", 2, "b")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //ba
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
s3 := strutil.PadStart("abcd", 6, "mno")
|
// Output:
|
||||||
fmt.Println(s3) //mnabcd
|
// foo
|
||||||
|
// foo
|
||||||
s4 := strutil.PadStart("abc", 6, "ab")
|
// foo
|
||||||
fmt.Println(s4) //abaabc
|
// bfoo
|
||||||
|
// bafoo
|
||||||
|
// barfoo
|
||||||
|
// barbfoo
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReverseStr">ReverseStr</span>
|
### <span id="ReverseStr">ReverseStr</span>
|
||||||
|
|
||||||
<p>Return string whose char order is reversed to the given string.</p>
|
<p>Return string whose char order is reversed to the given string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -473,6 +540,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ReverseStr(s string) string
|
func ReverseStr(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -490,8 +558,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="SnakeCase">SnakeCase</span>
|
### <span id="SnakeCase">SnakeCase</span>
|
||||||
|
|
||||||
<p>Coverts string to snake_case, non letters and numbers will be ignored.</p>
|
<p>Coverts string to snake_case, non letters and numbers will be ignored.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -499,6 +567,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SnakeCase(s string) string
|
func SnakeCase(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -525,8 +594,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpperSnakeCase">UpperSnakeCase</span>
|
### <span id="UpperSnakeCase">UpperSnakeCase</span>
|
||||||
|
|
||||||
<p>Coverts string to upper KEBAB-CASE, non letters and numbers will be ignored.</p>
|
<p>Coverts string to upper KEBAB-CASE, non letters and numbers will be ignored.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -534,6 +603,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SnakeCase(s string) string
|
func SnakeCase(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -560,9 +630,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Wrap">Wrap</span>
|
### <span id="Wrap">Wrap</span>
|
||||||
|
|
||||||
<p>Wrap a string with another string.</p>
|
<p>Wrap a string with another string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -570,6 +639,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Wrap(str string, wrapWith string) string
|
func Wrap(str string, wrapWith string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -596,10 +666,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Wrap">Wrap</span>
|
### <span id="Wrap">Wrap</span>
|
||||||
|
|
||||||
<p>Unwrap a given string from anther string. will change str value.</p>
|
<p>Unwrap a given string from anther string. will change str value.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -607,6 +675,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Unwrap(str string, wrapToken string) string
|
func Unwrap(str string, wrapToken string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -633,9 +702,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SplitEx">SplitEx</span>
|
### <span id="SplitEx">SplitEx</span>
|
||||||
|
|
||||||
<p>Split a given string whether the result contains empty string.</p>
|
<p>Split a given string whether the result contains empty string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -643,6 +711,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SplitEx(s, sep string, removeEmptyString bool) []string
|
func SplitEx(s, sep string, removeEmptyString bool) []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -669,7 +738,88 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="SplitWords">SplitWords</span>
|
||||||
|
|
||||||
|
<p>Splits a string into words, word only contains alphabetic characters.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SplitWords(s string) []string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.SplitWords("a word")
|
||||||
|
result2 := strutil.SplitWords("I'am a programmer")
|
||||||
|
result3 := strutil.SplitWords("Bonjour, je suis programmeur")
|
||||||
|
result4 := strutil.SplitWords("a -b-c' 'd'e")
|
||||||
|
result5 := strutil.SplitWords("你好,我是一名码农")
|
||||||
|
result6 := strutil.SplitWords("こんにちは,私はプログラマーです")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [a word]
|
||||||
|
// [I'am a programmer]
|
||||||
|
// [Bonjour je suis programmeur]
|
||||||
|
// [a b-c' d'e]
|
||||||
|
// []
|
||||||
|
// []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="WordCount">WordCount</span>
|
||||||
|
|
||||||
|
<p>Return the number of meaningful word, word only contains alphabetic characters.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WordCount(s string) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.WordCount("a word")
|
||||||
|
result2 := strutil.WordCount("I'am a programmer")
|
||||||
|
result3 := strutil.WordCount("Bonjour, je suis programmeur")
|
||||||
|
result4 := strutil.WordCount("a -b-c' 'd'e")
|
||||||
|
result5 := strutil.WordCount("你好,我是一名码农")
|
||||||
|
result6 := strutil.WordCount("こんにちは,私はプログラマーです")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2
|
||||||
|
// 3
|
||||||
|
// 4
|
||||||
|
// 3
|
||||||
|
// 0
|
||||||
|
// 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Strutil
|
# Strutil
|
||||||
|
|
||||||
strutil 包含处理字符串的相关函数。
|
strutil 包含处理字符串的相关函数。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ strutil包含处理字符串的相关函数。
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/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](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>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/strutil"
|
"github.com/duke-git/lancet/strutil"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [After](#After)
|
- [After](#After)
|
||||||
- [AfterLast](#AfterLast)
|
- [AfterLast](#AfterLast)
|
||||||
- [Before](#Before)
|
- [Before](#Before)
|
||||||
@@ -31,6 +33,7 @@ import (
|
|||||||
- [UpperKebabCase](#UpperKebabCase)
|
- [UpperKebabCase](#UpperKebabCase)
|
||||||
- [LowerFirst](#LowerFirst)
|
- [LowerFirst](#LowerFirst)
|
||||||
- [UpperFirst](#UpperFirst)
|
- [UpperFirst](#UpperFirst)
|
||||||
|
- [Pad](#Pad)
|
||||||
- [PadEnd](#PadEnd)
|
- [PadEnd](#PadEnd)
|
||||||
- [PadStart](#PadStart)
|
- [PadStart](#PadStart)
|
||||||
- [Reverse](#Reverse)
|
- [Reverse](#Reverse)
|
||||||
@@ -39,15 +42,15 @@ import (
|
|||||||
- [Wrap](#Wrap)
|
- [Wrap](#Wrap)
|
||||||
- [Unwrap](#Unwrap)
|
- [Unwrap](#Unwrap)
|
||||||
- [SplitEx](#SplitEx)
|
- [SplitEx](#SplitEx)
|
||||||
|
- [SplitWords](#SplitWords)
|
||||||
|
- [WordCount](#WordCount)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
|
|
||||||
## Documentation 文档
|
## Documentation 文档
|
||||||
|
|
||||||
|
|
||||||
### <span id="After">After</span>
|
### <span id="After">After</span>
|
||||||
|
|
||||||
<p>截取源字符串中char首次出现时的位置之后的子字符串</p>
|
<p>截取源字符串中char首次出现时的位置之后的子字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -55,6 +58,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func After(s, char string) string
|
func After(s, char string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -75,9 +79,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AfterLast">AfterLast</span>
|
### <span id="AfterLast">AfterLast</span>
|
||||||
|
|
||||||
<p>截取源字符串中char最后一次出现时的位置之后的子字符串</p>
|
<p>截取源字符串中char最后一次出现时的位置之后的子字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -85,6 +88,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func AfterLast(s, char string) string
|
func AfterLast(s, char string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -105,10 +109,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Before">Before</span>
|
### <span id="Before">Before</span>
|
||||||
|
|
||||||
<p>截取源字符串中char首次出现时的位置之前的子字符串</p>
|
<p>截取源字符串中char首次出现时的位置之前的子字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -116,6 +118,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Before(s, char string) string
|
func Before(s, char string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -136,10 +139,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeforeLast">BeforeLast</span>
|
### <span id="BeforeLast">BeforeLast</span>
|
||||||
|
|
||||||
<p>截取源字符串中char最后一次出现时的位置之前的子字符串</p>
|
<p>截取源字符串中char最后一次出现时的位置之前的子字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -147,6 +148,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func BeforeLast(s, char string) string
|
func BeforeLast(s, char string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -167,10 +169,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CamelCase">CamelCase</span>
|
### <span id="CamelCase">CamelCase</span>
|
||||||
|
|
||||||
<p>将字符串转换为驼峰式字符串, 非字母和数字会被忽略</p>
|
<p>将字符串转换为驼峰式字符串, 非字母和数字会被忽略</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -178,6 +178,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CamelCase(s string) string
|
func CamelCase(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -204,10 +205,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Capitalize">Capitalize</span>
|
### <span id="Capitalize">Capitalize</span>
|
||||||
|
|
||||||
<p>将字符串的第一个字符转换为大写</p>
|
<p>将字符串的第一个字符转换为大写</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -215,6 +214,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Capitalize(s string) string
|
func Capitalize(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -235,9 +235,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsString">IsString</span>
|
### <span id="IsString">IsString</span>
|
||||||
|
|
||||||
<p>检查值的数据类型是否为字符串</p>
|
<p>检查值的数据类型是否为字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -245,6 +244,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsString(v interface{}) bool
|
func IsString(v interface{}) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -263,8 +263,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="KebabCase">KebabCase</span>
|
### <span id="KebabCase">KebabCase</span>
|
||||||
|
|
||||||
<p>将字符串转换为kebab-case, 非字母和数字会被忽略</p>
|
<p>将字符串转换为kebab-case, 非字母和数字会被忽略</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -272,6 +272,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func KebabCase(s string) string
|
func KebabCase(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -295,9 +296,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpperKebabCase">UpperKebabCase</span>
|
### <span id="UpperKebabCase">UpperKebabCase</span>
|
||||||
|
|
||||||
<p>将字符串转换为大写KEBAB-CASE, 非字母和数字会被忽略</p>
|
<p>将字符串转换为大写KEBAB-CASE, 非字母和数字会被忽略</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -305,6 +305,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func KebabCase(s string) string
|
func KebabCase(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -328,8 +329,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="LowerFirst">LowerFirst</span>
|
### <span id="LowerFirst">LowerFirst</span>
|
||||||
|
|
||||||
<p>将字符串的第一个字符转换为小写</p>
|
<p>将字符串的第一个字符转换为小写</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -337,6 +338,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func LowerFirst(s string) string
|
func LowerFirst(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -360,10 +362,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpperFirst">UpperFirst</span>
|
### <span id="UpperFirst">UpperFirst</span>
|
||||||
|
|
||||||
<p>将字符串的第一个字符转换为大写</p>
|
<p>将字符串的第一个字符转换为大写</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -371,6 +371,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func UpperFirst(s string) string
|
func UpperFirst(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -394,18 +395,62 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="Pad">Pad</span>
|
||||||
|
|
||||||
|
<p>如果字符串长度短于size,则在左右两侧填充字符串。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func Pad(source string, size int, padStr string) string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.Pad("foo", 1, "bar")
|
||||||
|
result2 := strutil.Pad("foo", 2, "bar")
|
||||||
|
result3 := strutil.Pad("foo", 3, "bar")
|
||||||
|
result4 := strutil.Pad("foo", 4, "bar")
|
||||||
|
result5 := strutil.Pad("foo", 5, "bar")
|
||||||
|
result6 := strutil.Pad("foo", 6, "bar")
|
||||||
|
result7 := strutil.Pad("foo", 7, "bar")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
// Output:
|
||||||
|
// foo
|
||||||
|
// foo
|
||||||
|
// foo
|
||||||
|
// foob
|
||||||
|
// bfoob
|
||||||
|
// bfooba
|
||||||
|
// bafooba
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="PadEnd">PadEnd</span>
|
### <span id="PadEnd">PadEnd</span>
|
||||||
<p>如果字符串长度短于size,则在右侧填充字符串</p>
|
|
||||||
|
<p>如果字符串长度短于size,则在右侧填充字符串。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func PadEnd(source string, size int, padStr string) string
|
func PadEnd(source string, size int, padStr string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -414,32 +459,44 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.PadEnd("a", 1, "b")
|
result1 := strutil.PadEnd("foo", 1, "bar")
|
||||||
fmt.Println(s1) //a
|
result2 := strutil.PadEnd("foo", 2, "bar")
|
||||||
|
result3 := strutil.PadEnd("foo", 3, "bar")
|
||||||
|
result4 := strutil.PadEnd("foo", 4, "bar")
|
||||||
|
result5 := strutil.PadEnd("foo", 5, "bar")
|
||||||
|
result6 := strutil.PadEnd("foo", 6, "bar")
|
||||||
|
result7 := strutil.PadEnd("foo", 7, "bar")
|
||||||
|
|
||||||
s2 := strutil.PadEnd("a", 2, "b")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //ab
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
s3 := strutil.PadEnd("abcd", 6, "mno")
|
// Output:
|
||||||
fmt.Println(s3) //abcdmn
|
// foo
|
||||||
|
// foo
|
||||||
s4 := strutil.PadEnd("abc", 6, "ab")
|
// foo
|
||||||
fmt.Println(s4) //abcaba
|
// foob
|
||||||
|
// fooba
|
||||||
|
// foobar
|
||||||
|
// foobarb
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="PadStart">PadStart</span>
|
### <span id="PadStart">PadStart</span>
|
||||||
<p>如果字符串长度短于size,则在左侧填充字符串</p>
|
|
||||||
|
<p>如果字符串长度短于size,则在左侧填充字符串。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func PadStart(source string, size int, padStr string) string
|
func PadStart(source string, size int, padStr string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -448,24 +505,35 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.PadStart("a", 1, "b")
|
result1 := strutil.PadStart("foo", 1, "bar")
|
||||||
fmt.Println(s1) //a
|
result2 := strutil.PadStart("foo", 2, "bar")
|
||||||
|
result3 := strutil.PadStart("foo", 3, "bar")
|
||||||
|
result4 := strutil.PadStart("foo", 4, "bar")
|
||||||
|
result5 := strutil.PadStart("foo", 5, "bar")
|
||||||
|
result6 := strutil.PadStart("foo", 6, "bar")
|
||||||
|
result7 := strutil.PadStart("foo", 7, "bar")
|
||||||
|
|
||||||
s2 := strutil.PadStart("a", 2, "b")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //ba
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
s3 := strutil.PadStart("abcd", 6, "mno")
|
// Output:
|
||||||
fmt.Println(s3) //mnabcd
|
// foo
|
||||||
|
// foo
|
||||||
s4 := strutil.PadStart("abc", 6, "ab")
|
// foo
|
||||||
fmt.Println(s4) //abaabc
|
// bfoo
|
||||||
|
// bafoo
|
||||||
|
// barfoo
|
||||||
|
// barbfoo
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ReverseStr">ReverseStr</span>
|
### <span id="ReverseStr">ReverseStr</span>
|
||||||
|
|
||||||
<p>返回字符顺序与给定字符串相反的字符串</p>
|
<p>返回字符顺序与给定字符串相反的字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -473,6 +541,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ReverseStr(s string) string
|
func ReverseStr(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -490,8 +559,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="SnakeCase">SnakeCase</span>
|
### <span id="SnakeCase">SnakeCase</span>
|
||||||
|
|
||||||
<p>将字符串转换为snake_case形式, 非字母和数字会被忽略</p>
|
<p>将字符串转换为snake_case形式, 非字母和数字会被忽略</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -499,6 +568,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SnakeCase(s string) string
|
func SnakeCase(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -525,8 +595,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpperSnakeCase">UpperSnakeCase</span>
|
### <span id="UpperSnakeCase">UpperSnakeCase</span>
|
||||||
|
|
||||||
<p>将字符串转换为大写SNAKE_CASE形式, 非字母和数字会被忽略</p>
|
<p>将字符串转换为大写SNAKE_CASE形式, 非字母和数字会被忽略</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -534,6 +604,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SnakeCase(s string) string
|
func SnakeCase(s string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -560,9 +631,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Wrap">Wrap</span>
|
### <span id="Wrap">Wrap</span>
|
||||||
|
|
||||||
<p>用另一个字符串包裹一个字符串</p>
|
<p>用另一个字符串包裹一个字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -570,6 +640,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Wrap(str string, wrapWith string) string
|
func Wrap(str string, wrapWith string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -596,10 +667,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Unwrap">Unwrap</span>
|
### <span id="Unwrap">Unwrap</span>
|
||||||
|
|
||||||
<p>用另一个字符串解开包裹一个字符串</p>
|
<p>用另一个字符串解开包裹一个字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -607,6 +676,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func Unwrap(str string, wrapToken string) string
|
func Unwrap(str string, wrapToken string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -633,9 +703,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SplitEx">SplitEx</span>
|
### <span id="SplitEx">SplitEx</span>
|
||||||
|
|
||||||
<p>分割字符串为切片,removeEmptyString参数指定是否去除空字符串</p>
|
<p>分割字符串为切片,removeEmptyString参数指定是否去除空字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -643,6 +712,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SplitEx(s, sep string, removeEmptyString bool) []string
|
func SplitEx(s, sep string, removeEmptyString bool) []string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -669,8 +739,88 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="SplitWords">SplitWords</span>
|
||||||
|
|
||||||
|
<p>将字符串拆分为单词,只支持字母字符单词。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func SplitWords(s string) []string
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.SplitWords("a word")
|
||||||
|
result2 := strutil.SplitWords("I'am a programmer")
|
||||||
|
result3 := strutil.SplitWords("Bonjour, je suis programmeur")
|
||||||
|
result4 := strutil.SplitWords("a -b-c' 'd'e")
|
||||||
|
result5 := strutil.SplitWords("你好,我是一名码农")
|
||||||
|
result6 := strutil.SplitWords("こんにちは,私はプログラマーです")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [a word]
|
||||||
|
// [I'am a programmer]
|
||||||
|
// [Bonjour je suis programmeur]
|
||||||
|
// [a b-c' d'e]
|
||||||
|
// []
|
||||||
|
// []
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="WordCount">WordCount</span>
|
||||||
|
|
||||||
|
<p>返回有意义单词的数量,只支持字母字符单词。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func WordCount(s string) int
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
result1 := strutil.WordCount("a word")
|
||||||
|
result2 := strutil.WordCount("I'am a programmer")
|
||||||
|
result3 := strutil.WordCount("Bonjour, je suis programmeur")
|
||||||
|
result4 := strutil.WordCount("a -b-c' 'd'e")
|
||||||
|
result5 := strutil.WordCount("你好,我是一名码农")
|
||||||
|
result6 := strutil.WordCount("こんにちは,私はプログラマーです")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2
|
||||||
|
// 3
|
||||||
|
// 4
|
||||||
|
// 3
|
||||||
|
// 0
|
||||||
|
// 0
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# System
|
# System
|
||||||
|
|
||||||
Package system contains some functions about os, runtime, shell command.
|
Package system contains some functions about os, runtime, shell command.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ Package system contains some functions about os, runtime, shell command.
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/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](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>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/system"
|
"github.com/duke-git/lancet/system"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [IsWindows](#IsWindows)
|
- [IsWindows](#IsWindows)
|
||||||
- [IsLinux](#IsLinux)
|
- [IsLinux](#IsLinux)
|
||||||
- [IsMac](#IsMac)
|
- [IsMac](#IsMac)
|
||||||
@@ -30,13 +32,12 @@ import (
|
|||||||
- [ExecCommand](#ExecCommand)
|
- [ExecCommand](#ExecCommand)
|
||||||
- [GetOsBits](#GetOsBits)
|
- [GetOsBits](#GetOsBits)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsWindows">IsWindows</span>
|
### <span id="IsWindows">IsWindows</span>
|
||||||
|
|
||||||
<p>Check if current os is windows.</p>
|
<p>Check if current os is windows.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -44,6 +45,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func IsWindows() bool
|
func IsWindows() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -58,10 +60,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsLinux">IsLinux</span>
|
### <span id="IsLinux">IsLinux</span>
|
||||||
|
|
||||||
<p>Check if current os is linux.</p>
|
<p>Check if current os is linux.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -69,6 +69,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsLinux() bool
|
func IsLinux() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -83,9 +84,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsMac">IsMac</span>
|
### <span id="IsMac">IsMac</span>
|
||||||
|
|
||||||
<p>Check if current os is macos.</p>
|
<p>Check if current os is macos.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -93,6 +93,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsMac() bool
|
func IsMac() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -107,9 +108,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetOsEnv">GetOsEnv</span>
|
### <span id="GetOsEnv">GetOsEnv</span>
|
||||||
|
|
||||||
<p>Gets the value of the environment variable named by the key.</p>
|
<p>Gets the value of the environment variable named by the key.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -117,6 +117,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetOsEnv(key string) string
|
func GetOsEnv(key string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -131,9 +132,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SetOsEnv">SetOsEnv</span>
|
### <span id="SetOsEnv">SetOsEnv</span>
|
||||||
|
|
||||||
<p>Sets the value of the environment variable named by the key.</p>
|
<p>Sets the value of the environment variable named by the key.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -141,6 +141,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SetOsEnv(key, value string) error
|
func SetOsEnv(key, value string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -155,10 +156,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RemoveOsEnv">RemoveOsEnv</span>
|
### <span id="RemoveOsEnv">RemoveOsEnv</span>
|
||||||
|
|
||||||
<p>Remove a single environment variable.</p>
|
<p>Remove a single environment variable.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -166,6 +165,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RemoveOsEnv(key string) error
|
func RemoveOsEnv(key string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -182,9 +182,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CompareOsEnv">CompareOsEnv</span>
|
### <span id="CompareOsEnv">CompareOsEnv</span>
|
||||||
|
|
||||||
<p>Get env named by the key and compare it with comparedEnv.</p>
|
<p>Get env named by the key and compare it with comparedEnv.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -192,6 +191,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CompareOsEnv(key, comparedEnv string) bool
|
func CompareOsEnv(key, comparedEnv string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -207,10 +207,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ExecCommand">CompareOsEnv</span>
|
### <span id="ExecCommand">CompareOsEnv</span>
|
||||||
|
|
||||||
<p>use shell /bin/bash -c(linux) or cmd (windows) to execute command.</p>
|
<p>use shell /bin/bash -c(linux) or cmd (windows) to execute command.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -218,6 +216,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ExecCommand(command string) (stdout, stderr string, err error)
|
func ExecCommand(command string) (stdout, stderr string, err error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -234,9 +233,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetOsBits">GetOsBits</span>
|
### <span id="GetOsBits">GetOsBits</span>
|
||||||
|
|
||||||
<p>获取当前操作系统位数,返回32或64</p>
|
<p>获取当前操作系统位数,返回32或64</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -244,6 +242,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetOsBits() int
|
func GetOsBits() int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -257,7 +256,3 @@ func main() {
|
|||||||
fmt.Println(osBit)
|
fmt.Println(osBit)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# System
|
# System
|
||||||
|
|
||||||
system 包含 os, runtime, shell command 相关函数。
|
system 包含 os, runtime, shell command 相关函数。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ system包含os, runtime, shell command相关函数。
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/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](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>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/system"
|
"github.com/duke-git/lancet/system"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
|
|
||||||
- [IsWindows](#IsWindows)
|
- [IsWindows](#IsWindows)
|
||||||
- [IsLinux](#IsLinux)
|
- [IsLinux](#IsLinux)
|
||||||
- [IsMac](#IsMac)
|
- [IsMac](#IsMac)
|
||||||
@@ -30,13 +32,12 @@ import (
|
|||||||
- [ExecCommand](#ExecCommand)
|
- [ExecCommand](#ExecCommand)
|
||||||
- [GetOsBits](#GetOsBits)
|
- [GetOsBits](#GetOsBits)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation 文档
|
## Documentation 文档
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsWindows">IsWindows</span>
|
### <span id="IsWindows">IsWindows</span>
|
||||||
|
|
||||||
<p>检查当前操作系统是否是windows</p>
|
<p>检查当前操作系统是否是windows</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -44,6 +45,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func IsWindows() bool
|
func IsWindows() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -58,10 +60,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsLinux">IsLinux</span>
|
### <span id="IsLinux">IsLinux</span>
|
||||||
|
|
||||||
<p>检查当前操作系统是否是linux</p>
|
<p>检查当前操作系统是否是linux</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -69,6 +69,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsLinux() bool
|
func IsLinux() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -83,9 +84,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsMac">IsMac</span>
|
### <span id="IsMac">IsMac</span>
|
||||||
|
|
||||||
<p>检查当前操作系统是否是macos</p>
|
<p>检查当前操作系统是否是macos</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -93,6 +93,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsMac() bool
|
func IsMac() bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -107,9 +108,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetOsEnv">GetOsEnv</span>
|
### <span id="GetOsEnv">GetOsEnv</span>
|
||||||
|
|
||||||
<p>获取key命名的环境变量的值</p>
|
<p>获取key命名的环境变量的值</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -117,6 +117,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetOsEnv(key string) string
|
func GetOsEnv(key string) string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -131,9 +132,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SetOsEnv">SetOsEnv</span>
|
### <span id="SetOsEnv">SetOsEnv</span>
|
||||||
|
|
||||||
<p>设置由key命名的环境变量的值</p>
|
<p>设置由key命名的环境变量的值</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -141,6 +141,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func SetOsEnv(key, value string) error
|
func SetOsEnv(key, value string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -155,10 +156,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RemoveOsEnv">RemoveOsEnv</span>
|
### <span id="RemoveOsEnv">RemoveOsEnv</span>
|
||||||
|
|
||||||
<p>删除单个环境变量</p>
|
<p>删除单个环境变量</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -166,6 +165,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func RemoveOsEnv(key string) error
|
func RemoveOsEnv(key string) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -182,9 +182,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CompareOsEnv">CompareOsEnv</span>
|
### <span id="CompareOsEnv">CompareOsEnv</span>
|
||||||
|
|
||||||
<p>获取key命名的环境变量值并与compareEnv进行比较</p>
|
<p>获取key命名的环境变量值并与compareEnv进行比较</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -192,6 +191,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func CompareOsEnv(key, comparedEnv string) bool
|
func CompareOsEnv(key, comparedEnv string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -207,10 +207,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ExecCommand">CompareOsEnv</span>
|
### <span id="ExecCommand">CompareOsEnv</span>
|
||||||
|
|
||||||
<p>使用shell /bin/bash -c(linux) 或 cmd (windows) 执行shell命令</p>
|
<p>使用shell /bin/bash -c(linux) 或 cmd (windows) 执行shell命令</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -218,6 +216,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ExecCommand(command string) (stdout, stderr string, err error)
|
func ExecCommand(command string) (stdout, stderr string, err error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -234,9 +233,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GetOsBits">GetOsBits</span>
|
### <span id="GetOsBits">GetOsBits</span>
|
||||||
|
|
||||||
<p>Get current os bits, 32bit or 64bit. return 32 or 64</p>
|
<p>Get current os bits, 32bit or 64bit. return 32 or 64</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -244,6 +242,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func GetOsBits() int
|
func GetOsBits() int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -257,7 +256,3 @@ func main() {
|
|||||||
fmt.Println(osBit)
|
fmt.Println(osBit)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Validator
|
# Validator
|
||||||
|
|
||||||
Package validator contains some functions for data validation.
|
Package validator contains some functions for data validation.
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ Package validator contains some functions for data validation.
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/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](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>
|
||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/validator"
|
"github.com/duke-git/lancet/validator"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Index
|
## Index
|
||||||
|
|
||||||
- [ContainChinese](#ContainChinese)
|
- [ContainChinese](#ContainChinese)
|
||||||
- [ContainLetter](#ContainLetter)
|
- [ContainLetter](#ContainLetter)
|
||||||
- [ContainLower](#ContainLower)
|
- [ContainLower](#ContainLower)
|
||||||
@@ -53,8 +55,8 @@ import (
|
|||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainChinese">ContainChinese</span>
|
### <span id="ContainChinese">ContainChinese</span>
|
||||||
|
|
||||||
<p>Check if the string contain mandarin chinese.</p>
|
<p>Check if the string contain mandarin chinese.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -62,6 +64,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ContainChinese(s string) bool
|
func ContainChinese(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -82,9 +85,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainLetter">ContainLetter</span>
|
### <span id="ContainLetter">ContainLetter</span>
|
||||||
|
|
||||||
<p>Check if the string contain at least one letter.</p>
|
<p>Check if the string contain at least one letter.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -92,6 +94,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ContainLetter(str string) bool
|
func ContainLetter(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -112,10 +115,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainLower">ContainLower</span>
|
### <span id="ContainLower">ContainLower</span>
|
||||||
|
|
||||||
<p>Check if the string contain at least one lower case letter a-z.</p>
|
<p>Check if the string contain at least one lower case letter a-z.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -123,6 +124,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ContainLower(str string) bool
|
func ContainLower(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -143,10 +145,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainUpper">ContainUpper</span>
|
### <span id="ContainUpper">ContainUpper</span>
|
||||||
|
|
||||||
<p>Check if the string contain at least one upper case letter A-Z.</p>
|
<p>Check if the string contain at least one upper case letter A-Z.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -154,6 +154,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ContainUpper(str string) bool
|
func ContainUpper(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -174,10 +175,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsAlpha">IsAlpha</span>
|
### <span id="IsAlpha">IsAlpha</span>
|
||||||
|
|
||||||
<p>Check if the string contains only letters (a-zA-Z).</p>
|
<p>Check if the string contains only letters (a-zA-Z).</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -185,6 +184,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsAlpha(s string) bool
|
func IsAlpha(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -206,6 +206,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="IsAllUpper">IsAllUpper</span>
|
### <span id="IsAllUpper">IsAllUpper</span>
|
||||||
|
|
||||||
<p>Check if string is all upper case letters A-Z.</p>
|
<p>Check if string is all upper case letters A-Z.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -213,6 +214,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsAllUpper(str string) bool
|
func IsAllUpper(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -230,10 +232,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsAllLower">IsAllLower</span>
|
### <span id="IsAllLower">IsAllLower</span>
|
||||||
|
|
||||||
<p>Check if string is all lower case letters a-z.</p>
|
<p>Check if string is all lower case letters a-z.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -241,6 +241,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsAllLower(str string) bool
|
func IsAllLower(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -258,10 +259,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsBase64">IsBase64</span>
|
### <span id="IsBase64">IsBase64</span>
|
||||||
|
|
||||||
<p>Check if the string is base64 string.</p>
|
<p>Check if the string is base64 string.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -269,6 +268,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsBase64(base64 string) bool
|
func IsBase64(base64 string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -286,10 +286,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsChineseMobile">IsChineseMobile</span>
|
### <span id="IsChineseMobile">IsChineseMobile</span>
|
||||||
|
|
||||||
<p>Check if the string is valid chinese mobile number.</p>
|
<p>Check if the string is valid chinese mobile number.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -297,6 +295,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsChineseMobile(mobileNum string) bool
|
func IsChineseMobile(mobileNum string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -314,9 +313,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsChineseIdNum">IsChineseIdNum</span>
|
### <span id="IsChineseIdNum">IsChineseIdNum</span>
|
||||||
|
|
||||||
<p>Check if the string is chinese id number.</p>
|
<p>Check if the string is chinese id number.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -324,6 +322,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsChineseIdNum(id string) bool
|
func IsChineseIdNum(id string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -341,10 +340,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsChinesePhone">IsChinesePhone</span>
|
### <span id="IsChinesePhone">IsChinesePhone</span>
|
||||||
|
|
||||||
<p>Check if the string is chinese phone number.</p>
|
<p>Check if the string is chinese phone number.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -352,6 +349,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsChinesePhone(phone string) bool
|
func IsChinesePhone(phone string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -369,10 +367,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsCreditCard">IsCreditCard</span>
|
### <span id="IsCreditCard">IsCreditCard</span>
|
||||||
|
|
||||||
<p>Check if the string is credit card.</p>
|
<p>Check if the string is credit card.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -380,6 +376,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsCreditCard(creditCart string) bool
|
func IsCreditCard(creditCart string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -397,10 +394,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsDns">IsDns</span>
|
### <span id="IsDns">IsDns</span>
|
||||||
|
|
||||||
<p>Check if the string is valid dns.</p>
|
<p>Check if the string is valid dns.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -408,6 +403,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsDns(dns string) bool
|
func IsDns(dns string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -428,10 +424,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsEmail">IsEmail</span>
|
### <span id="IsEmail">IsEmail</span>
|
||||||
|
|
||||||
<p>Check if the string is email address.</p>
|
<p>Check if the string is email address.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -439,6 +433,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsEmail(email string) bool
|
func IsEmail(email string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -456,11 +451,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsEmptyString">IsEmptyString</span>
|
### <span id="IsEmptyString">IsEmptyString</span>
|
||||||
|
|
||||||
<p>Check if the string is empty or not.</p>
|
<p>Check if the string is empty or not.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -468,6 +460,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsEmptyString(s string) bool
|
func IsEmptyString(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -485,10 +478,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsFloatStr">IsFloatStr</span>
|
### <span id="IsFloatStr">IsFloatStr</span>
|
||||||
|
|
||||||
<p>Check if the string can convert to a float.</p>
|
<p>Check if the string can convert to a float.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -496,6 +487,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsFloatStr(s string) bool
|
func IsFloatStr(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -514,10 +506,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsNumberStr">IsNumberStr</span>
|
### <span id="IsNumberStr">IsNumberStr</span>
|
||||||
|
|
||||||
<p>Check if the string can convert to a number.</p>
|
<p>Check if the string can convert to a number.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -525,6 +515,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsNumberStr(s string) bool
|
func IsNumberStr(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -543,10 +534,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsJSON">IsJSON</span>
|
### <span id="IsJSON">IsJSON</span>
|
||||||
|
|
||||||
<p>Check if the string is valid JSON.</p>
|
<p>Check if the string is valid JSON.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -554,6 +543,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsJSON(str string) bool
|
func IsJSON(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -572,10 +562,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsRegexMatch">IsRegexMatch</span>
|
### <span id="IsRegexMatch">IsRegexMatch</span>
|
||||||
|
|
||||||
<p>Check if the string match the regexp.</p>
|
<p>Check if the string match the regexp.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -583,6 +571,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsRegexMatch(s, regex string) bool
|
func IsRegexMatch(s, regex string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -598,10 +587,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsIntStr">IsIntStr</span>
|
### <span id="IsIntStr">IsIntStr</span>
|
||||||
|
|
||||||
<p>Check if the string can convert to a integer.</p>
|
<p>Check if the string can convert to a integer.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -609,6 +596,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsIntStr(s string) bool
|
func IsIntStr(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -625,10 +613,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsIp">IsIp</span>
|
### <span id="IsIp">IsIp</span>
|
||||||
|
|
||||||
<p>Check if the string is a ip address.</p>
|
<p>Check if the string is a ip address.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -636,6 +622,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsIp(ipstr string) bool
|
func IsIp(ipstr string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -652,10 +639,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsIpV4">IsIpV4</span>
|
### <span id="IsIpV4">IsIpV4</span>
|
||||||
|
|
||||||
<p>Check if the string is a ipv4 address.</p>
|
<p>Check if the string is a ipv4 address.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -663,6 +648,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsIpV4(ipstr string) bool
|
func IsIpV4(ipstr string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -679,10 +665,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsIpV6">IsIpV6</span>
|
### <span id="IsIpV6">IsIpV6</span>
|
||||||
|
|
||||||
<p>Check if the string is a ipv6 address.</p>
|
<p>Check if the string is a ipv6 address.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -690,6 +674,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsIpV6(ipstr string) bool
|
func IsIpV6(ipstr string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -706,10 +691,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsStrongPassword">IsStrongPassword</span>
|
### <span id="IsStrongPassword">IsStrongPassword</span>
|
||||||
|
|
||||||
<p>Check if the string is strong password (alpha(lower+upper) + number + special chars(!@#$%^&*()?><)).</p>
|
<p>Check if the string is strong password (alpha(lower+upper) + number + special chars(!@#$%^&*()?><)).</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -717,6 +700,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsStrongPassword(password string, length int) bool
|
func IsStrongPassword(password string, length int) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -734,10 +718,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsUrl">IsUrl</span>
|
### <span id="IsUrl">IsUrl</span>
|
||||||
|
|
||||||
<p>Check if the string is url.</p>
|
<p>Check if the string is url.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -745,6 +727,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsUrl(str string) bool
|
func IsUrl(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -761,10 +744,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsWeakPassword">IsWeakPassword</span>
|
### <span id="IsWeakPassword">IsWeakPassword</span>
|
||||||
|
|
||||||
<p>Check if the string is weak password(only letter or only number or letter + number)
|
<p>Check if the string is weak password(only letter or only number or letter + number)
|
||||||
.</p>
|
.</p>
|
||||||
|
|
||||||
@@ -773,6 +754,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsWeakPassword(password string, length int) bool
|
func IsWeakPassword(password string, length int) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -790,6 +772,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="IsZeroValue">IsZeroValue</span>
|
### <span id="IsZeroValue">IsZeroValue</span>
|
||||||
|
|
||||||
<p>Checks if passed value is a zero value.</p>
|
<p>Checks if passed value is a zero value.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -797,6 +780,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsZeroValue(value any) bool
|
func IsZeroValue(value any) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -817,9 +801,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsGBK">IsGBK</span>
|
### <span id="IsGBK">IsGBK</span>
|
||||||
|
|
||||||
<p>Checks if data encoding is gbk(Chinese character internal code extension specification). this function is implemented by whether double bytes fall within the encoding range of gbk,while each byte of utf-8 encoding format falls within the encoding range of gbk.Therefore, utf8.valid() should be called first to check whether it is not utf-8 encoding and then call IsGBK() to check gbk encoding. like the example.</p>
|
<p>Checks if data encoding is gbk(Chinese character internal code extension specification). this function is implemented by whether double bytes fall within the encoding range of gbk,while each byte of utf-8 encoding format falls within the encoding range of gbk.Therefore, utf8.valid() should be called first to check whether it is not utf-8 encoding and then call IsGBK() to check gbk encoding. like the example.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
@@ -827,6 +810,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsGBK(data []byte) bool
|
func IsGBK(data []byte) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -847,11 +831,3 @@ func main() {
|
|||||||
fmt.Println("data encoding is unknown")
|
fmt.Println("data encoding is unknown")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
# Validator
|
# Validator
|
||||||
|
|
||||||
validator 验证器包,包含常用字符串格式验证函数。
|
validator 验证器包,包含常用字符串格式验证函数。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
@@ -7,10 +8,10 @@ validator验证器包,包含常用字符串格式验证函数。
|
|||||||
|
|
||||||
[https://github.com/duke-git/lancet/blob/v1/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](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>
|
||||||
|
|
||||||
## 用法:
|
## 用法:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
"github.com/duke-git/lancet/validator"
|
"github.com/duke-git/lancet/validator"
|
||||||
@@ -20,6 +21,7 @@ import (
|
|||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## 目录:
|
## 目录:
|
||||||
|
|
||||||
- [ContainChinese](#ContainChinese)
|
- [ContainChinese](#ContainChinese)
|
||||||
- [ContainLetter](#ContainLetter)
|
- [ContainLetter](#ContainLetter)
|
||||||
- [ContainLower](#ContainLower)
|
- [ContainLower](#ContainLower)
|
||||||
@@ -53,8 +55,8 @@ import (
|
|||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainChinese">ContainChinese</span>
|
### <span id="ContainChinese">ContainChinese</span>
|
||||||
|
|
||||||
<p>验证字符串是否包含中文字符</p>
|
<p>验证字符串是否包含中文字符</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -62,6 +64,7 @@ import (
|
|||||||
```go
|
```go
|
||||||
func ContainChinese(s string) bool
|
func ContainChinese(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -82,9 +85,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainLetter">ContainLetter</span>
|
### <span id="ContainLetter">ContainLetter</span>
|
||||||
|
|
||||||
<p>验证字符串是否包含至少一个英文字母</p>
|
<p>验证字符串是否包含至少一个英文字母</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -92,6 +94,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ContainLetter(str string) bool
|
func ContainLetter(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -112,10 +115,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainLower">ContainLower</span>
|
### <span id="ContainLower">ContainLower</span>
|
||||||
|
|
||||||
<p>验证字符串是否包含至少一个英文小写字母</p>
|
<p>验证字符串是否包含至少一个英文小写字母</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -123,6 +124,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ContainLower(str string) bool
|
func ContainLower(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -143,10 +145,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ContainUpper">ContainUpper</span>
|
### <span id="ContainUpper">ContainUpper</span>
|
||||||
|
|
||||||
<p>验证字符串是否包含至少一个英文大写字母.</p>
|
<p>验证字符串是否包含至少一个英文大写字母.</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -154,6 +154,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func ContainUpper(str string) bool
|
func ContainUpper(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -174,10 +175,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsAlpha">IsAlpha</span>
|
### <span id="IsAlpha">IsAlpha</span>
|
||||||
|
|
||||||
<p>验证字符串是否只包含英文字母</p>
|
<p>验证字符串是否只包含英文字母</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -185,6 +184,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsAlpha(s string) bool
|
func IsAlpha(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -206,6 +206,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="IsAllUpper">IsAllUpper</span>
|
### <span id="IsAllUpper">IsAllUpper</span>
|
||||||
|
|
||||||
<p>验证字符串是否全是大写英文字母</p>
|
<p>验证字符串是否全是大写英文字母</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -213,6 +214,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsAllUpper(str string) bool
|
func IsAllUpper(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -230,10 +232,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsAllLower">IsAllLower</span>
|
### <span id="IsAllLower">IsAllLower</span>
|
||||||
|
|
||||||
<p>验证字符串是否全是小写英文字母</p>
|
<p>验证字符串是否全是小写英文字母</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -241,6 +241,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsAllLower(str string) bool
|
func IsAllLower(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -258,10 +259,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsBase64">IsBase64</span>
|
### <span id="IsBase64">IsBase64</span>
|
||||||
|
|
||||||
<p>验证字符串是否是base64编码</p>
|
<p>验证字符串是否是base64编码</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -269,6 +268,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsBase64(base64 string) bool
|
func IsBase64(base64 string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -286,10 +286,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsChineseMobile">IsChineseMobile</span>
|
### <span id="IsChineseMobile">IsChineseMobile</span>
|
||||||
|
|
||||||
<p>验证字符串是否是中国手机号码</p>
|
<p>验证字符串是否是中国手机号码</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -297,6 +295,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsChineseMobile(mobileNum string) bool
|
func IsChineseMobile(mobileNum string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -314,9 +313,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsChineseIdNum">IsChineseIdNum</span>
|
### <span id="IsChineseIdNum">IsChineseIdNum</span>
|
||||||
|
|
||||||
<p>验证字符串是否是中国身份证号码</p>
|
<p>验证字符串是否是中国身份证号码</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -324,6 +322,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsChineseIdNum(id string) bool
|
func IsChineseIdNum(id string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -341,10 +340,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsChinesePhone">IsChinesePhone</span>
|
### <span id="IsChinesePhone">IsChinesePhone</span>
|
||||||
|
|
||||||
<p>验证字符串是否是中国电话座机号码</p>
|
<p>验证字符串是否是中国电话座机号码</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -352,6 +349,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsChinesePhone(phone string) bool
|
func IsChinesePhone(phone string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -369,10 +367,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsCreditCard">IsCreditCard</span>
|
### <span id="IsCreditCard">IsCreditCard</span>
|
||||||
|
|
||||||
<p>验证字符串是否是信用卡号码</p>
|
<p>验证字符串是否是信用卡号码</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -380,6 +376,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsCreditCard(creditCart string) bool
|
func IsCreditCard(creditCart string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -397,10 +394,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsDns">IsDns</span>
|
### <span id="IsDns">IsDns</span>
|
||||||
|
|
||||||
<p>验证字符串是否是有效dns</p>
|
<p>验证字符串是否是有效dns</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -408,6 +403,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsDns(dns string) bool
|
func IsDns(dns string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -428,10 +424,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsEmail">IsEmail</span>
|
### <span id="IsEmail">IsEmail</span>
|
||||||
|
|
||||||
<p>验证字符串是否是有效电子邮件地址</p>
|
<p>验证字符串是否是有效电子邮件地址</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -439,6 +433,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsEmail(email string) bool
|
func IsEmail(email string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -456,11 +451,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsEmptyString">IsEmptyString</span>
|
### <span id="IsEmptyString">IsEmptyString</span>
|
||||||
|
|
||||||
<p>验证字符串是否是空字符串</p>
|
<p>验证字符串是否是空字符串</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -468,6 +460,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsEmptyString(s string) bool
|
func IsEmptyString(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -485,10 +478,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsFloatStr">IsFloatStr</span>
|
### <span id="IsFloatStr">IsFloatStr</span>
|
||||||
|
|
||||||
<p>验证字符串是否是可以转换为浮点数</p>
|
<p>验证字符串是否是可以转换为浮点数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -496,6 +487,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsFloatStr(s string) bool
|
func IsFloatStr(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -514,10 +506,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsNumberStr">IsNumberStr</span>
|
### <span id="IsNumberStr">IsNumberStr</span>
|
||||||
|
|
||||||
<p>验证字符串是否是可以转换为数字</p>
|
<p>验证字符串是否是可以转换为数字</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -525,6 +515,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsNumberStr(s string) bool
|
func IsNumberStr(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -543,10 +534,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsJSON">IsJSON</span>
|
### <span id="IsJSON">IsJSON</span>
|
||||||
|
|
||||||
<p>验证字符串是否是有效json</p>
|
<p>验证字符串是否是有效json</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -554,6 +543,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsJSON(str string) bool
|
func IsJSON(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -572,10 +562,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsRegexMatch">IsRegexMatch</span>
|
### <span id="IsRegexMatch">IsRegexMatch</span>
|
||||||
|
|
||||||
<p>验证字符串是否可以匹配正则表达式</p>
|
<p>验证字符串是否可以匹配正则表达式</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -583,6 +571,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsRegexMatch(s, regex string) bool
|
func IsRegexMatch(s, regex string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -598,10 +587,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsIntStr">IsIntStr</span>
|
### <span id="IsIntStr">IsIntStr</span>
|
||||||
|
|
||||||
<p>验证字符串是否是可以转换为整数</p>
|
<p>验证字符串是否是可以转换为整数</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -609,6 +596,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsIntStr(s string) bool
|
func IsIntStr(s string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -625,10 +613,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsIp">IsIp</span>
|
### <span id="IsIp">IsIp</span>
|
||||||
|
|
||||||
<p>验证字符串是否是ip地址</p>
|
<p>验证字符串是否是ip地址</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -636,6 +622,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsIp(ipstr string) bool
|
func IsIp(ipstr string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -652,10 +639,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsIpV4">IsIpV4</span>
|
### <span id="IsIpV4">IsIpV4</span>
|
||||||
|
|
||||||
<p>验证字符串是否是ipv4地址</p>
|
<p>验证字符串是否是ipv4地址</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -663,6 +648,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsIpV4(ipstr string) bool
|
func IsIpV4(ipstr string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -679,10 +665,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsIpV6">IsIpV6</span>
|
### <span id="IsIpV6">IsIpV6</span>
|
||||||
|
|
||||||
<p>验证字符串是否是ipv6地址</p>
|
<p>验证字符串是否是ipv6地址</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -690,6 +674,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsIpV6(ipstr string) bool
|
func IsIpV6(ipstr string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -706,10 +691,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsStrongPassword">IsStrongPassword</span>
|
### <span id="IsStrongPassword">IsStrongPassword</span>
|
||||||
|
|
||||||
<p>验证字符串是否是强密码:(alpha(lower+upper) + number + special chars(!@#$%^&*()?><))</p>
|
<p>验证字符串是否是强密码:(alpha(lower+upper) + number + special chars(!@#$%^&*()?><))</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -717,6 +700,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsStrongPassword(password string, length int) bool
|
func IsStrongPassword(password string, length int) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -734,10 +718,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsUrl">IsUrl</span>
|
### <span id="IsUrl">IsUrl</span>
|
||||||
|
|
||||||
<p>验证字符串是否是url</p>
|
<p>验证字符串是否是url</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -745,6 +727,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsUrl(str string) bool
|
func IsUrl(str string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -761,10 +744,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsWeakPassword">IsWeakPassword</span>
|
### <span id="IsWeakPassword">IsWeakPassword</span>
|
||||||
|
|
||||||
<p>验证字符串是否是弱密码:(only letter or only number or letter + number)
|
<p>验证字符串是否是弱密码:(only letter or only number or letter + number)
|
||||||
.</p>
|
.</p>
|
||||||
|
|
||||||
@@ -773,6 +754,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsWeakPassword(password string, length int) bool
|
func IsWeakPassword(password string, length int) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -789,9 +771,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsZeroValue">IsZeroValue</span>
|
### <span id="IsZeroValue">IsZeroValue</span>
|
||||||
|
|
||||||
<p>判断传入的参数值是否为零值</p>
|
<p>判断传入的参数值是否为零值</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -799,6 +780,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsZeroValue(value any) bool
|
func IsZeroValue(value any) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -819,9 +801,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsGBK">IsGBK</span>
|
### <span id="IsGBK">IsGBK</span>
|
||||||
|
|
||||||
<p>检查数据编码是否为gbk(汉字内部代码扩展规范)。该函数的实现取决于双字节是否在gbk的编码范围内,而utf-8编码格式的每个字节都在gbk编码范围内。因此,应该首先调用utf8.valid检查它是否是utf-8编码,然后调用IsGBK检查gbk编码。如示例所示。</p>
|
<p>检查数据编码是否为gbk(汉字内部代码扩展规范)。该函数的实现取决于双字节是否在gbk的编码范围内,而utf-8编码格式的每个字节都在gbk编码范围内。因此,应该首先调用utf8.valid检查它是否是utf-8编码,然后调用IsGBK检查gbk编码。如示例所示。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
@@ -829,6 +810,7 @@ func main() {
|
|||||||
```go
|
```go
|
||||||
func IsGBK(data []byte) bool
|
func IsGBK(data []byte) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>例子:</b>
|
<b>例子:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -849,5 +831,3 @@ func main() {
|
|||||||
fmt.Println("data encoding is unknown")
|
fmt.Println("data encoding is unknown")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -113,3 +113,15 @@ func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool {
|
|||||||
|
|
||||||
return quit
|
return quit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pipeline takes a list of functions and returns a function whose param will be passed into
|
||||||
|
// the functions one by one.
|
||||||
|
func Pipeline(funcs ...func(interface{}) interface{}) func(interface{}) interface{} {
|
||||||
|
return func(arg interface{}) (result interface{}) {
|
||||||
|
result = arg
|
||||||
|
for _, fn := range funcs {
|
||||||
|
result = fn(result)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -130,3 +130,21 @@ func TestSchedule(t *testing.T) {
|
|||||||
expected := []string{"*", "*", "*", "*", "*"}
|
expected := []string{"*", "*", "*", "*", "*"}
|
||||||
assert.Equal(expected, res)
|
assert.Equal(expected, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPipeline(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestPipeline")
|
||||||
|
|
||||||
|
addOne := func(x interface{}) interface{} {
|
||||||
|
return x.(int) + 1
|
||||||
|
}
|
||||||
|
double := func(x interface{}) interface{} {
|
||||||
|
return 2 * x.(int)
|
||||||
|
}
|
||||||
|
square := func(x interface{}) interface{} {
|
||||||
|
return x.(int) * x.(int)
|
||||||
|
}
|
||||||
|
|
||||||
|
f := Pipeline(addOne, double, square)
|
||||||
|
|
||||||
|
assert.Equal(36, f(2))
|
||||||
|
}
|
||||||
|
|||||||
@@ -19,16 +19,21 @@ const (
|
|||||||
LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
if min == max {
|
if min == max {
|
||||||
return min
|
return min
|
||||||
}
|
}
|
||||||
|
|
||||||
if max < min {
|
if max < min {
|
||||||
min, max = max, min
|
min, max = max, min
|
||||||
}
|
}
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
||||||
return r.Intn(max-min) + min
|
return rand.Intn(max-min) + min
|
||||||
}
|
}
|
||||||
|
|
||||||
// RandBytes generate random byte slice
|
// RandBytes generate random byte slice
|
||||||
@@ -72,9 +77,12 @@ func RandNumeralOrLetter(length int) string {
|
|||||||
// random generate a random string based on given string range
|
// random generate a random string based on given string range
|
||||||
func random(s string, length int) string {
|
func random(s string, length int) string {
|
||||||
b := make([]byte, length)
|
b := make([]byte, length)
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
||||||
|
// fix: https://github.com/duke-git/lancet/issues/75
|
||||||
|
// r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
|
|
||||||
for i := range b {
|
for i := range b {
|
||||||
b[i] = s[r.Int63()%int64(len(s))]
|
b[i] = s[rand.Int63()%int64(len(s))]
|
||||||
}
|
}
|
||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,42 +66,22 @@ func LowerFirst(s string) string {
|
|||||||
return string(r) + s[size:]
|
return string(r) + s[size:]
|
||||||
}
|
}
|
||||||
|
|
||||||
// PadEnd pads string on the right side if it's shorter than size.
|
// PadStart pads string on the left and right side if it's shorter than size.
|
||||||
// Padding characters are truncated if they exceed size.
|
// Padding characters are truncated if they exceed size.
|
||||||
func PadEnd(source string, size int, padStr string) string {
|
func Pad(source string, size int, padStr string) string {
|
||||||
len1 := len(source)
|
return padAtPosition(source, size, padStr, 0)
|
||||||
len2 := len(padStr)
|
|
||||||
|
|
||||||
if len1 >= size {
|
|
||||||
return source
|
|
||||||
}
|
|
||||||
|
|
||||||
fill := ""
|
|
||||||
if len2 >= size-len1 {
|
|
||||||
fill = padStr[0 : size-len1]
|
|
||||||
} else {
|
|
||||||
fill = strings.Repeat(padStr, size-len1)
|
|
||||||
}
|
|
||||||
return source + fill[0:size-len1]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PadStart pads string on the left side if it's shorter than size.
|
// PadStart pads string on the left side if it's shorter than size.
|
||||||
// Padding characters are truncated if they exceed size.
|
// Padding characters are truncated if they exceed size.
|
||||||
func PadStart(source string, size int, padStr string) string {
|
func PadStart(source string, size int, padStr string) string {
|
||||||
len1 := len(source)
|
return padAtPosition(source, size, padStr, 1)
|
||||||
len2 := len(padStr)
|
|
||||||
|
|
||||||
if len1 >= size {
|
|
||||||
return source
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fill := ""
|
// PadEnd pads string on the right side if it's shorter than size.
|
||||||
if len2 >= size-len1 {
|
// Padding characters are truncated if they exceed size.
|
||||||
fill = padStr[0 : size-len1]
|
func PadEnd(source string, size int, padStr string) string {
|
||||||
} else {
|
return padAtPosition(source, size, padStr, 2)
|
||||||
fill = strings.Repeat(padStr, size-len1)
|
|
||||||
}
|
|
||||||
return fill[0:size-len1] + source
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// KebabCase covert string to kebab-case
|
// KebabCase covert string to kebab-case
|
||||||
@@ -273,3 +253,74 @@ func SplitEx(s, sep string, removeEmptyString bool) []string {
|
|||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SplitWords splits a string into words, word only contains alphabetic characters.
|
||||||
|
func SplitWords(s string) []string {
|
||||||
|
var word string
|
||||||
|
var words []string
|
||||||
|
var r rune
|
||||||
|
var size, pos int
|
||||||
|
|
||||||
|
isWord := false
|
||||||
|
|
||||||
|
for len(s) > 0 {
|
||||||
|
r, size = utf8.DecodeRuneInString(s)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case isLetter(r):
|
||||||
|
if !isWord {
|
||||||
|
isWord = true
|
||||||
|
word = s
|
||||||
|
pos = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
case isWord && (r == '\'' || r == '-'):
|
||||||
|
// is word
|
||||||
|
|
||||||
|
default:
|
||||||
|
if isWord {
|
||||||
|
isWord = false
|
||||||
|
words = append(words, word[:pos])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pos += size
|
||||||
|
s = s[size:]
|
||||||
|
}
|
||||||
|
|
||||||
|
if isWord {
|
||||||
|
words = append(words, word[:pos])
|
||||||
|
}
|
||||||
|
|
||||||
|
return words
|
||||||
|
}
|
||||||
|
|
||||||
|
// WordCount return the number of meaningful word, word only contains alphabetic characters.
|
||||||
|
func WordCount(s string) int {
|
||||||
|
var r rune
|
||||||
|
var size, count int
|
||||||
|
|
||||||
|
isWord := false
|
||||||
|
|
||||||
|
for len(s) > 0 {
|
||||||
|
r, size = utf8.DecodeRuneInString(s)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case isLetter(r):
|
||||||
|
if !isWord {
|
||||||
|
isWord = true
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
|
||||||
|
case isWord && (r == '\'' || r == '-'):
|
||||||
|
// is word
|
||||||
|
|
||||||
|
default:
|
||||||
|
isWord = false
|
||||||
|
}
|
||||||
|
|
||||||
|
s = s[size:]
|
||||||
|
}
|
||||||
|
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
|||||||
@@ -96,3 +96,72 @@ func toUpperAll(rs []rune) []rune {
|
|||||||
}
|
}
|
||||||
return rs
|
return rs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func padAtPosition(str string, length int, padStr string, position int) string {
|
||||||
|
if len(str) >= length {
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
if padStr == "" {
|
||||||
|
padStr = " "
|
||||||
|
}
|
||||||
|
|
||||||
|
length = length - len(str)
|
||||||
|
startPadLen := 0
|
||||||
|
if position == 0 {
|
||||||
|
startPadLen = length / 2
|
||||||
|
} else if position == 1 {
|
||||||
|
startPadLen = length
|
||||||
|
}
|
||||||
|
endPadLen := length - startPadLen
|
||||||
|
|
||||||
|
charLen := len(padStr)
|
||||||
|
leftPad := ""
|
||||||
|
cur := 0
|
||||||
|
for cur < startPadLen {
|
||||||
|
leftPad += string(padStr[cur%charLen])
|
||||||
|
cur++
|
||||||
|
}
|
||||||
|
|
||||||
|
cur = 0
|
||||||
|
rightPad := ""
|
||||||
|
for cur < endPadLen {
|
||||||
|
rightPad += string(padStr[cur%charLen])
|
||||||
|
cur++
|
||||||
|
}
|
||||||
|
|
||||||
|
return leftPad + str + rightPad
|
||||||
|
}
|
||||||
|
|
||||||
|
// isLetter checks r is a letter but not CJK character.
|
||||||
|
func isLetter(r rune) bool {
|
||||||
|
if !unicode.IsLetter(r) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
// cjk char: /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/
|
||||||
|
|
||||||
|
// hiragana and katakana (Japanese only)
|
||||||
|
case r >= '\u3034' && r < '\u30ff':
|
||||||
|
return false
|
||||||
|
|
||||||
|
// CJK unified ideographs extension A (Chinese, Japanese, and Korean)
|
||||||
|
case r >= '\u3400' && r < '\u4dbf':
|
||||||
|
return false
|
||||||
|
|
||||||
|
// CJK unified ideographs (Chinese, Japanese, and Korean)
|
||||||
|
case r >= '\u4e00' && r < '\u9fff':
|
||||||
|
return false
|
||||||
|
|
||||||
|
// CJK compatibility ideographs (Chinese, Japanese, and Korean)
|
||||||
|
case r >= '\uf900' && r < '\ufaff':
|
||||||
|
return false
|
||||||
|
|
||||||
|
// half-width katakana (Japanese only)
|
||||||
|
case r >= '\uff66' && r < '\uff9f':
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|||||||
@@ -168,9 +168,19 @@ func TestLowerFirst(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPad(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestPad")
|
||||||
|
|
||||||
|
assert.Equal("a ", Pad("a", 2, ""))
|
||||||
|
assert.Equal("a", Pad("a", 1, "b"))
|
||||||
|
assert.Equal("ab", Pad("a", 2, "b"))
|
||||||
|
assert.Equal("mabcdm", Pad("abcd", 6, "m"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestPadEnd(t *testing.T) {
|
func TestPadEnd(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestPadEnd")
|
assert := internal.NewAssert(t, "TestPadEnd")
|
||||||
|
|
||||||
|
assert.Equal("a ", PadEnd("a", 2, " "))
|
||||||
assert.Equal("a", PadEnd("a", 1, "b"))
|
assert.Equal("a", PadEnd("a", 1, "b"))
|
||||||
assert.Equal("ab", PadEnd("a", 2, "b"))
|
assert.Equal("ab", PadEnd("a", 2, "b"))
|
||||||
assert.Equal("abcdmn", PadEnd("abcd", 6, "mno"))
|
assert.Equal("abcdmn", PadEnd("abcd", 6, "mno"))
|
||||||
@@ -285,3 +295,37 @@ func TestSplitEx(t *testing.T) {
|
|||||||
assert.Equal([]string{" a", "b", "c", ""}, SplitEx(" a = b = c = ", " = ", false))
|
assert.Equal([]string{" a", "b", "c", ""}, SplitEx(" a = b = c = ", " = ", false))
|
||||||
assert.Equal([]string{" a", "b", "c"}, SplitEx(" a = b = c = ", " = ", true))
|
assert.Equal([]string{" a", "b", "c"}, SplitEx(" a = b = c = ", " = ", true))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSplitWords(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSplitWords")
|
||||||
|
|
||||||
|
cases := map[string][]string{
|
||||||
|
"a word": {"a", "word"},
|
||||||
|
"I'am a programmer": {"I'am", "a", "programmer"},
|
||||||
|
"Bonjour, je suis programmeur": {"Bonjour", "je", "suis", "programmeur"},
|
||||||
|
"a -b-c' 'd'e": {"a", "b-c'", "d'e"},
|
||||||
|
"你好,我是一名码农": nil,
|
||||||
|
"こんにちは,私はプログラマーです": nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range cases {
|
||||||
|
assert.Equal(v, SplitWords(k))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWordCount(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestSplitWords")
|
||||||
|
|
||||||
|
cases := map[string]int{
|
||||||
|
"a word": 2, // {"a", "word"},
|
||||||
|
"I'am a programmer": 3, // {"I'am", "a", "programmer"},
|
||||||
|
"Bonjour, je suis programmeur": 4, // {"Bonjour", "je", "suis", "programmeur"},
|
||||||
|
"a -b-c' 'd'e": 3, // {"a", "b-c'", "d'e"},
|
||||||
|
"你好,我是一名码农": 0, // nil,
|
||||||
|
"こんにちは,私はプログラマーです": 0, // nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range cases {
|
||||||
|
assert.Equal(v, WordCount(k))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user