1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00
2023-01-03 11:55:23 +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 19:09:18 +08:00
2022-03-17 10:57:35 +08:00
2022-12-29 19:55:40 +08:00
2022-12-30 14:28:49 +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-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:

  • NewChannel : create a Channel pointer instance. [doc]
  • Bridge : link multiply channels into one channel. [doc]
  • FanIn : merge multiple channels into one channel. [doc]
  • Generate : creates a channel, then put values into the channel. [doc]
  • Or : read one or more channels into one channel, will close when any readin channel is closed. [doc]
  • OrDone : read a channel into another channel, will close until cancel context. [doc]
  • Repeat : create channel, put values into the channel repeatly until cancel the context. [doc]
  • RepeatFn : create a channel, excutes fn repeatly, and put the result into the channel, until close context. [doc]
  • Take : create a channel whose values are taken from another channel with limit number. [doc]
  • Tee : split one chanel into two channels, until cancel the context. [doc]

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

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

Function list:

  • Bool : returns the truthy value of anything. [doc] [play]
  • And : returns true if both a and b are truthy. [doc] [play]
  • Or : returns false if neither a nor b is truthy. [doc] [play]
  • Xor : returns true if a or b but not both is truthy. [doc] [play]
  • Nor : returns true if neither a nor b is truthy. [doc] [play
  • Xnor : returns true if both a and b or neither a nor b are truthy. [doc] [play]
  • Nand : returns false if both a and b are truthy. [doc] [play]
  • TernaryOperator : ternary operator. [doc] [play]

4. Convertor package contains some functions for data convertion.

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

Function list:

  • ColorHexToRGB : convert color hex to color rgb. [doc] [play]
  • ColorRGBToHex : convert rgb color to hex color. [doc] [play]
  • ToBool : convert string to bool. [doc] [play]
  • ToBytes : convert value to byte slice. [doc] [play]
  • ToChar : convert string to char slice. [doc] [play]
  • ToChannel : convert a collection of elements to a read-only channel. [doc] [play]
  • ToFloat : convert value to float64, if param is a invalid floatable, will return 0.0 and error. [doc] [play]
  • ToInt : convert value to int64 value, if input is not numerical, return 0 and error. [doc] [play]
  • ToJson : convert value to a json string. [doc] [play]
  • ToMap : convert a slice of structs to a map based on iteratee function. [doc] [play]
  • ToPointer : return a pointer of passed value. [doc] [play]
  • ToString : convert value to string. [doc] [play]
  • StructToMap : convert struct to map, only convert exported struct field. [doc] [play]
  • MapToSlice : convert map to slice based on iteratee function. [doc] [play]
  • EncodeByte : encode data to byte slice. [doc] [play]
  • DecodeByte : decode byte slice data to target object. [doc] [play]

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:

  • Comma : add comma to a number value by every 3 numbers from right, ahead by symbol char. [doc] [play]

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:

  • ForEach : executes iteratee funcation for every key and value pair in map. [doc] [play]
  • Filter : iterates over map, return a new map contains all key and value pairs pass the predicate function. [doc] [play]
  • Intersect : iterates over maps, return a new map of key and value pairs in all given maps. [doc] [play]
  • Keys : returns a slice of the map's keys. [doc] [play]
  • Merge : merge maps, next key will overwrite previous key. [doc] [play]
  • Minus : creates a map of whose key in mapA but not in mapB. [doc] [play]
  • Values : returns a slice of the map's values. [doc] [play]
  • IsDisjoint : check two map are disjoint if they have no keys in common. [doc] [play]

12. Mathutil package implements some functions for math calculation.

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

Function list:

  • Average :return average value of numbers. [doc] [play]
  • Exponent : calculate x^n for int64. [doc] [play]
  • Fibonacci :calculate fibonacci number before n for int. [doc] [play]
  • Factorial : calculate x! for uint. [doc] [play]
  • Max : return maximum value of numbers. [doc] [play]
  • MaxBy : return the maximum value of a slice using the given comparator function. [doc] [play]
  • Min : return minimum value of numbers. [doc] [play]
  • MinBy : return the minimum value of a slice using the given comparator function. [doc] [play]
  • Percent : calculate the percentage of value to total. [doc]
  • RoundToFloat : round up to n decimal places for float64. [doc] [play]
  • RoundToString : round up to n decimal places for float64, return string. [doc] [play]
  • TruncRound : round off n decimal places for int64. [doc] [play]

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:

  • IsWindows : check if current os is windows. [doc] [play]
  • IsLinux : check if current os is linux. [doc] [play]
  • IsMac : check if current os is macos. [doc] [play]
  • GetOsEnv : get the value of the environment variable named by the key. [doc] [play]
  • SetOsEnv : set the value of the environment variable named by the key. [doc] [play]
  • RemoveOsEnv : remove a single environment variable. [doc] [play]
  • CompareOsEnv : get env named by the key and compare it with passed env. [doc] [play]
  • ExecCommand : execute command, return the stdout and stderr string of command, and error if error occurs. [doc] [play]
  • GetOsBits : return current os bits (32 or 64). [doc] [play]

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%