支持300+常用功能的开源GO语言工具函数库

《开源精选》是我们分享 Github、Gitee 等开源社区中优质项目的栏目,包括技术、学习、实用与各种有趣的内容。本期推荐的是一个 Go 语言工具函数库——Lancet。

支持300+常用功能的开源GO语言工具函数库

lancet(柳叶刀)是一个全面、高效、可复用的go语言工具函数库。lancet 受到了 java apache common 包和 lodash.js 的启发。

特性

  • • 全面、高效、可复用

  • • 300+常用go工具函数,支持 string、slice、datetime、net、crypt…

  • • 只依赖 go 标准库

  • • 所有导出函数单元测试覆盖率100%

安装

  1. 1. 对于使用go1.18及以上的用户,建议安装 v2.x.x。因为 v2.x.x用 go1.18的泛型重写了大部分函数。

go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
  1. 2. 使用go1.18以下版本的用户,必须安装 v1.x.x。目前最新的 v1版本是 v1.2.9。

go get github.com/duke-git/lancet@v1.2.9 // 使用go1.18以下版本, 必须安装v1.x.x版本

用法

lancet 是以包的结构组织代码的,使用时需要导入相应的包名。例如:如果使用字符串相关函数,需要导入 strutil 包:

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

此处以字符串工具函数 ReverseStr(逆序字符串)为例,需要导入 strutil 包:

package main

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

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

算法包

algorithm 算法包实现一些基本算法。例如:sort、search、lrucache 等。

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

示例:BubbleSort

冒泡排序,参数 comparator 需要实现包 lancetconstraints.Comparator

函数签名:

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}
}

并发

并发包包含一些支持并发编程的功能。例如:goroutine、channel、async 等。

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

示例:NewChannel

返回一个 Channel 指针实例

函数签名:

type Channel struct {}
func NewChannel() *Channel

Example:

package main

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

func main() {
    c := concurrency.NewChannel()
}

转换器包

convertor 转换器包支持一些常见的数据类型转换。

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

示例:ToBool

字符串转布尔类型,使用 strconv.ParseBool

函数签名:

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
}

加密包

cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64、md5、hmac、aes、des、rsa。

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

示例:AesEcbEncrypt

使用 AES ECB 算法模式加密数据. 参数key的长度是16, 24 or 32。

函数签名:

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))
}


传送门

开源协议:MIT

开源地址:https://github.com/duke-git/lancet

-END-


原文始发于微信公众号(开源技术专栏):支持300+常用功能的开源GO语言工具函数库

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/120623.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!