45

collection: 封装map与slice,提升golang开发效率

 4 years ago
source link: https://www.tuicool.com/articles/2iMvimr
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.

golang的数组与map缺少像php一样很多的系统api的支持,使得诸如像获取map中所有key或者打乱数组等这些操作都需要自己写一个api。而此时collection应运而生,collection是一个数据结构可以很轻易的跟map和slice自由转换。collection几乎包括了你开发所需要的各种api操作,php有的都会有。

对map数组的支持使得你可以对数据库查来的数据进行诸如where查询,筛选乃至于修改等操作。极大的提升了你的golang开发效率。而且collection致力于提供优雅的便捷的且健壮api,使得你开发的代码十分简洁易于维护。

项目地址: https://github.com/chenhg5/co...

文档地址: https://godoc.org/github.com/...

开发不易,如若觉得有用,欢迎star,欢迎pr

使用例子

获取map的所有key

a := map[string]interface{}{
        "name": "mike",
        "sex":  1,
    }

fmt.Println(Collect(a).Keys().ToStringArray())

// Output: []string{"name", "sex"}

数组排序

a := []int{4, 5, 2, 3, 6, 7}

fmt.Println(Collect(a).Sort().ToIntArray())

// Output: []int{2, 3, 4, 5, 6, 7}

数组打乱

a := []int{4, 5, 2, 3, 6, 7}

fmt.Println(Collect(a). Shuffle().ToIntArray())

// Output: []int{ 3, 2, 5, 4, 6, 7}

更多例子在这里


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK