<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dan</title>
    <description>The latest articles on DEV Community by Dan (@denis_davydov_64474a53031).</description>
    <link>https://dev.to/denis_davydov_64474a53031</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3859755%2F1c2d57a2-9ce6-4559-9f14-5061fad4f53a.png</url>
      <title>DEV Community: Dan</title>
      <link>https://dev.to/denis_davydov_64474a53031</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/denis_davydov_64474a53031"/>
    <language>en</language>
    <item>
      <title>Detailed Explanation: What the go Keyword Actually Does in Golang</title>
      <dc:creator>Dan</dc:creator>
      <pubDate>Fri, 03 Apr 2026 20:25:51 +0000</pubDate>
      <link>https://dev.to/denis_davydov_64474a53031/what-does-go-keyword-actually-do-in-golang-2o10</link>
      <guid>https://dev.to/denis_davydov_64474a53031/what-does-go-keyword-actually-do-in-golang-2o10</guid>
      <description>&lt;p&gt;As we know, the go keyword runs goroutines. Or rather, this is how we often think about it.&lt;/p&gt;

&lt;p&gt;The go keyword in the Go programming language is one of its core elements, distinguishing it from other programming languages. It is used to launch a function in a separate goroutine. This allows the program to continue executing the current code without waiting for the invoked function to complete.&lt;/p&gt;

&lt;p&gt;In this article, we will examine the exact role of the go keyword and explore how its use affects program execution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}()&lt;/span&gt;

  &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What does this code print? If go keyword actually runs goroutine, most times we see “Hello World” as output of our program.&lt;/p&gt;

&lt;p&gt;But in reality the result is not determined. To figure it out, why this happens, we need to remember, what is goroutines queue in GMP model.&lt;/p&gt;

&lt;p&gt;&lt;a href="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%2Frf1ufjynp4ku2k4s7m2u.png" class="article-body-image-wrapper"&gt;&lt;img src="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%2Frf1ufjynp4ku2k4s7m2u.png" alt=" " width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this picture, we can see LRQ and GRQ. This is a local queue and global queue. Every “P” has its own local queue. “P” put “G” (which mean Goroutine) to “M” from that queue. But how goroutines get into this queue?&lt;/p&gt;

&lt;p&gt;To explain of this moment we actually need to understand what does do go keyword. How do we do this? Of course, by disassembling our program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;GOOS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;linux &lt;span class="nv"&gt;GOARCH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;amd64 go build &lt;span class="nt"&gt;-gcflags&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'-S'&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; /dev/null ./main.go &amp;amp;&amp;gt; asm.S

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As result we get the following go assembly code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;main.main STEXT size=50 args=0x0 locals=0x10 funcid=0x0 align=0x0
 0x0000 00000 (:5) TEXT main.main(SB), ABIInternal, $16-0
 0x0000 00000 (:5) CMPQ SP, 16(R14)
 0x0004 00004 (:5) PCDATA $0, $-2
 0x0004 00004 (:5) JLS 43
 0x0006 00006 (:5) PCDATA $0, $-1
 0x0006 00006 (:5) PUSHQ BP
 0x0007 00007 (:5) MOVQ SP, BP
 0x000a 00010 (:5) SUBQ $8, SP
 0x000e 00014 (:5) FUNCDATA $0, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
 0x000e 00014 (:5) FUNCDATA $1, gclocals·g2BeySu+wFnoycgXfElmcg==(SB)
 0x000e 00014 (:6) LEAQ main.main.func1·f(SB), AX
 0x0015 00021 (:6) PCDATA $1, $0
 0x0015 00021 (:6) CALL runtime.newproc(SB)
 0x001a 00026 (:9) XORL AX, AX
 0x001c 00028 (:9) NOP
 0x0020 00032 (:9) CALL os.Exit(SB)
 0x0025 00037 (:10) ADDQ $8, SP
 0x0029 00041 (:10) POPQ BP
 0x002a 00042 (:10) RET
 0x002b 00043 (:10) NOP
 0x002b 00043 (:5) PCDATA $1, $-1
 0x002b 00043 (:5) PCDATA $0, $-2
 0x002b 00043 (:5) CALL runtime.morestack_noctxt(SB)
 0x0030 00048 (:5) PCDATA $0, $-1
 0x0030 00048 (:5) JMP 0
 0x0000 49 3b 66 10 76 25 55 48 89 e5 48 83 ec 08 48 8d  I;f.v%UH..H...H.
 0x0010 05 00 00 00 00 e8 00 00 00 00 31 c0 0f 1f 40 00  ..........1...@.
 0x0020 e8 00 00 00 00 48 83 c4 08 5d c3 e8 00 00 00 00  .....H...]......
 0x0030 eb ce  

.....
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this assembly code, we interested two moments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0x000e 00014 (:6) LEAQ main.main.func1·f(SB), AX
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What does this code mean? We put the anonimous function func1 into AX register. This is how Linux ABI works. Linux ABI demand to passing arguments via registers.&lt;/p&gt;

&lt;p&gt;Second interested moment is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0x0015 00021 (:6) CALL runtime.newproc(SB)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code call runtime.newproc function. This function answers our question: what does go keyword actually do. Lets look at its source code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c"&gt;// Create a new g running fn.&lt;/span&gt;
&lt;span class="c"&gt;// Put it on the queue of g's waiting to run.&lt;/span&gt;
&lt;span class="c"&gt;// The compiler turns a go statement into a call to this.&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;newproc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fn&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;funcval&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
 &lt;span class="n"&gt;gp&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;getg&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
 &lt;span class="n"&gt;pc&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;getcallerpc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
 &lt;span class="n"&gt;systemstack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;newg&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;newproc1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="n"&gt;pp&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;getg&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ptr&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="n"&gt;runqput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;newg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;mainStarted&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="n"&gt;wakep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets skip the unnecessary details.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;newg&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;newproc1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This piece of code create new internal/runtime goroutine representation.&lt;/p&gt;

&lt;p&gt;And finally:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;runqput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;newg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Put this goroutine to local queue&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c"&gt;// runqput tries to put g on the local runnable queue.&lt;/span&gt;
&lt;span class="c"&gt;// If next is false, runqput adds g to the tail of the runnable queue.&lt;/span&gt;
&lt;span class="c"&gt;// If next is true, runqput puts g in the pp.runnext slot.&lt;/span&gt;
&lt;span class="c"&gt;// If the run queue is full, runnext puts g on the global queue.&lt;/span&gt;
&lt;span class="c"&gt;// Executed only by the owner P.&lt;/span&gt;
&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;runqput&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pp&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gp&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="o"&gt;....&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And… Nothing. There’s no more about running goroutine. Lets put it to local queue.&lt;/p&gt;

&lt;p&gt;In conclusion, the go keyword creates a goroutine and places it into a local queue. The actual execution is handled by the Go scheduler, which manages goroutines using the GMP model. Understanding this process is key to optimizing concurrency in Go.&lt;/p&gt;

</description>
      <category>go</category>
      <category>assembly</category>
    </item>
  </channel>
</rss>
