douya2982 2012-07-24 05:02
浏览 121
已采纳

什么时候关闭文件?

Is necessary to close a file when you only want create it? I'm supposed that it's only necessary in case of reading or writing.

_, err := os.OpenFile(name, os.O_CREATE, 0640)
  • 写回答

2条回答 默认 最新

  • dongyao2022 2012-07-24 05:07
    关注

    Will it work? Yes. The file will be created.

    Should you do it? No. It is a bad idea in general even though you can get away with it sometimes.

    Opening a file allocates resources like a file handle to your process. You should close it to free those resources. Otherwise they will be unavailable until the process dies.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?