<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          支持300+常用功能的開源GO語言工具函數(shù)庫

          共 2983字,需瀏覽 6分鐘

           ·

          2022-05-15 14:11

          本期推薦的是一個Go語言工具函數(shù)庫——Lancet。



          lancet(柳葉刀)是一個全面、高效、可復(fù)用的go語言工具函數(shù)庫。lancet受到了java apache common包和lodash.js的啟發(fā)。


          特性

          • 全面、高效、可復(fù)用
          • 300+常用go工具函數(shù),支持string、slice、datetime、net、crypt...
          • 只依賴go標(biāo)準(zhǔn)庫
          • 所有導(dǎo)出函數(shù)單元測試覆蓋率100%


          安裝

          1、對于使用go1.18及以上的用戶,建議安裝v2.x.x。因?yàn)関2.x.x用go1.18的泛型重寫了大部分函數(shù)。

          go get github.com/duke-git/lancet/v2 //安裝v2最新版本v2.x.x

          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是以包的結(jié)構(gòu)組織代碼的,使用時需要導(dǎo)入相應(yīng)的包名。例如:如果使用字符串相關(guān)函數(shù),需要導(dǎo)入strutil包:

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

          此處以字符串工具函數(shù)ReverseStr(逆序字符串)為例,需要導(dǎo)入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算法包實(shí)現(xiàn)一些基本算法。例如:sort、search、lrucache等。

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

          示例:BubbleSort

          冒泡排序,參數(shù)comparator需要實(shí)現(xiàn)包
          lancetconstraints.Comparator

          函數(shù)簽名:

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


          并發(fā)包

          并發(fā)包包含一些支持并發(fā)編程的功能。例如:goroutine、channel、async等。

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

          示例:NewChannel

          返回一個 Channel 指針實(shí)例

          函數(shù)簽名:

          type Channel struct {}
          func NewChannel() *Channel

          Example:

          package main

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

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


          轉(zhuǎn)換器包

          convertor轉(zhuǎn)換器包支持一些常見的數(shù)據(jù)類型轉(zhuǎn)換。

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

          示例:ToBool

          字符串轉(zhuǎn)布爾類型,使用strconv.ParseBool

          函數(shù)簽名:

          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加密包支持?jǐn)?shù)據(jù)加密和解密,獲取md5,hash值。支持base64、md5、hmac、aes、des、rsa。

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

          示例:AesEcbEncrypt

          使用AES ECB算法模式加密數(shù)據(jù). 參數(shù)`key`的長度是16, 24 or 32。

          函數(shù)簽名:

          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—

          開源協(xié)議:MIT
          開源地址:
          https://github.com/duke-git/lancet


          想要了解更多相關(guān)的內(nèi)容,歡迎掃描下方?? 關(guān)注 公眾號,回復(fù)關(guān)鍵詞 [實(shí)戰(zhàn)群]  ,就有機(jī)會進(jìn)群和我們進(jìn)行交流~


          分享、在看與點(diǎn)贊,至少我要擁有一個叭~

          瀏覽 56
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日本亚洲天堂 | 淫色淫色网站 | 成人伊人网在线观看 | 天天摸天天干天天日 | YY6080伦理韩国日本 |