Golang中好用的三方工具包lancet使用

项目地址

lancet源码项目地址

自己练习的项目:a_lancet_tests

slice.Map:结构体切片转换&返回结构体切片中某个属性组成的新切片

package a_lancet_tests

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

// slice.Map: Notice 结构体切片之间的转换
type teacher struct {
    Name string
    Age  int
}
type teacherRPC struct {
    Name string
    Age  int
}

func TestSliceChange(t *testing.T) {

    tr1 := teacherRPC{Name: "whw", Age: 22}
    tr2 := teacherRPC{Name: "naruto", Age: 23}
    trSlice := []*teacherRPC{&tr1, &tr2}

    // Notice 转化成 *teacher 组成的切片!
    tSlice := slice.Map(trSlice, func(index int, item *teacherRPC) *teacher {
        return &teacher{
            Name: item.Name,
            Age:  item.Age,
        }
    })

    fmt.Printf(">> %T, %v \n", tSlice, tSlice) // []*a_lancet_tests.teacher, [0x1400000c108 0x1400000c120]
}

// slice.Map: 返回结构体切片中某个属性组成的切片
func TestTT123(t *testing.T) {
    type student struct {
        sid  int
        name string
    }

    lst := make([]*student, 0)
    for i := 0; i < 5; i++ {
        currStu := student{
            sid:  i,
            name: fmt.Sprintf("whw-%v", i),
        }
        lst = append(lst, &currStu)
    }

    ret := slice.Map(lst, func(index int, item *student) string {
        return item.name
    })

    fmt.Println("ret: ", ret) // [whw-0 whw-1 whw-2 whw-3 whw-4]

} 

slice.ForEach:遍历&也可以转结构体切片

// 使用slice.ForEach进行结构体切片的转换
func TestForEach(t *testing.T) {
    tr1 := teacherRPC{Name: "whw", Age: 22}
    tr2 := teacherRPC{Name: "naruto", Age: 23}
    trSlice := []*teacherRPC{&tr1, &tr2}

    tSLice := make([]*teacher, 0)

    slice.ForEach(trSlice, func(index int, item *teacherRPC) {
        tSLice = append(tSLice, &teacher{
            Name: item.Name,
            Age:  item.Age,
        })
    })

    fmt.Printf("tSlice: %T, %v", tSLice, tSLice) // []*a_lancet_tests.teacher, [0x140000ac0c0 0x140000ac0d8]

slice.GroupWith:把结构体切片转为map并且可以指定map的key

// slice.GroupWith 把切片转为map,并且可以指定map的key~
func TestGroupWith(t *testing.T) {
    tr1 := teacherRPC{Name: "whw", Age: 22}
    tr2 := teacherRPC{Name: "naruto", Age: 23}
    trSlice := []teacherRPC{tr1, tr2}

    group := slice.GroupWith(trSlice, func(item teacherRPC) string {
        return item.Name
    })

    fmt.Println("group: ", group) // map[naruto:[{naruto 23}] whw:[{whw 22}]]
}

slice.KeyBy:将结构体切片转为map并且key可以是结构体中的某个字段

// slice.KeyBy
func TestKeyBy(t *testing.T) {

    type stu struct {
        Name string
        Age  int
    }

    s1 := stu{"whw1", 22}
    s2 := stu{"whw2", 23}
    s3 := stu{"whw3", 24}

    lst := []stu{s1, s2, s3}

    retMap := slice.KeyBy(lst, func(item stu) string {
        return item.Name
    })

    fmt.Println("retMap: ", retMap)
    // retMap:  map[whw1:{whw1 22} whw2:{whw2 23} whw3:{whw3 24}]
}

slice.GroupBy:结构体切片根据某个属性过滤

// slice.GroupBy
func TestGroupBy(t *testing.T) {
    tr1 := teacherRPC{Name: "whw", Age: 22}
    tr2 := teacherRPC{Name: "naruto", Age: 23}
    tr3 := teacherRPC{Name: "sasuke", Age: 24}
    trSlice := []teacherRPC{tr1, tr2, tr3}

    s1, s2 := slice.GroupBy(trSlice, func(index int, item teacherRPC) bool {
        return item.Age > 22
    })

    fmt.Println("s1: ", s1) // [{naruto 23} {sasuke 24}]
    fmt.Println("s2: ", s2) // [{whw 22}]

} 

slice.Intersection:获取两个切片中元素的交集并返回到新切片中

// slice.Intersection
func TestSliceIntersection(t *testing.T) {

    s1 := []string{"whw", "naruto", "sasuke"}
    s2 := []string{"whw", "sakurua"}
    s3 := []string{"sakurua"}

    fmt.Println("1: ", slice.Intersection(s1, s2)) // [whw]
    fmt.Println("2: ", slice.Intersection(s1, s3)) // []

}

slice.IndexOf:获取元素在切片中的索引(0开始) 

func TestSS(t *testing.T) {
    s1 := []string{"whw", "naruto", "sasuke"}

    fmt.Println(slice.IndexOf(s1, "whw"))    // 0
    fmt.Println(slice.IndexOf(s1, "sasuke")) // 2
    fmt.Println(slice.IndexOf(s1, "whw2"))   // -1
    fmt.Println(slice.IndexOf(s1, "whw3"))   // -1

}

maputil:ForEach与Filter

package a_lancet_tests

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

func TestS1(t *testing.T) {

    m1 := map[string]int{
        "a1": 1,
        "a2": 22,
        "a3": 34,
        "a4": 66,
    }

    // ForEach
    maputil.ForEach(m1, func(key string, val int) {
        fmt.Println("key: ", key, "val: ", val)
    })
    /*
        key:  a1 val:  1
        key:  a2 val:  22
        key:  a3 val:  34
        key:  a4 val:  66
    */

    // Filter
    filterFunc := func(_ string, val int) bool {
        return val > 30
    }

    ret := maputil.Filter(m1, filterFunc)
    fmt.Println("ret: ", ret) // ret:  map[a3:34 a4:66]
}

~~~

posted on 2023-05-29 15:14  江湖乄夜雨  阅读(356)  评论(0编辑  收藏  举报