3

lancet(go语言工具函数库) 发布v1.1.7版本

 2 years ago
source link: https://studygolang.com/articles/35395
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

Go version

v1.1.7特性

  • 👏 新增40+常用go工具函数
  • 💪 部分函数性能优化
go get github.com/duke-git/lancet

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

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

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

package main

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

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

v1.1.7版本新增功能函数:

1. fileutil文件处理包

  • 文件处理常用函数
  • 导入包:import "github.com/duke-git/lancet/fileutil"
package main

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

func main() {
    fmt.Println(fileutil.IsDir("./")) // true
}
  • 函数列表:
func ClearFile(path string) error //清空文件内容
func ReadFileToString(path string) (string, error) //读取文件内容为字符串
func ReadFileByLine(path string)([]string, error) //按行读取文件内容

2. function包可以控制函数执行,支持部分函数式编程

  • 控制函数执行,支持部分函数式编程
  • 导入包:import "github.com/duke-git/lancet/function"
package main

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

func main() {
    var print = func(s string) {
        fmt.Println(s)
    }
    function.Delay(2*time.Second, print, "hello world")
}
  • Function list:
func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value  //创建一个函数, 只有在运行了n次之后才有效果
func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value  //创建一个函数,调用不超过n次。 当n已经达到时,最后一个函数调用的结果将被记住并返回
func (f Fn) Curry(i interface{}) func(...interface{}) interface{}  //函数柯里化
func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{}  //从右至左组合函数
func Delay(delay time.Duration, fn interface{}, args ...interface{})  //延迟调用函数
func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool //每隔duration时间调用函数, 关闭返回通道可以停止调用
func (w *Watcher) Start() //开时watcher
func (w *Watcher) Stop() //开时watcher
func (w *Watcher) Reset() {} //重置代码watcher
func (w *Watcher) GetElapsedTime() time.Duration //get code excution elapsed time.

3. netutil网络处理包

  • 处理ip, http请求相关函数
  • 导入包:import "github.com/duke-git/lancet/netutil"
  • http方法params参数顺序:header, query string, body, httpclient
package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "github.com/duke-git/lancet/netutil"
)

func main() {
    url := "https://gutendex.com/books?"
    header := make(map[string]string)
    header["Content-Type"] = "application/json"
    queryParams := make(map[string]interface{})
    queryParams["ids"] = "1"

    resp, err := netutil.HttpGet(url, header, queryParams)
    if err != nil {
       log.Fatal(err)
    }

    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println("response: ", resp.StatusCode, string(body))
}
  • 函数列表:
func ParseHttpResponse(resp *http.Response, obj interface{}) error //将http响应解码成特定interface

4. slice切片操作包

  • 切片操作相关函数
  • 导入包:import "github.com/duke-git/lancet/slice"
  • 由于go目前对范型支持不稳定,slice处理函数参数和返回值大部分为interface{}, 待范型特性稳定后,会重构相关函数
package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "github.com/duke-git/lancet/slice"
)

func main() {
    nums := []int{1, 4, 3, 4, 6, 7, 3}
    uniqueNums, _ := slice.IntSlice(slice.Unique(nums))
    fmt.Println(uniqueNums) //[1 4 3 6 7]
}
  • 函数列表:
func Drop(slice interface{}, n int) interface{} //创建一个新切片,当n大于0时删除原切片前n个元素,当n小于0时删除原切片后n个元素
func Every(slice, function interface{}) bool //slice中所有元素都符合函数条件时返回true, 否则返回false. 函数签名:func(index int, value interface{}) bool
func Find(slice, function interface{}) (interface{}, bool)//查找slice中第一个符合条件的元素,函数签名:func(index int, value interface{}) bool
func FlattenDeep(slice interface{}) interface{} //将slice递归为一维切片。
func Intersection(slices ...interface{}) interface{} //slice交集,去重
func Reduce(slice, function, zero interface{}) interface{} //切片reduce操作, 函数签名:func(index int, value1, value2 interface{}) interface{}
func Shuffle(slice interface{}) interface{} //创建一个被打乱值的切片
func Some(slice, function interface{}) bool //slice中任意一个元素都符合函数条件时返回true, 否则返回false. 函数签名:func(index int, value interface{}) bool
func Union(slices ...interface{}) interface{} //slice并集, 去重
func Without(slice interface{}, values ...interface{}) interface{} //slice去除values
func GroupBy(slice, function interface{}) (interface{}, interface{}) //根据函数function的逻辑分slice为两组slice

5. strutil字符串处理包

  • 字符串操作相关函数
  • 导入包:import "github.com/duke-git/lancet/strutil"
package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "github.com/duke-git/lancet/strutil"
)

func main() {
    str := "Foo-Bar"
    camelCaseStr := strutil.CamelCase(str)
    fmt.Println(camelCaseStr) //fooBar
}
  • 函数列表:
func Wrap(str string, wrapWith string) string //包裹字符串 Wrap("abc", "*") -> *abc*.
func Unwrap(str string, wrapToken string) string //解包裹字符串 Wrap("*abc*", "*") -> abc.

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK