我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

延迟执行关闭文件

I remember reading about a feature of go in that you can declare a file handle to be closed when you first open it, and at the end of the (function? method? garbage collection?) it would automatically close.

What is the syntax for this and what is it called?

It sounds like you are after the defer keyword. This lets you specify statements that will be executed when the function exits. For example:

defer f.Close()

Deferred cleanup routines are executed in the opposite order they are created.

近期文章

  • sync.WaitGroup是Barrier的实现吗?
  • 关于HTTP劫持和保持活动状态
  • 无法连接时停止频道
  • 在Golang中从mongodb检索非结构化数组
  • 这是在golang中终止goroutine的正确方法吗?
  • Golang-Docker API-解析ImagePull的结果
  • 当没有通道可供读取时,怎么办?
  • 使用容器/堆实现优先级队列
  • 使用go / ast包
  • 如何使用Go制作GUI窗口?
  • 在GAE上整合Paypal IPN和Golang
  • 如何安全地丢弃golang数据库/ sql池连接,例如当它们指向只读副本时?
  • 有没有办法告诉Go中的文件类型(可执行文件,文本等)?
  • 模板中的bson.ObjectId
  • goroutine是否适合大型并行计算边界问题?
  • goapp服务:找不到dev_appserver.py
  • Golang net / http请求正文始终为空
  • 我如何在func中声明chan的切片
  • 具有称为“ * er”的非接口是惯用的吗
  • 如何避免n-n关系中的“导入周期”

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华