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

Golang 函数与 Goroutine 的资源分配实战
简介
在 Go 语言中,函数和 goroutine 是常被使用的并发性机制。函数是执行代码的单元,而 goroutine 则是并发执行的函数。合理分配函数和 goroutine 的资源至关重要,以优化程序性能和防止资源浪费。
函数的资源分配
函数只在执行时占用资源,在执行完毕后自动释放所有资源。可以通过使用 defer 语句来确保资源在函数返回前释放。例如:
func cleanup() {
// 释放资源
}
func main() {
defer cleanup()
// 执行代码
}Goroutine 的资源分配
与函数不同,goroutine 在创建时分配资源,并且直到 goroutine 退出才释放这些资源。如果不及时释放 goroutine,会导致内存泄漏和性能下降。
立即学习“go语言免费学习笔记(深入)”;
有几种方法可以确保 goroutine 释放资源:
-
显式关闭 channel 和 other 资源:
MvMmall 网店系统下载免费的开源程序长期以来,为中国的网上交易提供免费开源的网上商店系统一直是我们的初衷和努力奋斗的目标,希望大家一起把MvMmall网上商店系统的免费开源进行到底。2高效的执行效率由资深的开发团队设计,从系统架构,数据库优化,配以通过W3C验证的面页模板,全面提升页面显示速度和提高程序负载能力。3灵活的模板系统MvMmall网店系统程序代码与网页界面分离,灵活的模板方案,完全自定义模板,官方提供免费模
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 < 10; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
fmt.Println("Goroutine", i)
// 使用 ctx 来响应取消请求
select {
case <-ctx.Done():
fmt.Println("Goroutine", i, "cancelled")
return
default:
// 继续执行任务
}
}(i)
}
// 取消所有 goroutine
cancel()
wg.Wait()
}运行此程序,输出如下:
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 都及时释放了资源。









