<p>Hello</p>
<p>I have a simple go file that will hit a rest api, create .txt files and exit. It hits several urls and I log statements to the command prompt (mainly the filename created). When I do "go build", it creates an .exe ("myprogram.exe").</p>
<p>When I double click the exe or right click and run as admin, it opens up a command prompt, looks like it starts running but then just exits. By exit, the window closes and I can't even figure out what the error message that caused the crash. There are no files created. I did manage to get a glimpse at part of the message and basically read something about invalid memory pointer/dereference. </p>
<p>However, if I type in the program name in my command prompt, it runs fine and exits cleanly, prints the log statements, dispalys errors that I can look at to debug if there are any.</p>
<p>How can I fix this issue of clicking the exe not running the program? I tried creating a batch file to start the exe but that did the same error as above. Also, if I got rid of all of the print statements to the console and wanted this program to "run in the background" without opening up a command prompt, how would I do this?</p>
<p>Any help is appreciated. Thanks!</p>
<hr/>**评论:**<br/><br/>intermernet: <pre><p>You can either create a shortcut to the exe, then modify it's properties and add cmd /k` to the beginning of the command line so that the command reads as something like</p>
<pre><code>cmd /k myprogram.exe
</code></pre>
<p>or you can add something like the following code snippet to the end of your program: EDIT: This won't do anything if the program is crashing before it reaches this point. The first option may be a better way to go.</p>
<pre><code>fmt.Println("Press Enter to continue...")
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
</code></pre>
<p><a href="http://play.golang.org/p/YwYtnyh_h6" rel="nofollow">Playground</a></p>
<p>To run a program in the background on Windows, you may want to look into <a href="https://golang.org/x/sys/windows/svc" rel="nofollow">https://golang.org/x/sys/windows/svc</a></p>
<blockquote>
<p>Package svc provides everything required to build Windows service.</p>
</blockquote></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传