Go 入门 08:goroutine 与 channel
Go 入门 08goroutine 与 channel并发是 Go 的招牌特性。Rob Pike 提出“Don’t communicate by sharing memory; share memory by communicating”——不要通过共享内存来通信而要通过通信来共享内存。这正是 goroutine channel 的核心哲学。一、goroutine1.1 启动一个 goroutine只需在函数调用前加go关键字gofunc(){fmt.Println(Hello from goroutine)}()goroutine 是 Go 运行时管理的轻量级线程初始栈仅 2KB线程通常 1MB可动态伸缩由 Go 调度器G-M-P 模型映射到 OS 线程单机轻松运行百万级goroutine。1.2 例子funcmain(){fori:0;i5;i{gofunc(nint){fmt.Println(worker,n)}(i)}time.Sleep(time.Second)// 简单等待不推荐}注意闭包捕获循环变量的坑Go 1.22 已修复最佳做法是把变量作为参数传入。二、channelchannel 是 goroutine 之间的类型化通信管道。2.1 创建 channelch:make(chanint)// 无缓冲ch:make(chanint,10)// 缓冲为 102.2 发送与接收ch-42// 发送v:-ch// 接收v,ok:-ch// okfalse 表示 channel 已关闭且无数据close(ch)// 关闭 channel只能由发送方关闭2.3 无缓冲 vs 有缓冲类型行为无缓冲发送阻塞直到有人接收强同步有缓冲缓冲未满不阻塞发送未空不阻塞接收三、最经典的并发模式3.1 使用 channel 等待 goroutine 完成funcmain(){done:make(chanstruct{})gofunc(){doWork()close(done)}()-done// 阻塞等待}struct{}是零字节类型只用作信号。3.2 sync.WaitGroup更通用的等待方式varwg sync.WaitGroupfori:0;i5;i{wg.Add(1)gofunc(nint){deferwg.Done()fmt.Println(worker,n)}(i)}wg.Wait()3.3 生产者-消费者funcproducer(chchan-int){fori:0;i10;i{ch-i}close(ch)// 告知消费者结束}funcconsumer(ch-chanint){forv:rangech{// 自动检测 closefmt.Println(v)}}funcmain(){ch:make(chanint,5)goproducer(ch)consumer(ch)}chan- int仅发送-chan int仅接收单向 channel 增强类型安全。3.4 Worker Pool最常用模式funcworker(idint,jobs-chanint,resultschan-int){forj:rangejobs{results-j*2}}funcmain(){jobs:make(chanint,100)results:make(chanint,100)forw:1;w3;w{goworker(w,jobs,results)}forj:1;j9;j{jobs-j}close(jobs)fori:0;i9;i{fmt.Println(-results)}}四、select 多路复用select{casev:-ch1:fmt.Println(from ch1:,v)casech2-42:fmt.Println(sent to ch2)case-time.After(time.Second):fmt.Println(timeout)default:fmt.Println(no channel ready)}要点select随机选择一个就绪的 casedefault让 select 不阻塞time.After是优雅实现超时的标准手法。4.1 实现超时控制ch:make(chanstring)gofetch(ch)select{casedata:-ch:fmt.Println(data)case-time.After(3*time.Second):fmt.Println(timeout!)}五、context现代化的取消传递context.Context是 Go 1.7 引入的标准并发传播机制几乎所有标准库与第三方库的网络/IO 函数都接受 ctx。ctx,cancel:context.WithTimeout(context.Background(),2*time.Second)defercancel()gofunc(){select{case-time.After(5*time.Second):fmt.Println(done)case-ctx.Done():fmt.Println(canceled:,ctx.Err())}}()-ctx.Done()实战中HTTP/RPC 请求第一个参数永远是 ctxfuncGetUser(ctx context.Context,idint64)(*User,error){...}六、并发安全工具6.1 sync.Mutex / RWMutexvarmu sync.Mutexvarcounterintfuncadd(){mu.Lock()defermu.Unlock()counter}6.2 sync.Oncevaronce sync.Once once.Do(func(){// 只会执行一次常用于单例})6.3 sync/atomicvarnint64atomic.AddInt64(n,1)v:atomic.LoadInt64(n)七、goroutine 泄漏最常见原因channel 永远收不到也不关闭。funcleak(){ch:make(chanint)// 无缓冲gofunc(){ch-1// 没人接收永远阻塞}()// 没读 chgoroutine 泄漏}避免方法用context控制 goroutine 生命周期关闭 channel 通知接收方用select default避免阻塞发送。八、实战并发抓取多个 URLpackagemainimport(fmtionet/httpsynctime)funcfetch(urlstring,wg*sync.WaitGroup,resultschan-string){deferwg.Done()start:time.Now()resp,err:http.Get(url)iferr!nil{results-fmt.Sprintf(%s ERROR: %v,url,err)return}deferresp.Body.Close()body,_:io.ReadAll(resp.Body)results-fmt.Sprintf(%s %d bytes in %v,url,len(body),time.Since(start))}funcmain(){urls:[]string{https://example.com,https://golang.org,https://www.google.com,}varwg sync.WaitGroup results:make(chanstring,len(urls))for_,u:rangeurls{wg.Add(1)gofetch(u,wg,results)}gofunc(){wg.Wait()close(results)}()forr:rangeresults{fmt.Println(r)}}九、小结goroutine 是 Go 的轻量级并发原语channel 实现 goroutine 间安全通信select实现多路复用与超时context是取消信号的高速公路并发场景永远警惕goroutine 泄漏。下一篇我们将学习 Go 的工程化基础包与模块go mod。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2625377.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!