<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged thread</title>
<link>https://coderlegion.com/tag/thread</link>
<description>Powered by Question2Answer</description>
<item>
<title>How does Go Routine work?</title>
<link>https://coderlegion.com/13375/how-does-go-routine-work</link>
<description>
&lt;p&gt;Hello, I'm Ganesh. I'm building &lt;em&gt;git-lrc&lt;/em&gt;, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. &lt;a href=&quot;https://github.com/HexmosTech/git-lrc&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;Star Us&lt;/a&gt; to help devs discover the project. Do give it a try and share your feedback for improving the product.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Go Routine?
&lt;/h2&gt;

&lt;p&gt;Go Routine is a lightweight thread managed by the Go Scheduler. Any thread created by the Go Runtime is handled by the Go Scheduler. &lt;/p&gt;

&lt;p&gt;Go Scheduler is a part of the Go Runtime that manages the execution of Go Routines. It is responsible for scheduling the execution of Go Routines and managing the state of the Go Routines. &lt;/p&gt;

&lt;p&gt;In Other Programming Languages, each thread has a stack size of 256 KB to 1 MB. But in Go, each thread has a stack size of 2 KB to 4 kb.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Concurrency Different From Parallelism?
&lt;/h2&gt;

&lt;p&gt;Concurrency &lt;/p&gt;

&lt;p&gt;It is mostly software based.&lt;br&gt;
Is the ability of a program to perform multiple tasks at the same time.&lt;br&gt;
It Focuses on reducing the execution time of a program.&lt;br&gt;
It is exececuting tasks in a most effient order the order is not specified it depends on the scheduler.&lt;/p&gt;

&lt;p&gt;Parallelism&lt;/p&gt;

&lt;p&gt;It is mostly hardware based.&lt;br&gt;
It is the ability of a processor to execute multiple instructions at the same time.&lt;br&gt;
It truly executes tasks in parallel.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to create a Go Routine?
&lt;/h2&gt;

&lt;p&gt;Every Go Program executes in a main go routine.&lt;/p&gt;

&lt;p&gt;Other go routines are created using &lt;code&gt;go&lt;/code&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight go&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fmt&quot;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;go&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;say&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;hello&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;say&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;world&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;say&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Sleep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;100&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Millisecond&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Println&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see that the output is not in order.&lt;/p&gt;

&lt;p&gt;This is because the Go Scheduler is responsible for scheduling the execution of Go Routines and managing the state of the Go Routines.&lt;/p&gt;

&lt;p&gt;We can't predict the output of the go routine.&lt;/p&gt;

&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class=&quot;highlight js-code-highlight&quot;&gt;
&lt;pre class=&quot;highlight console&quot;&gt;&lt;code&gt;&lt;span class=&quot;gp&quot;&gt;gk@jarvis:~/exp/code/rd/go-exmaple$&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;go run main.go 
&lt;span class=&quot;go&quot;&gt;hello
world
world
hello
hello
world
world
hello
hello
world
&lt;/span&gt;&lt;span class=&quot;gp&quot;&gt;gk@jarvis:~/exp/code/rd/go-exmaple$&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;go run main.go 
&lt;span class=&quot;go&quot;&gt;world
hello
hello
world
world
hello
hello
world
world
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In Next Article, we will see how to work with unordered events.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/HexmosTech/git-lrc&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;&lt;img src=&quot;https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fabn6nl3iehrohehnxgje.png&quot; alt=&quot;git-lrc&quot; width=&quot;800&quot; height=&quot;373&quot;&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Any feedback or contributors are welcome! It&amp;rsquo;s online, source-available, and ready for anyone to use. &lt;br&gt;
&amp;#11088; Star it on GitHub: &lt;a href=&quot;https://github.com/HexmosTech/git-lrc&quot; rel=&quot;noopener noreferrer nofollow&quot;&gt;https://github.com/HexmosTech/git-lrc&lt;/a&gt;&lt;/p&gt;


</description>
<category>Other</category>
<guid isPermaLink="true">https://coderlegion.com/13375/how-does-go-routine-work</guid>
<pubDate>Mon, 06 Apr 2026 06:47:50 +0000</pubDate>
</item>
</channel>
</rss>