1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00
2022-12-29 20:03:59 +08:00
2022-12-10 16:41:40 +08:00
2022-12-10 16:41:40 +08:00
2022-12-10 16:53:41 +08:00
2022-12-29 20:03:59 +08:00
2022-12-10 16:53:41 +08:00
2022-12-10 16:41:40 +08:00
2022-03-16 18:41:40 +08:00
2022-12-26 17:20:14 +08:00
2022-12-10 16:41:40 +08:00
2022-12-10 19:09:18 +08:00
2022-03-17 10:57:35 +08:00
2022-12-29 19:55:40 +08:00
2022-12-09 19:28:45 +08:00
2022-03-17 10:57:35 +08:00
2022-12-10 20:59:27 +08:00
2022-12-10 20:59:27 +08:00
2021-11-28 21:18:19 +08:00
2022-07-08 10:52:53 +08:00
2022-12-29 20:03:59 +08:00
2022-12-29 20:03:59 +08:00
2022-11-16 11:31:50 +08:00


Go version Release GoDoc Go Report Card test codecov License

Lancet is a comprehensive, efficient, and reusable util function library of go. Inspired by the java apache common package and lodash.js.

English | 简体中文

Feature

  • 👏 Comprehensive, efficient and reusable.
  • 💪 300+ go util functions, support string, slice, datetime, net, crypt...
  • 💅 Only depend on the go standard library.
  • 🌍 Unit test for every exported function.

Installation

Note:

  1. For users who use go1.18 and above, it is recommended to install lancet v2.x.x. Cause in v2.x.x all functions was rewriten with generics of go1.18.
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
  1. For users who use version below go1.18, you should install v1.x.x. The latest of v1.x.x is v1.3.5.
go get github.com/duke-git/lancet@v1.3.5 // below go1.18, install latest version of v1.x.x

Usage

Lancet organizes the code into package structure, and you need to import the corresponding package name when use it. For example, if you use string-related functions,import the strutil package like below:

import "github.com/duke-git/lancet/v2/strutil"

Example

Here takes the string function Reverse (reverse order string) as an example, and the strutil package needs to be imported.

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/strutil"
)

func main() {
    s := "hello"
    rs := strutil.Reverse(s)
    fmt.Println(rs) //olleh
}

Documentation

import "github.com/duke-git/lancet/v2/algorithm"

Function list:

  • BubbleSort : sorts slice with bubble sort algorithm, will change the original slice. [doc] [play]
  • CountSort : sorts slice with bubble sort algorithm, don't change original slice. [doc] [play]
  • HeapSort : sorts slice with heap sort algorithm, will change the original slice. [doc] [play]
  • InsertionSort : sorts slice with insertion sort algorithm, will change the original slice. [doc] [play]
  • MergeSort : sorts slice with merge sort algorithm, will change the original slice. [doc] [play]
  • QuickSort : sorts slice with quick sort algorithm, will change the original slice. [doc] [play]
  • SelectionSort : sorts slice with selection sort algorithm, will change the original slice. [doc] [play]
  • ShellSort : sorts slice with shell sort algorithm, will change the original slice. [doc] [play]
  • BinarySearch : returns the index of target within a sorted slice, use binary search (recursive call itself). [doc] [play]
  • BinaryIterativeSearch : returns the index of target within a sorted slice, use binary search (no recursive). [doc] [play]
  • LinearSearch : returns the index of target in slice base on equal function. [doc]
  • LRUCache : implements memory cache with lru algorithm. [doc] [play]

2. Concurrency package contain some functions to support concurrent programming. eg, goroutine, channel, async.

import "github.com/duke-git/lancet/v2/concurrency"

Function list:

3. Condition package contains some functions for conditional judgment. eg. And, Or, TernaryOperator...

import "github.com/duke-git/lancet/v2/condition"

Function list:

4. Convertor package contains some functions for data convertion.

import "github.com/duke-git/lancet/v2/convertor"

Function list:

5. Cryptor package is for data encryption and decryption.

import "github.com/duke-git/lancet/v2/cryptor"

Function list:

6. Datetime package supports date and time format and compare.

import "github.com/duke-git/lancet/v2/datetime"

Function list:

import list "github.com/duke-git/lancet/v2/datastructure/list"
import link "github.com/duke-git/lancet/v2/datastructure/link"
import stack "github.com/duke-git/lancet/v2/datastructure/stack"
import queue "github.com/duke-git/lancet/v2/datastructure/queue"
import set "github.com/duke-git/lancet/v2/datastructure/set"
import tree "github.com/duke-git/lancet/v2/datastructure/tree"
import heap "github.com/duke-git/lancet/v2/datastructure/heap"
import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"

Function list:

8. Fileutil package implements some basic functions for file operations.

import "github.com/duke-git/lancet/v2/fileutil"

Function list

9. Formatter contains some functions for data formatting.

import "github.com/duke-git/lancet/v2/formatter"

Function list:

10. Function package can control the flow of function execution and support part of functional programming

import "github.com/duke-git/lancet/v2/function"

Function list:

11. Maputil package includes some functions to manipulate map.

import "github.com/duke-git/lancet/v2/maputil"

Function list:

12. Mathutil package implements some functions for math calculation.

import "github.com/duke-git/lancet/v2/mathutil"

Function list:

13. Netutil package contains functions to get net information and send http request.

import "github.com/duke-git/lancet/v2/netutil"

Function list:

14. Random package implements some basic functions to generate random int and string.

import "github.com/duke-git/lancet/v2/random"

Function list:

15. Retry package is for executing a function repeatedly until it was successful or canceled by the context.

import "github.com/duke-git/lancet/v2/retry"

Function list:

16. Slice contains some functions to manipulate slice.

import "github.com/duke-git/lancet/v2/slice"

Function list:

17. Strutil package contains some functions to manipulate string.

import "github.com/duke-git/lancet/v2/strutil"

Function list:

  • After : returns the substring after the first occurrence of a specified string in the source string. [doc] [play]
  • AfterLast : returns the substring after the last occurrence of a specified string in the source string. [doc] [play]
  • Before : returns the substring before the first occurrence of a specified string in the source string. [doc] [play]
  • BeforeLast : returns the substring before the last occurrence of a specified string in the source string. [doc] [play]
  • CamelCase : coverts source string to its camelCase string. [doc] [play]
  • Capitalize : converts the first character of source string to upper case and the remaining to lower case. [doc] [play]
  • IsString : checks if the parameter value data type is string or not. [doc] [play]
  • KebabCase : coverts string to kebab-case string. [doc] [play]
  • UpperKebabCase : coverts string to upper KEBAB-CASE string. [doc] [play]
  • LowerFirst : converts the first character of string to lower case. [doc] [play]
  • UpperFirst : converts the first character of string to upper case. [doc] [play]
  • PadEnd : pads string with given characters on the right side if it's shorter than limit size. Padding characters are truncated if they exceed size. [doc] [play]
  • PadStart : pads string with given characters on the left side if it's shorter than limit size. Padding characters are truncated if they exceed size. [doc] [play]
  • Reverse : returns string whose char order is reversed to the given string. [doc] [play]
  • SnakeCase : coverts string to snake_case string. [doc] [play]
  • UpperSnakeCase : coverts string to upper SNAKE_CASE string. [doc] [play]
  • SplitEx : split a given string which can control the result slice contains empty string or not. [doc] [play]
  • Substring : returns a substring of the specified length starting at the specified offset position. [doc]
  • Wrap : wrap a string with given string. [doc] [play]
  • Unwrap : unwrap a given string from anther string. will change source string. [doc] [play]

19. System package contain some functions about os, runtime, shell command.

import "github.com/duke-git/lancet/v2/system"

Function list:

19. Validator package contains some functions for data validation.

import "github.com/duke-git/lancet/v2/validator"

Function list:

20. xerror package implements helpers for errors.

import "github.com/duke-git/lancet/v2/xerror"

Function list:

How to Contribute

I really appreciate any code commits which make lancet lib powerful. Please follow the rules below to create your pull request.

  1. Fork the repository.
  2. Create your feature branch.
  3. Commit your changes.
  4. Push to the branch
  5. Create new pull request.
Description
A comprehensive, efficient, and reusable util function library of go.
Readme MIT 11 MiB
Languages
Go 100%