beginner's question: please help fix this hello world program.

blov · · 41 次点击    
<p>Update. This question has been answered! Thank you.</p> <h2>Original version of this question:</h2> <p>Hi everyone:</p> <ol> <li><p>A preliminary question: <strong><em>is there a beginner&#39;s golang subreddit?</em></strong> This is the only one I found. I realize that for most people it would be noise so I am happy to address my future questions to a more specialized forum. </p></li> <li><p>On to my main question. I read the manual and specs and was informed that goroutines are easy: according to them, I just add the word &#34;go.&#34;</p></li> </ol> <p>This works:</p> <pre><code>package main import &#34;fmt&#34; func main() { fmt.Println(&#34;hello world&#34;) } </code></pre> <p>This does not work:</p> <pre><code>package main import &#34;fmt&#34; func main() { go fmt.Println(&#34;hello world&#34;) } </code></pre> <p>(Notice the addition of the word &#39;go&#39; at the start of the penultimate line - this is the only change.)</p> <p>For the first. Expected result: prints hello world to console. Actual result: same. </p> <p>For the second. Expected result: the second should also print &#34;hello world&#34;. Actual result: nothing gets printed.</p> <p>System: I use windows 7. I am using an older version of Go, from the last time I tried it and gave up after I couldn&#39;t compile simple code examples. The difference is this time I have read the specificaitons.</p> <p>Please let me know what I am doing wrong. The files are otherwise identical.</p> <hr/> <p>On a personal note I feel frustration, as I feel that I did my part, by reading the manual, but go is not doing its part, by doing what it says in the manual. (Which is that I must simply add the word &#34;go&#34; to start a goroutine.)</p> <p>As it is so far from what it is written on the tin, I am going to give up soon. (Another example is that I must manually fix line breaks after using go fmt, since I am a Windows user and notepad cannot open the file afterward: so it might as well be 7zip&#39;d if I can&#39;t use it. So my use of go fmt is in two steps</p> <p>1) apply go fmt</p> <p>2) fix its output so I can keep working on it</p> <p>I think nobody can argue that notepad is not an appropriate text editor for a Windows user. I looked for &#34;go fmt&#34; options that would retain carriage returns but did not find any. So, I had to spend a few minutes figuring out how to let me keep working while following best practices. It is a frustrating experience.</p> <hr/>**评论:**<br/><br/>rom16384: <pre><p>The main program is exiting before the goroutine has had an opportunity to execute. See <a href="" rel="nofollow"></a></p></pre>deusmetallum: <pre><p>This article is great for explaining go routines.</p></pre>: <pre><p>[deleted]</p></pre>very-little-gravitas: <pre><p>The spec does cover this:</p> <blockquote> <p>Program execution begins by initializing the main package and then invoking the function main. When that function invocation returns, the program exits. It does not wait for other (non-main) goroutines to complete.</p> </blockquote> <p><a href="" rel="nofollow"></a></p> <p>They could perhaps link to this from the goroutine section, but it is covered. </p></pre>drvd: <pre><p>Sorry to see you frustrated.</p> <p>The spec is correct. It never says that the function is allowed (or even required) to terminate. It describes what happens once the function terminates.</p> <p>If the whole program terminates before a goroutine is done then this goroutine is terminated too (obviously) . You clearly marked in green that &#34;upcoming lines in the program are evaluated&#34; so you are aware that the program terminates once the end of main is reached (described elsewhere in the spec). The &#34;independent concurrent thread of control&#34; exist only during the lifetime of the whole program which finishes at the end of main.</p></pre>deusmetallum: <pre><p>I think you&#39;ve misunderstood. Go routines do all the things listed, but they also terminate when the main program terminates. This is true of all threaded progammes.</p></pre>: <pre><p>[deleted]</p></pre>deusmetallum: <pre><p>TBH, I wouldn&#39;t give up on Go. It&#39;s a pretty awesome language, and recruiters are clambering to find people with those skills.</p></pre>haikubot-1911: <pre><p><em>That&#39;s fine, it&#39;s just not</em></p> <p><em>What the specification</em></p> <p><em>Says very clearly.</em></p> <p> </p> <p>                  <sup>-</sup> <sup>throwawaybeginr</sup></p> <hr/> <p><sup><sup>I&#39;m</sup></sup> <sup><sup>a</sup></sup> <sup><sup>bot</sup></sup> <sup><sup>made</sup></sup> <sup><sup>by</sup></sup> <sup><sup><a href="/u/Eight1911" rel="nofollow">/u/Eight1911</a>.</sup></sup> <sup><sup>I</sup></sup> <sup><sup>detect</sup></sup> <sup><sup>haiku.</sup></sup></p></pre>TheRealHellcat: <pre><p>None of what&#39;s written in that spec is broken.</p> <p>The only, small culprint of your example above is that the main routing (main() in itself is one as well) ends - due to the keeping the promise of a goroutine started being non-blocking - before the started goroutine had a chance to execute the actual &#34;print&#34; and - here comes the actual issue in the example - all goroutines are terminated when the main() ends/exits.</p> <p>Goroutines are not forked out as fully seperate, selfsustaining threads, they might behave like it but they are still running in the context and as part of the original process.</p></pre>cowsrool: <pre><p>See this for line endings <a href="" rel="nofollow"></a>. Gofmt is to standardize what code should look like and that includes no carriage returns. Notepad++ and wordpad both know how to handle no carriage returns</p></pre>Chillance: <pre><p>Things has been answered but let me simply answer anyway.</p> <p>You can see the &#34;go&#34; keyword as setting up what follows it in a separate &#34;thin thread&#34;, while continuing on with the program. And since your program ends before it has the time to actually execute that statement, nothing gets printed.</p> <p>I would recommend using VSCode as your editor for Go code. There is an extension that helps a lot developing in Go there. Auto formating when saving the file, and all other nice checks for the code written.</p></pre>LadyDascalie: <pre><blockquote> <p>notepad is not an appropriate text editor</p> </blockquote> <p>Just use Atom or vscode, there are plenty of options, all better than notepad.</p> <blockquote> <p>far from what it is written on the tin</p> </blockquote> <p>It does exactly what is written on the tin. It starts a goroutine.</p> <p>Your program exists before the goroutine has finished.</p> <p>Code execution does not stop and wait magically.</p> <p>Try adding a time.Sleep(time.Second)) after your statement, and see for yourself, then look into how to do this properly with <code>sync.WaitGroup</code> and other methods. </p> <p>All very well detailed on <a href="" rel="nofollow">Go By Example</a></p></pre>
41 次点击  
加入收藏 微博
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet