<p>Hey guys, hope you're having the best monday you could have.</p>
<p>I don't know if this is an import problem, a syntax problem, or a goeclipse problem, but I just cannot write to a file. Here is what I have:</p>
<p>package main</p>
<p>import (
"os"
"io"
"fmt"
)</p>
<p>func main() {</p>
<pre><code> filename := "output.txt"
file, err := os.Create(filename)
if err != nil {
fmt.Println(err)
}
fmt.Println(" Write to file : " + filename)
n, err := io.WriteString(file, " Hello World !")
if err != nil {
fmt.Println(n, err)
}
file.Close()
</code></pre>
<p>}</p>
<p>I made this just to figure out how to write to a file. It runs, displays "Write to file: " and shows the file I want to write to, but just simply won't write to the file.</p>
<p>Is there something I'm missing or some kind of thing I have to do in eclipse to get this to work?</p>
<p>Thanks again SO much!</p>
<p>EDIT: Figured it out thanks to you guys. Issue was a.) I needed to specifically refresh the program and not just clean and save and b.) my directory was really really weird so I needed to change that. Thanks again!</p>
<hr/>**评论:**<br/><br/>davecheney: <pre><p>I suspect that the file is being written, but not where you think. Compile and run this program. <a href="http://play.golang.org/p/C-N1zdJalN" rel="nofollow">http://play.golang.org/p/C-N1zdJalN</a></p></pre>Elegantmetal: <pre><p>Awesome this was the issue. It was writing to a VERY weird spot on my 2nd hardrive (some weird folder in my steam directory?) and I was able to change it. Thanks!</p></pre>slmyers: <pre><p>refresh the project and you should see it there.</p></pre>Elegantmetal: <pre><p>By refresh the project do you mean clean and save? Because neither of those worked : /</p></pre>slmyers: <pre><p>I've never used the go plugin for eclipse, so this might be wrong, and if so forgive me. </p>
<p>Select the project and press f5 and/or right click and look for "Refresh".</p></pre>Elegantmetal: <pre><p>Ok got it! This solved my main issue of just not working and then davecheney's solved the other half of my issue. I didn't think refreshing worked but it ended up writing to a SUPER weird spot on my 2nd hard drive. Thanks everybody!</p></pre>The_Sly_Marbo: <pre><p>Make sure you check the error returned by <code>file.Close()</code>. That may explain what's going on.</p></pre>Elegantmetal: <pre><p>file.Close() doesn't seem to be giving me an error when I step over it in debugging. Since I'm new to GDB I could be very incorrect though</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传