Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The exact output of your program is not defined and depends on the scheduler. The scheduler can choose freely between all goroutines that are currently not blocked. It tries to run those goroutines concurrently by switching the current goroutine in very short time intervals so that the user gets the feeling that everything happens simultanously. In addition to that, it can also execute more than one goroutine in parallel on different CPUs (if you happen to have a multicore system and increase <a href="http://golang.org/pkg/runtime/#GOMAXPROCS" rel="nofollow">runtime.GOMAXPROCS</a>). One situation that might lead to your output is:</p> <ol> <li><code>main</code> creates two goroutines</li> <li>the scheduler chooses to switch to one of the new goroutines immediately and chooses <code>display</code></li> <li><code>display</code> prints out the message and is blocked by the channel send (<code>c &lt;- true</code>) since there isn't a receiver yet.</li> <li>the scheduler chooses to run <code>sum</code> next</li> <li>the sum is computed and printed on the screen</li> <li>the scheduler chooses to not resume the <code>sum</code> goroutine (it has already used a fair amount of time) and continues with <code>display</code></li> <li><code>display</code> sends the value to the channel</li> <li>the scheduler chooses to run main next</li> <li>main quits and all goroutines are destroyed</li> </ol> <p>But that is just one possible execution order. There are many others and some of them will lead to a different output. If you want to print just the first result and quit the program afterwards, you should probably use a <code>result chan string</code> and change your <code>main</code> function to print <code>fmt.Println(&lt;-result)</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload