<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'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 "go."</p></li>
</ol>
<p>This works:</p>
<pre><code>package main
import "fmt"
func main() {
fmt.Println("hello world")
}
</code></pre>
<p>This does not work:</p>
<pre><code>package main
import "fmt"
func main() {
go fmt.Println("hello world")
}
</code></pre>
<p>(Notice the addition of the word 'go' 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 "hello world". 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'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 "go" 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'd if I can'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 "go fmt" 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="https://nathanleclaire.com/blog/2014/02/15/how-to-wait-for-all-goroutines-to-finish-executing-before-continuing/" rel="nofollow">https://nathanleclaire.com/blog/2014/02/15/how-to-wait-for-all-goroutines-to-finish-executing-before-continuing/</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="https://golang.org/ref/spec#Program_execution" rel="nofollow">https://golang.org/ref/spec#Program_execution</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 "upcoming lines in the program are evaluated" so you are aware that the program terminates once the end of main is reached (described elsewhere in the spec). The "independent concurrent thread of control" exist only during the lifetime of the whole program which finishes at the end of main.</p></pre>deusmetallum: <pre><p>I think you'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't give up on Go. It's a pretty awesome language, and recruiters are clambering to find people with those skills.</p></pre>haikubot-1911: <pre><p><em>That's fine, it'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'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'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 "print" 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="https://github.com/golang/go/issues/16355" rel="nofollow">https://github.com/golang/go/issues/16355</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 "go" keyword as setting up what follows it in a separate "thin thread", 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="https://gobyexample.com/" rel="nofollow">Go By Example</a></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传