源码地址:https://github.com/emirpasic/gods
前言
发现这个包是看见 Leetcode 在线 Go 环境给的描述
版本:
Go 1.16.2支持 https://godoc.org/github.com/emirpasic/gods 第三方库。
于是查看,发现是为了弥补 Golang 本身缺少的集合与容器等数据结构编写的,既然刷题可以用到、并且可以减少工作量,所以有必要学习一下。
尝试
开源项目的优势,文档说明齐全,照着文档读一遍即可理解大概,如果之前有 OOP 相关的编码经验,那就更容易啦!
大概看了下 Readme,感觉也没什么好写的🤣
基本上就是需要用什么然后直接 xxx.New(),之后就可以 OOP 直接调用方法,方法的命名与其他编程语言类似。
给出一个 arraylist 的例子:
package mainimport ("github.com/emirpasic/gods/lists/arraylist""github.com/emirpasic/gods/utils")func main() {list := arraylist.New()list.Add("a") // ["a"]list.Add("c", "b") // ["a","c","b"]list.Sort(utils.StringComparator) // ["a","b","c"]_, _ = list.Get(0) // "a",true_, _ = list.Get(100) // nil,false_ = list.Contains("a", "b", "c") // true_ = list.Contains("a", "b", "c", "d") // falselist.Swap(0, 1) // ["b","a",c"]list.Remove(2) // ["b","a"]list.Remove(1) // ["b"]list.Remove(0) // []list.Remove(0) // [] (ignored)_ = list.Empty() // true_ = list.Size() // 0list.Add("a") // ["a"]list.Clear() // []list.Insert(0, "b") // ["b"]list.Insert(0, "a") // ["a","b"]}
