![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
golang
文章平均质量分 65
cist-tty
golang/云计算 开荒学习中,请多指教。
展开
-
golang--好用的第三方库1
好用的golang库1 (信号量、单飞、信号栅栏、分组操作、分布式锁)1 信号量golang.org/x/sync :使用互斥锁 +List 实现的。互斥锁实现其它字段的保护,而 List 实现了一个等待队列,等待者的通知是通过 Channel 的通知机制实现的。https://github.com/marusama/semaphore :可以动态更改资源容量的信号量2 单飞模式https://pkg.go.dev/golang.org/x/sync/singleflight 合并读请求3 信号原创 2021-05-26 11:52:20 · 792 阅读 · 1 评论 -
golang--通过chan实现互斥锁
通过chan实现互斥锁利用 select+chan 的方式,很容易实现 TryLock、Timeout 的功能原理chan中有一把锁lock 可以保护chan中的字段,同时chan的send和recev 两种角色存在一种hapends-befores的关系。type hchan struct { qcount uint // total data in the queue dataqsiz uint // size of the circular qu原创 2021-05-17 22:37:09 · 659 阅读 · 0 评论 -
golang--获取goroutine id的两种实现
Golang–获取goroutine id 的两种实现众所周知,在golang并发编程的一些情况下,需要打印一下goroutien的id号,怎么来实现呢?下面提供两种方法:从runtime.Stack()方法中获取方法栈,然后从栈帧处获取;获取运行时g指针,反解析出g的结构。g指针时保存在当前goroutine的TLS对象中。1 解析方法栈func GoID() int { var buf [64]byte n := runtime.Stack(buf[:], false)原创 2021-02-02 16:38:42 · 1767 阅读 · 0 评论 -
golang——go 编程模式
go编程模式实战左耳朵耗子老师在极客时间的课程,这里整理记录下1.函数编程type Option func(*Server)func Protocol(p string) Option { return func(s *Server) { s.Protocol = p }}func Timeout(timeout time.Duration) Option { return func(s *Server) { s.Timeout =原创 2021-01-23 17:06:42 · 550 阅读 · 0 评论