Golang中使用set
两种 go 实现 set 的思路, 分别是 map 和 bitset。
map 的 key 肯定是唯一的,而这恰好与 set 的特性一致,天然保证 set 中成员的唯一性。而且通过 map 实现 set,在检查是否存在某个元素时可直接使用 _, ok := m[key] 的语法,效率高。
原文链接:https://studygolang.com/articles/27476?fr=sidebar
golang-set-A simple set type for the Go language. Also used by Docker, 1Password, Ethereum.
在github上已经有了一个成熟的包,名为golang-set,包中提供了线程安全和非线程安全的set。提供了五个set函数:
// NewSet创建并返回空集的引用,结果集上的操作是线程安全的
func NewSet(s ...interface{}) Set {}
// NewSetFromSlice从现有切片创建并返回集合的引用,结果集上的操作是线程安全的
func NewSetFromSlice(s []interface{}) Set {}
// NewSetWith创建并返回具有给定元素的新集合,结果集上的操作是线程安全的
func NewSetWith(elts ...interface{}) Set {}
// NewThreadUnsafeSet创建并返回对空集的引用,结果集上的操作是非线程安全的
func NewThreadUnsafeSet() Set {}
// NewThreadUnsafeSetFromSlice创建并返回对现有切片中集合的引用,结果集上的操作是非线程安全的。
func NewThreadUnsafeSetFromSlice(s []interface{}) Set {}
demo
package main import (
"fmt"
"github.com/deckarep/golang-set"
) func main() {
// 默认创建的线程安全的,如果无需线程安全
// 可以使用 NewThreadUnsafeSet 创建,使用方法都是一样的。
s1 := mapset.NewSet(, , , )
fmt.Println("s1 contains 3: ", s1.Contains())
fmt.Println("s1 contains 5: ", s1.Contains()) // interface 参数,可以传递任意类型
s1.Add("poloxue")
fmt.Println("s1 contains poloxue: ", s1.Contains("poloxue"))
s1.Remove()
fmt.Println("s1 contains 3: ", s1.Contains()) s2 := mapset.NewSet(, , , ) // 并集
fmt.Println(s1.Union(s2))
}
运行结果:
s1 contains : true
s1 contains : false
s1 contains poloxue: true
s1 contains : false
Set{, , , poloxue, , }
Examples but not exhaustive:
requiredClasses := mapset.NewSet()
requiredClasses.Add("Cooking")
requiredClasses.Add("English")
requiredClasses.Add("Math")
requiredClasses.Add("Biology") scienceSlice := []interface{}{"Biology", "Chemistry"}
scienceClasses := mapset.NewSetFromSlice(scienceSlice) electiveClasses := mapset.NewSet()
electiveClasses.Add("Welding")
electiveClasses.Add("Music")
electiveClasses.Add("Automotive") bonusClasses := mapset.NewSet()
bonusClasses.Add("Go Programming")
bonusClasses.Add("Python Programming") //Show me all the available classes I can take
allClasses := requiredClasses.Union(scienceClasses).Union(electiveClasses).Union(bonusClasses)
fmt.Println(allClasses) //Set{Cooking, English, Math, Chemistry, Welding, Biology, Music, Automotive, Go Programming, Python Programming} //Is cooking considered a science class?
fmt.Println(scienceClasses.Contains("Cooking")) //false //Show me all classes that are not science classes, since I hate science.
fmt.Println(allClasses.Difference(scienceClasses)) //Set{Music, Automotive, Go Programming, Python Programming, Cooking, English, Math, Welding} //Which science classes are also required classes?
fmt.Println(scienceClasses.Intersect(requiredClasses)) //Set{Biology} //How many bonus classes do you offer?
fmt.Println(bonusClasses.Cardinality()) //2 //Do you have the following classes? Welding, Automotive and English?
fmt.Println(allClasses.IsSuperset(mapset.NewSetFromSlice([]interface{}{"Welding", "Automotive", "English"}))) //true
Golang中使用set的更多相关文章
- golang中的race检测
golang中的race检测 由于golang中的go是非常方便的,加上函数又非常容易隐藏go. 所以很多时候,当我们写出一个程序的时候,我们并不知道这个程序在并发情况下会不会出现什么问题. 所以在本 ...
- 基础知识 - Golang 中的正则表达式
------------------------------------------------------------ Golang中的正则表达式 ------------------------- ...
- golang中的reflect包用法
最近在写一个自动生成api文档的功能,用到了reflect包来给结构体赋值,给空数组新增一个元素,这样只要定义一个input结构体和一个output的结构体,并填写一些相关tag信息,就能使用程序来生 ...
- Golang中的坑二
Golang中的坑二 for ...range 最近两周用Golang做项目,编写web服务,两周时间写了大概五千行代码(业务代码加单元测试用例代码).用Go的感觉很爽,编码效率高,运行效率也不错,用 ...
- Golang 中的坑 一
Golang 中的坑 短变量声明 Short variable declarations 考虑如下代码: package main import ( "errors" " ...
- google的grpc在golang中的使用
GRPC是google开源的一个高性能.跨语言的RPC框架,基于HTTP2协议,基于protobuf 3.x,基于Netty 4.x. 前面写过一篇golang标准库的rpc包的用法,这篇文章接着讲一 ...
- Golang中Struct与DB中表字段通过反射自动映射 - sqlmapper
Golang中操作数据库已经有现成的库"database/sql"可以用,但是"database/sql"只提供了最基础的操作接口: 对数据库中一张表的增删改查 ...
- Golang中WaitGroup使用的一点坑
Golang中WaitGroup使用的一点坑 Golang 中的 WaitGroup 一直是同步 goroutine 的推荐实践.自己用了两年多也没遇到过什么问题.直到一天午睡后,同事扔过来一段奇怪的 ...
- Golang中使用lua进行扩展
前言 最近在项目中需要使用lua进行扩展,发现github上有一个用golang编写的lua虚拟机,名字叫做gopher-lua.使用后发现还不错,借此分享给大家. 数据类型 lua中的数据类型与go ...
- golang中Context的使用场景
golang中Context的使用场景 context在Go1.7之后就进入标准库中了.它主要的用处如果用一句话来说,是在于控制goroutine的生命周期.当一个计算任务被goroutine承接了之 ...
随机推荐
- 【接单】找我付费定制Python工具软件或网站开发、Chrome浏览器插件、油猴脚本
各位可付费找我定制Python工具软件或网站开发.Chrome插件.油猴脚本.自动化软件,可通过我做的软件来评判我的实力,一定要先和我沟通你的需求,做不了的我也不会接. 费用50元起,通过淘宝APP或 ...
- linux的/etc/passwd、/etc/shadow、/etc/group和/etc/gshadow
1./etc/passwd 存储用户信息 [root@oldboy ~]# head /etc/passwd root:x:::root:/root:/bin/bash bin:x:::bin:/b ...
- NanoHTTPD服务
需要导入nanohttpd2.3,jar包 继承NanoHTTPD public class HttpServer extends NanoHTTPD { public HttpServer(int ...
- Mac Sourcetree克隆项目提示无效的url
之前用SoucreTree拉去过另一个账号的git项目,今天创建了一个新的码云账号,克隆里面的项目是一直报错误 > 错误如下: > 原因以及解决方案:
- PHP lcg_value() 函数
实例 返回范围为 (0, 1) 的一个伪随机数: <?phpecho lcg_value();?>高佣联盟 www.cgewang.com 定义和用法 lcg_value() 函数返回范围 ...
- PHP xml_error_string() 函数
定义和用法 xml_error_string() 函数获取 XML 解析器的错误描述.高佣联盟 www.cgewang.com 如果成功,该函数则返回错误描述.如果失败,则返回 FALSE. 语法 x ...
- PHP show_source() 函数
实例 对测试文件("test.php")进行 PHP 语法高亮显示: <html><body><?phpshow_source("test. ...
- 牛客练习赛60 D 斩杀线计算大师
LINK:斩杀线计算大师 给出a,b,c三个值 求出 ax+by+cz=k的x,y,z的正整数解 保证一定有解. 考虑两个数的时候 ax+by=k 扩展欧几里得可以解决. 三个数的时候 一个暴力的想法 ...
- 可笑,你竟然不知道 Java 如何生成 UUID
先看再点赞,给自己一点思考的时间,微信搜索[沉默王二]关注这个靠才华苟且的程序员.本文 GitHub github.com/itwanger 已收录,里面还有一线大厂整理的面试题,以及我的系列文章. ...
- Windows聚焦失效问题的解决办法
1. 设置Windows聚焦 步骤:任务栏右键 → 任务栏设置 → 锁屏界面 → 背景选择Windows聚焦 2. 解决Windows聚焦失效问题 设置完Windows聚焦之后,锁屏界面却没有变. 尝 ...