This issue recommends a Go language utility library – Lancet.
Lancet is a comprehensive, efficient, and reusable Go language tool library. Lancet was inspired by the Java Apache Common package and lodash. js.
characteristic
- Comprehensive, efficient, and reusable
- 300+commonly used go tool functions, supporting string, slice, timestamp, net, crypt
- Only relying on the Go standard library
- All exported function unit tests have a coverage rate of 100%
install
1. For users using go1.18 and above, it is recommended to install v2. x.x. Because v2. x.x rewrote most of the functions with go1.18 generics.
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
2. Users using versions below go1.18 must install v1. x.x. The latest v1 version currently available is v1.2.9.
go get github.com/duke-git/lancet@v1.2.9 // 使用go1.18以下版本, 必须安装v1.x.x版本
usage
Lancet organizes code in a package structure and requires importing the corresponding package name when used. For example, if using string related functions, you need to import the strutil package:
import "github.com/duke-git/lancet/v2/strutil"
Taking the string utility function ReverseStr as an example, we need to import the strutil package:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
s := "hello"
rs := strutil.ReverseStr(s)
fmt.Println(rs) //olleh
}
Algorithm package
The algorithm package implements some basic algorithms. For example: sort, search, lrucache, etc.
import ( "github.com/duke-git/lancet/v2/algorithm")
Example: BubbleSort
Bubble sort, the parameter comparator needs to be implemented in the package
lancetconstraints.Comparator
Function Signature:
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
Example:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/algorithm"
)
func main() {
type intComparator struct{}
func (c *intComparator) Compare(v1 any, v2 any) int {
val1, _ := v1.(int)
val2, _ := v2.(int)
//ascending order
if val1 < val2 {
return -1
} else if val1 > val2 {
return 1
}
return 0
}
intSlice := []int{2, 1, 5, 3, 6, 4}
comparator := &intComparator{}
algorithm.BubbleSort(intSlice, comparator)
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
}
And issue the contract
The concurrent package contains some features that support concurrent programming. For example: goroutines, channels, asyncs, etc.
import "github.com/duke-git/lancet/v2/concurrency"
Example: NewChannel
Return a Channel pointer instance
Function Signature:
type Channel struct {}
func NewChannel() *Channel
Example:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/concurrency"
)
func main() {
c := concurrency.NewChannel()
}
Converter package
The converter package supports some common data type conversions.
import "github.com/duke-git/lancet/v2/convertor"
Example: ToBool
Convert string to boolean type using strconv ParseBool
Function Signature:
func ToBool(s string) (bool, error)
Example:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/convertor"
)
func main() {
v1, _ := convertor.ToBool("1")
fmt.Println(v1) //true
v2, _ := convertor.ToBool("true")
fmt.Println(v2) //true
v3, _ := convertor.ToBool("True")
fmt.Println(v3) //true
v4, _ := convertor.ToBool("123")
fmt.Println(v4) //false
}
Encrypted package
The crypto encryption package supports data encryption and decryption, obtaining md5 and hash values. Supports base64, md5, hmac, AES, DES, RSA.
import "github.com/duke-git/lancet/v2/cryptor"
Example: AesEcbEncrypt
Encrypt data using AES ECB algorithm mode The length of parameter ‘key’ is 16, 24, or 32.
Function Signature:
func AesEcbEncrypt(data, key []byte) []byte
Example:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/cryptor"
)
func main() {
data := "hello world"
key := "abcdefghijklmnop"
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
fmt.Println(string(encrypted))
}
—END—
Open source license: MIT