<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Goroutine on Tequila's 学习笔记</title><link>https://latnx.github.io/docs/notes/d-205f3859abe469be/d-f7ccae636d10bb41/d-9a104ee4130d428d/</link><description>Recent content in Goroutine on Tequila's 学习笔记</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><copyright>© 2026 Tequila</copyright><atom:link href="https://latnx.github.io/docs/notes/d-205f3859abe469be/d-f7ccae636d10bb41/d-9a104ee4130d428d/index.xml" rel="self" type="application/rss+xml"/><item><title>Chan</title><link>https://latnx.github.io/docs/notes/d-205f3859abe469be/d-f7ccae636d10bb41/d-9a104ee4130d428d/n-41c9868b0abc1352/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://latnx.github.io/docs/notes/d-205f3859abe469be/d-f7ccae636d10bb41/d-9a104ee4130d428d/n-41c9868b0abc1352/</guid><description>ch := make(chan int) // 创建一个管道ch ch &amp;lt;- v // 向管道ch中发送数据v. v := &amp;lt;-ch // 从管道中读取数据存储到变量v close(ch) // 关闭管道ch 双向channel和单向channel 协程之间可以利用channel来传递数据</description></item><item><title>Context</title><link>https://latnx.github.io/docs/notes/d-205f3859abe469be/d-f7ccae636d10bb41/d-9a104ee4130d428d/n-c9549f0a67d3d027/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://latnx.github.io/docs/notes/d-205f3859abe469be/d-f7ccae636d10bb41/d-9a104ee4130d428d/n-c9549f0a67d3d027/</guid><description>type Context interface { Deadline() (deadline time.Time, ok bool) Done() &amp;lt;-chan struct{} Err() error Value(key interface{}) interface{} } func WithCancel(parent Context) (ctx Context, cancel CancelFunc) func WithDeadline(parent Context, deadline time.Time) (Context, CancelFunc) func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) func WithValue(parent Context, key, val interface{}) Context context.WithCancel # package main import ( &amp;#34;context&amp;#34; &amp;#34;fmt&amp;#34; &amp;#34;time&amp;#34; ) func main() { ctx, cancel := context.WithCancel(context.Background()) go Watch(ctx, &amp;#34;goroutine1&amp;#34;) go Watch(ctx, &amp;#34;goroutine2&amp;#34;) time.Sleep(6 * time.</description></item><item><title>Select</title><link>https://latnx.github.io/docs/notes/d-205f3859abe469be/d-f7ccae636d10bb41/d-9a104ee4130d428d/n-d1f9c5c78e6447aa/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://latnx.github.io/docs/notes/d-205f3859abe469be/d-f7ccae636d10bb41/d-9a104ee4130d428d/n-d1f9c5c78e6447aa/</guid><description>Go语言的 select 语句，是用来起一个goroutine监听多个Channel的读写事件，提高从多个Channel获取信息的效率，相当于也是单线程处理多个IO事件，其思想基本相同。
select { case &amp;lt;- channel1: // 如果从channel1读取数据成功，执行case语句 do ... case channel2 &amp;lt;- 1: // 如果向channel2写入数据成功，执行case语句 do ... default: // 如果上面都没有成功，进入default处理流程 do ... }</description></item><item><title>Sync</title><link>https://latnx.github.io/docs/notes/d-205f3859abe469be/d-f7ccae636d10bb41/d-9a104ee4130d428d/n-2f406ea8af75b16b/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://latnx.github.io/docs/notes/d-205f3859abe469be/d-f7ccae636d10bb41/d-9a104ee4130d428d/n-2f406ea8af75b16b/</guid><description>1️⃣ Mutex（互斥锁） # 用于保护共享资源。 常用方法：
Lock() Unlock() TryLock()（Go 1.18+） 示例 # package main import ( &amp;#34;fmt&amp;#34; &amp;#34;sync&amp;#34; ) func main() { var mu sync.Mutex count := 0 mu.Lock() count++ mu.Unlock() fmt.Println(count) } 2️⃣ RWMutex（读写锁） # 读写分离，提高并发性能。
常用方法：
Lock() / Unlock() （写锁） RLock() / RUnlock() （读锁） ✅ 最小示例 # package main import ( &amp;#34;fmt&amp;#34; &amp;#34;sync&amp;#34; ) func main() { var mu sync.RWMutex data := 10 mu.RLock() fmt.Println(data) mu.RUnlock() } 3️⃣ WaitGroup（等待协程完成） # 用于等待多个 goroutine 执行完成。 常用方法：</description></item><item><title>协程池</title><link>https://latnx.github.io/docs/notes/d-205f3859abe469be/d-f7ccae636d10bb41/d-9a104ee4130d428d/n-fc31f98d11ed8000/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://latnx.github.io/docs/notes/d-205f3859abe469be/d-f7ccae636d10bb41/d-9a104ee4130d428d/n-fc31f98d11ed8000/</guid><description>Go语言虽然有着高效的GMP调度模型，理论上支持成千上万的goroutine，但是goroutine过多，对调度，gc以及系统内存都会造成压力，这样会使我们的服务性能不升反降。常用做法可以用池化技术，构造一个协程池，把进程中的协程控制在一定的数量，防止系统中goroutine过多，影响服务性能。
sem := make(chan struct{}, 10) for _, task := range tasks { sem &amp;lt;- struct{}{} go func(task Task) { defer func() { &amp;lt;-sem }() task() }(task) } package main import ( &amp;#34;fmt&amp;#34; &amp;#34;sync&amp;#34; &amp;#34;sync/atomic&amp;#34; &amp;#34;time&amp;#34; ) type Task struct { f func() error // 具体的任务逻辑 } func NewTask(funcArg func() error) *Task { return &amp;amp;Task{ f: funcArg, } } type Pool struct { RunningWorkers int64 // 运行着的worker数量 Capacity int64 // 协程池worker容量 JobCh chan *Task // 用于worker取任务 sync.</description></item></channel></rss>