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

cg33 · · 3486 次点击 · 开始浏览    置顶
这是一个创建于 的主题,其中的信息可能已经有所发展或是发生改变。

golang的数组与map缺少像php一样很多的系统api的支持,使得诸如像获取map中所有key或者打乱数组等这些操作都需要自己写一个api。而此时collection应运而生,collection是一个数据结构可以很轻易的跟map和slice自由转换。collection几乎包括了你开发所需要的各种api操作,php有的都会有。 对map数组的支持使得你可以对数据库查来的数据进行诸如where查询,筛选乃至于修改等操作。极大的提升了你的golang开发效率。而且collection致力于提供优雅的便捷的且健壮api,使得你开发的代码十分简洁易于维护。 项目地址:https://github.com/chenhg5/collection<br> 文档地址:https://godoc.org/github.com/chenhg5/collection#Collection 欢迎star,欢迎pr 使用例子 ---- [获取map的所有key](https://godoc.org/github.com/chenhg5/collection#BaseCollection.Keys) ```golang a := map[string]interface{}{ "name": "mike", "sex": 1, } fmt.Println(Collect(a).Keys().ToStringArray()) // Output: [name sex] ``` [数组排序](https://godoc.org/github.com/chenhg5/collection#BaseCollection.Sort) ```golang a := []int{4, 5, 2, 3, 6, 7} fmt.Println(Collect(a).Sort().ToIntArray()) // Output: [2 3 4 5 6 7] ``` [数组打乱](https://godoc.org/github.com/chenhg5/collection#BaseCollection.Shuffle) ```golang a := []int{4, 5, 2, 3, 6, 7} fmt.Println(Collect(a). Shuffle().ToIntArray()) // Output: [3 2 5 4 6 7] ``` [Select与Where](https://godoc.org/github.com/chenhg5/collection#BaseCollection.Where) ```golang a := []map[string]interface{}{ {"name": "mike", "sex": 0}, {"name": "Mary", "sex": 1}, {"name": "Jane", "sex": 2}, } fmt.Println(Collect(a).Where("sex", 0).Select("sex").ToMapArray()) // Output: [map[sex:0]] ``` [更多例子在这里](https://godoc.org/github.com/chenhg5/collection#pkg-examples)

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

3486 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传