Im having problems with os.OpenFile help appreciated.

polaris · · 943 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am pretty sure that its the perm FileMode that is tripping me up. I don&#39;t really understand it. It seems to be a system of numbers to tell the file what you are allowed to do. How are you supposed to know which numbers will allow what you want? and isn&#39;t this just and more complicated repetition of the flag you pass to the function? This has been frustrating and confusing.</p> <hr/>**评论:**<br/><br/>faiface: <pre><p>Those numbers originated from Unix (I think) and are pretty much well known. But, for most usecases you can avoid them, there are simple <code>os.Open</code> and <code>os.Create</code> functions.</p> <p><code>os.Open</code> opens a file for reading.</p> <p><code>os.Create</code> opens a file for writing.</p></pre>SamJTWIV: <pre><p>Yeah thats what I keep reading. The problem is neither of those work for appending. Because os.Open is read only and os.Create truncates if file already exists. So I guess I am falling outside of most use cases.</p></pre>faiface: <pre><p>Yeah, so then you need to search and study how FileMode numbers work :)</p></pre>Thaxll: <pre><p>See that: <a href="https://en.wikipedia.org/wiki/File_system_permissions#Numeric_notation" rel="nofollow">https://en.wikipedia.org/wiki/File_system_permissions#Numeric_notation</a></p></pre>SamJTWIV: <pre><p>I looked there and it said that 0777 allowed everything so i tried that and got errors saying there was a problem with my File variable and nothing worked.</p></pre>0xjnml: <pre><p>Try 0660.</p></pre>SamJTWIV: <pre><p>This worked. Thanks. Though Im still confused as to why the function doesn&#39;t just use the os.O_APPEND to figure out what permissions you want.</p></pre>allhatenocattle: <pre><p>If you are being tripped up, I&#39;d recommend just using os.Open or os.Create as the docs say in os.OpenFile.</p> <p>The wikipedia entry on file system permissions is helpful if you aren&#39;t familiar with them: <a href="https://en.wikipedia.org/wiki/File_system_permissions" rel="nofollow">https://en.wikipedia.org/wiki/File_system_permissions</a></p></pre>WikiTextBot: <pre><p><strong>File system permissions</strong></p> <p>Most file systems have methods to assign permissions or access rights to specific users and groups of users. These systems control the ability of the users to view, change, navigate, and execute the contents of the file system.</p> <hr/> <p><sup>[</sup> <a href="https://www.reddit.com/message/compose?to=kittens_from_space" rel="nofollow"><sup>PM</sup></a> <sup>|</sup> <a href="https://reddit.com/message/compose?to=WikiTextBot&amp;message=Excludeme&amp;subject=Excludeme" rel="nofollow"><sup>Exclude</sup> <sup>me</sup></a> <sup>|</sup> <a href="https://np.reddit.com/r/golang/about/banned" rel="nofollow"><sup>Exclude</sup> <sup>from</sup> <sup>subreddit</sup></a> <sup>|</sup> <a href="https://np.reddit.com/r/WikiTextBot/wiki/index" rel="nofollow"><sup>FAQ</sup> <sup>/</sup> <sup>Information</sup></a> <sup>|</sup> <a href="https://github.com/kittenswolf/WikiTextBot" rel="nofollow"><sup>Source</sup></a> <sup>]</sup> <sup>Downvote</sup> <sup>to</sup> <sup>remove</sup> <sup>|</sup> <sup>v0.27</sup></p></pre>SamJTWIV: <pre><p>Yeah thats what I keep reading. The problem is neither of those work for appending. Because os.Open is read only and os.Create truncates if file already exists. So I guess I am falling outside of most use cases.</p></pre>SamJTWIV: <pre><p>Thanks everyone.</p></pre>

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

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