go not respecting posix unlink semantics?

xuanbao · · 602 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>When I do man unlink, the man page says that if I unlink a file that&#39;s open, the holder of that file descriptor can still finish reading the file, and the file will be actually deleted only when the last file descriptor is closed. However, in Go, for some reason this doesn&#39;t seem to happen. I&#39;ve tried with both os.Remove and syscall.Unlink, and the reader gets a &#34;bad file descriptor error&#34; after the file is removed. I have an example in this gist that demonstrates this:</p> <p><a href="https://gist.github.com/shaladdle/a3a170b7c673334847f8">https://gist.github.com/shaladdle/a3a170b7c673334847f8</a></p> <p>Has anyone run into this? Is it expected behavior?</p> <hr/>**评论:**<br/><br/>RalphCorderoy: <pre><p>You are closing the file before unlinking it.</p> <blockquote> <p>Each time a &#34;defer&#34; statement executes, the function value and parameters to the call are evaluated as usual and saved anew but the actual function is not invoked.</p> </blockquote> <p>See <a href="https://golang.org/ref/spec#Defer_statements">https://golang.org/ref/spec#Defer_statements</a></p></pre>g_o_b_o_y_s: <pre><p>Ah you&#39;re right! What a silly mistake. Thanks!</p></pre>velco: <pre><p>You can use:</p> <pre><code>defer func() { check(syscall.Close(fd)) }() </code></pre></pre>CMUComputerScience: <pre><p>What a n00b move.</p> <p>We thought we trained you better</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

602 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传