函数在执行时分配资源,执行完毕后自动释放;而 goroutine 在创建时分配资源,需显式关闭或使用 context、waitgroup 确保释放,防止内存泄漏和性能下降。

Golang 函数与 Goroutine 的资源分配实战
简介
在 Go 语言中,函数和 goroutine 是常被使用的并发性机制。函数是执行代码的单元,而 goroutine 则是并发执行的函数。合理分配函数和 goroutine 的资源至关重要,以优化程序性能和防止资源浪费。
函数的资源分配
函数只在执行时占用资源,在执行完毕后自动释放所有资源。可以通过使用 defer 语句来确保资源在函数返回前释放。例如:
func cleanup() {
// 释放资源
}
func main() {
defer cleanup()
// 执行代码
}
Goroutine 的资源分配
与函数不同,goroutine 在创建时分配资源,并且直到 goroutine 退出才释放这些资源。如果不及时释放 goroutine,会导致内存泄漏和性能下降。
有几种方法可以确保 goroutine 释放资源:
-
显式关闭 channel 和 other 资源:
Golang Uber Dig下载使用 uber-go/dig 在 Golang 中实现依赖注入——基于反射容器,提供 Provide/Invoke、dig.In/dig.Out 参数与结果对象,支持命名依赖等
c := make(chan int) // 使用 channel close(c)
-
使用
context.Done():ctx, cancel := context.WithCancel(context.Background()) // 使用 context cancel()
-
通过
WaitGroup等待 goroutine 退出:var wg sync.WaitGroup wg.Add(1) go func() { // 执行代码 wg.Done() }() wg.Wait()
实战案例
在以下示例中,我们创建了多个 goroutine 来执行异步任务:
package main
import (
"context"
"fmt"
"sync"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
for i := 0; i <p>运行此程序,输出如下:</p><pre class="brush:php;toolbar:false;">Goroutine 0
Goroutine 1
Goroutine 2
Goroutine 3
Goroutine 4
Goroutine 5
Goroutine 0 cancelled
Goroutine 1 cancelled
Goroutine 2 cancelled
Goroutine 3 cancelled
Goroutine 4 cancelled
Goroutine 5 cancelled
Goroutine 6
Goroutine 7
Goroutine 8
Goroutine 9
Goroutine 6 cancelled
Goroutine 7 cancelled
Goroutine 8 cancelled
Goroutine 9 cancelled
从输出中可以看出,当 cancel() 函数被调用时,所有 goroutine 都及时释放了资源。
golang免费学习笔记(深入):立即使用
在学习笔记中,你将探索golang的核心概念和高级技巧!










