go - 我需要释放指针吗
问题描述
我正在 Go 中实现队列。
type Node struct {
Value interface{}
Next *Node
}
type Queue struct {
Front *Node
Rear *Node
}
func (q *Queue) IsEmpty() bool {
if q.Front == nil {
return true
}
return false
}
func (q *Queue) Dequeue() (interface{}, error) {
if q.IsEmpty() {
return nil, errors.New(constants.EMPTY)
}
tmp := q.Front
result := tmp.Value
q.Front = q.Front.Next
if q.Front == nil {
q.Front = nil
q.Rear = nil
}
tmp = nil // free tmp
return result, nil
}
在 Dequeue 函数中,我是否需要通过将 tmp 指针设置为 nil 来释放它,或者 Go 会为我做这件事?请为我详细解释。
提前致谢!
解决方案
Go 有垃圾收集,所以它会释放它没有引用的内存空间。
当Dequeue
函数结束时,您将失去对为您出列的变量分配的空间的引用,垃圾收集器将释放它。你不必否定它。
推荐阅读
- google-analytics - 如何在广告管理器仪表板中跟踪 Facebook 和 Google 的应用安装(应用安装活动)?
- python - 如何读取循环以读取文本文件中每一行中的每个元素
- android - 谷歌地方自动完成并不总是显示建议,当它显示时,我无法选择一个地方
- android - 如何在 Kotlin 中处理后按
- reactjs - 登录反应本机后我无法刷新应用程序
- azure-boards - Azure 中的连接板
- javascript - 如何检查 shift+tab 是否被按下?
- javascript - 如何在猫鼬中加入和运行 NOT EXISTS 查询
- html - 如何修复多个旋转 90 度时相互重叠
- jenkins - 如何通过詹金斯使用集线器自动创建 git pull-request?